micro-utils/libmu/proc_parser.c

133 lines
3.1 KiB
C
Raw Normal View History

2024-07-09 06:23:04 +00:00
#include <fcntl.h>
2024-07-05 16:54:49 +00:00
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
2024-07-09 06:23:04 +00:00
#include <errno.h>
#include <ctype.h>
2024-07-09 19:33:45 +00:00
#include "proc_parser.h"
int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
2024-07-09 06:23:04 +00:00
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 */
2024-07-10 19:59:28 +00:00
if (!strncmp(token, "Uid", 3))
2024-07-09 06:23:04 +00:00
proc_s->uid = strtoul(val, 0L, 10);
else if (!strncmp(token, "Gid", 3))
proc_s->gid = strtoul(val, 0L, 10);
2024-07-10 13:59:36 +00:00
else if (!strncmp(token, "VmRSS", 3))
proc_s->vmrss = strtoul(val, 0L, 10);
2024-07-09 06:23:04 +00:00
token = strtok(NULL, "\n");
}
return 0;
}
2024-07-09 19:33:45 +00:00
int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
2024-07-09 06:23:04 +00:00
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;
}
2024-07-23 08:33:47 +00:00
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);
2024-07-10 13:59:36 +00:00
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);
2024-07-09 06:23:04 +00:00
fclose(fp);
2024-07-10 19:59:28 +00:00
/* Remove ( and ) from string */
2024-07-23 08:33:47 +00:00
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] = ' ';
}
2024-07-10 19:59:28 +00:00
2024-07-09 06:23:04 +00:00
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;
2024-07-23 08:33:47 +00:00
/* CMDLINE */
if (mu_proc_cmdline(prog_name, pid, proc_s))
ret = 1;
2024-07-09 06:23:04 +00:00
return ret;
}