133 lines
3.1 KiB
C
133 lines
3.1 KiB
C
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <ctype.h>
|
|
#include "proc_parser.h"
|
|
|
|
int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
|
|
proc_s->uid = -1;
|
|
proc_s->gid = -1;
|
|
|
|
/* Proccess file */
|
|
char path[PATH_MAX + 1];
|
|
snprintf(path, sizeof(path), "/proc/%d/status", pid);
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
if (fd < 0) {
|
|
if (prog_name)
|
|
fprintf(stderr, "%s: %s: %s\n", prog_name, path, strerror(errno));
|
|
|
|
return 1;
|
|
}
|
|
|
|
char status[2048];
|
|
ssize_t n = read(fd, status, sizeof(status) - 1);
|
|
status[n] = '\0';
|
|
close(fd);
|
|
|
|
char *token = strtok(status, "\n");
|
|
while (token) {
|
|
char *val = strchr(token, ':');
|
|
if (val == NULL) {
|
|
fprintf(stderr, "ps: incorrect %s file\n", path);
|
|
return 1;
|
|
}
|
|
|
|
*val = '\0';
|
|
val++;
|
|
|
|
/* Strip */
|
|
while (isspace(*val))
|
|
val++;
|
|
|
|
/* Write values */
|
|
if (!strncmp(token, "Uid", 3))
|
|
proc_s->uid = strtoul(val, 0L, 10);
|
|
|
|
else if (!strncmp(token, "Gid", 3))
|
|
proc_s->gid = strtoul(val, 0L, 10);
|
|
|
|
else if (!strncmp(token, "VmRSS", 3))
|
|
proc_s->vmrss = strtoul(val, 0L, 10);
|
|
|
|
token = strtok(NULL, "\n");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
|
|
char path[PATH_MAX + 1];
|
|
snprintf(path, sizeof(path), "/proc/%d/stat", pid);
|
|
|
|
FILE *fp = fopen(path, "r");
|
|
if (fp == NULL) {
|
|
if (prog_name)
|
|
fprintf(stderr, "%s: %s: %s\n", prog_name, path, strerror(errno));
|
|
|
|
return 1;
|
|
}
|
|
|
|
fscanf(fp, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &proc_s->pid, proc_s->cmd, &proc_s->state, &proc_s->ppid, &proc_s->pgrp, &proc_s->sid, &proc_s->tty, &proc_s->tpgid, &proc_s->flags, &proc_s->minflt, &proc_s->cminflt, &proc_s->majflt, &proc_s->cmajflt, &proc_s->utime, &proc_s->stime);
|
|
fscanf(fp, "%ld %ld %ld %ld %ld %ld %llu %lu %ld", &proc_s->cutime, &proc_s->cstime, &proc_s->priority, &proc_s->nice, &proc_s->num_threads, &proc_s->itrealvalue, &proc_s->starttime, &proc_s->vsize, &proc_s->rss);
|
|
fclose(fp);
|
|
|
|
/* Remove ( and ) from string */
|
|
size_t len = strlen(proc_s->cmd) - 1;
|
|
proc_s->cmd[len] = '\0';
|
|
|
|
memmove(proc_s->cmd, proc_s->cmd + 1, len);
|
|
return 0;
|
|
}
|
|
|
|
int mu_proc_cmdline(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
|
|
proc_s->cmdline[0] = '\0';
|
|
|
|
char path[PATH_MAX + 1];
|
|
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
if (fd < 0) {
|
|
if (prog_name)
|
|
fprintf(stderr, "%s: %s: %s\n", prog_name, path, strerror(errno));
|
|
|
|
return 1;
|
|
}
|
|
|
|
ssize_t size = read(fd, proc_s->cmdline, sizeof(proc_s->cmdline));
|
|
close(fd);
|
|
|
|
if (size == 0)
|
|
return 0;
|
|
|
|
for (ssize_t i = 0; i < size; i++) {
|
|
if (proc_s->cmdline[i] == '\0' && i != size - 1)
|
|
proc_s->cmdline[i] = ' ';
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int mu_proc_parser(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
|
|
memset(proc_s, 0, sizeof(struct mu_proc));
|
|
|
|
int ret = 0;
|
|
|
|
/* STAT */
|
|
if (mu_proc_stat(prog_name, pid, proc_s))
|
|
ret = 1;
|
|
|
|
/* STATUS */
|
|
if (mu_proc_status(prog_name, pid, proc_s))
|
|
ret = 1;
|
|
|
|
/* CMDLINE */
|
|
if (mu_proc_cmdline(prog_name, pid, proc_s))
|
|
ret = 1;
|
|
|
|
return ret;
|
|
}
|