kaegi / procps-sys Goto Github PK
View Code? Open in Web Editor NEWInterface procps library from Rust
License: GNU Affero General Public License v3.0
Interface procps library from Rust
License: GNU Affero General Public License v3.0
Hi @kaegi !
Thank you for your very useful crates.
I run the example, but can't get cmdline.
extern crate procps_sys;
use procps_sys::readproc::{openproc, readproc, closeproc, proc_t};
use std::ffi::CStr;
use std::ptr::null_mut;
fn main() {
unsafe {
// intialize query for process list
let proctab = openproc(/* fills cmdline line attribute */
procps_sys::readproc::PROC_FILLCOM);
// go through all processes
let mut procinfo = proc_t::default();
while readproc(proctab, &mut procinfo) != null_mut() {
// read cmdline attribute
let cmdline = if procinfo.cmdline == null_mut() {
"".to_string()
} else {
CStr::from_ptr(*procinfo.cmdline)
.to_string_lossy()
.into_owned()
};
// print information
println!("pid: {} cmdline: \"{}\"", procinfo.tid, cmdline);
}
closeproc(proctab);
}
}
$ cargo run
(snip)
pid: 862 cmdline: ""
pid: 867 cmdline: ""
pid: 869 cmdline: ""
pid: 873 cmdline: ""
pid: 884 cmdline: ""
pid: 898 cmdline: ""
pid: 901 cmdline: ""
pid: 992 cmdline: ""
pid: 1007 cmdline: ""
pid: 1061 cmdline: ""
pid: 1079 cmdline: ""
pid: 1080 cmdline: ""
pid: 1155 cmdline: ""
pid: 1170 cmdline: ""
pid: 1179 cmdline: ""
pid: 1181 cmdline: ""
pid: 1189 cmdline: ""
pid: 1232 cmdline: ""
pid: 2768 cmdline: ""
pid: 16229 cmdline: ""
pid: 16234 cmdline: ""
pid: 16510 cmdline: ""
pid: 16512 cmdline: ""
pid: 16514 cmdline: ""
pid: 16515 cmdline: ""
pid: 16572 cmdline: ""
pid: 16573 cmdline: ""
pid: 16629 cmdline: ""
pid: 16630 cmdline: ""
pid: 19256 cmdline: ""
pid: 19309 cmdline: ""
pid: 19310 cmdline: ""
pid: 22509 cmdline: ""
pid: 23120 cmdline: ""
pid: 23740 cmdline: ""
(snip)
I think char **
is always empty on Rust. but the following C code is run normaly.
$ cat misc/a.c
#include <string.h>
#include <stdio.h>
#include <proc/readproc.h>
int main(int argc, char **argv) {
PROCTAB *proc = openproc(PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS | PROC_FILLCOM);
proc_t *proc_info;
while ((proc_info = readproc(proc, NULL)) != NULL) {
if (proc_info->cmdline != NULL) {
printf("%20s:\t%5ld\t%5lld\t%5lld\t%20s\n", proc_info->cmd, proc_info->resident,
proc_info->utime, proc_info->stime, proc_info->cmdline[0]);
freeproc(proc_info);
}
}
closeproc(proc);
}
$ gcc misc/a.c -lprocps
$ ./a.out
systemd: 1485 119 1498 /sbin/init
systemd-journal: 1069 12 9 /lib/systemd/systemd-journald
lvmetad: 359 0 0 /sbin/lvmetad
systemd-udevd: 979 4 3 /lib/systemd/systemd-udevd
dhclient: 724 0 0 /sbin/dhclient
cron: 742 4 1 /usr/sbin/cron
rsyslogd: 885 2 2 /usr/sbin/rsyslogd
lxcfs: 1031 10 19 /usr/bin/lxcfs
dbus-daemon: 971 105 46 /usr/bin/dbus-daemon
snapd: 3397 7 1 /usr/lib/snapd/snapd
acpid: 305 0 0 /usr/sbin/acpid
systemd-logind: 792 0 5 /lib/systemd/systemd-logind
accounts-daemon: 1574 43 24 /usr/lib/accountsservice/accounts-daemon
atd: 544 0 0 /usr/sbin/atd
mdadm: 42 0 0 /sbin/mdadm
polkitd: 2026 1 0 /usr/lib/policykit-1/polkitd
memcached: 654 106 70 /usr/bin/memcached
redis-server: 2191 996 2274 /usr/bin/redis-server 127.0.0.1:6379
sshd: 1596 0 0 /usr/sbin/sshd
iscsid: 39 48 84 /sbin/iscsid
iscsid: 878 173 464 /sbin/iscsid
VBoxService: 627 34 745 /usr/sbin/VBoxService
irqbalance: 56 16 66 /usr/sbin/irqbalance
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.