Skip to content

Instantly share code, notes, and snippets.

@userlandkernel
Last active April 6, 2020 15:45
Show Gist options
  • Save userlandkernel/3bedcf83c0821da9d71023abd579c0ba to your computer and use it in GitHub Desktop.
Save userlandkernel/3bedcf83c0821da9d71023abd579c0ba to your computer and use it in GitHub Desktop.
Get all processes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
extern int proc_pidpath(int pid, void * buffer, uint32_t buffersize);
#define MAXPATHLEN 1024
#define PROC_PIDPATHINFO_MAXSIZE MAXPATHLEN*4
#define MAXCOMLEN 16
struct proc_bsdshortinfo {
uint32_t pbsi_pid; /* process id */
uint32_t pbsi_ppid; /* process parent id */
uint32_t pbsi_pgid; /* process perp id */
uint32_t pbsi_status; /* p_stat value, SZOMB, SRUN, etc */
char pbsi_comm[MAXCOMLEN]; /* upto 16 characters of process name */
uint32_t pbsi_flags; /* 64bit; emulated etc */
uid_t pbsi_uid; /* current uid on process */
gid_t pbsi_gid; /* current gid on process */
uid_t pbsi_ruid; /* current ruid on process */
gid_t pbsi_rgid; /* current tgid on process */
uid_t pbsi_svuid; /* current svuid on process */
gid_t pbsi_svgid; /* current svgid on process */
uint32_t pbsi_rfu; /* reserved for future use*/
};
int main(int argc, char *argv[]) {
// For each process id
for(pid_t pid = 0; pid < UINT16_MAX;pid++){
char pathBuffer [PROC_PIDPATHINFO_MAXSIZE];
// Skip it if it is our own process
if(pid == getpid())
continue;
// Get the path of the process binary
int success = proc_pidpath(pid, pathBuffer, sizeof(pathBuffer));
// If we failed to get it then skip
if(!success)
continue;
// Get the position of the last path component (the process name)
char nameBuffer[256];
int position = strlen(pathBuffer);
while(position >= 0 && pathBuffer[position] != '/')
{
position--;
}
// Copy that name to our buffer
strcpy(nameBuffer, pathBuffer + position + 1);
// Print it
printf("path: %s\n\nname:%s\n\n", pathBuffer, nameBuffer);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment