Skip to content

Instantly share code, notes, and snippets.

@haxrob
Last active June 30, 2024 08:14
Show Gist options
  • Save haxrob/4a025d36a5ed96fd364f4f862aaa76bc to your computer and use it in GitHub Desktop.
Save haxrob/4a025d36a5ed96fd364f4f862aaa76bc to your computer and use it in GitHub Desktop.
Get process name and argv in Solaris from /proc/[pid/psinfo
### NOTE: Partially generated by ChatGTPo ###
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <procfs.h>
#include <dirent.h>
#include <string.h>
#define PROC_DIRECTORY "/proc"
int main() {
DIR *procdir;
struct dirent *entry;
char path[256];
psinfo_t psinfo;
procdir = opendir(PROC_DIRECTORY);
if (procdir == NULL) {
perror("Error opening /proc directory");
exit(1);
}
printf("PID\tCOMMAND\nARGV\n");
while ((entry = readdir(procdir)) != NULL) {
if (entry->d_name[0] >= '0' && entry->d_name[0] <= '9') {
snprintf(path, sizeof(path), "%s/%s/psinfo", PROC_DIRECTORY, entry->d_name);
int fd = open(path, O_RDONLY);
if (fd != -1) {
if (read(fd, &psinfo, sizeof(psinfo)) == sizeof(psinfo)) {
printf("%d\t%s\t%s\n", psinfo.pr_pid, psinfo.pr_fname, psinfo.pr_psargs);
}
close(fd);
}
}
}
closedir(procdir);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment