fixed ps
This commit is contained in:
parent
c705f3b30b
commit
c6e315c24c
@ -71,15 +71,43 @@ int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fscanf(fp, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu", &proc_s->pid, proc_s->cmdline, &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, "%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);
|
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);
|
fclose(fp);
|
||||||
|
|
||||||
/* Remove ( and ) from string */
|
/* Remove ( and ) from string */
|
||||||
size_t len = strlen(proc_s->cmdline) - 1;
|
size_t len = strlen(proc_s->cmd) - 1;
|
||||||
proc_s->cmdline[len] = '\0';
|
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] = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
memmove(proc_s->cmdline, proc_s->cmdline + 1, len);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,5 +124,9 @@ int mu_proc_parser(const char *prog_name, const pid_t pid, struct mu_proc *proc_
|
|||||||
if (mu_proc_status(prog_name, pid, proc_s))
|
if (mu_proc_status(prog_name, pid, proc_s))
|
||||||
ret = 1;
|
ret = 1;
|
||||||
|
|
||||||
|
/* CMDLINE */
|
||||||
|
if (mu_proc_cmdline(prog_name, pid, proc_s))
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ struct mu_proc {
|
|||||||
|
|
||||||
/* from stat */
|
/* from stat */
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
char cmdline[PATH_MAX + 1];
|
char cmd[PATH_MAX + 1];
|
||||||
char state;
|
char state;
|
||||||
int ppid;
|
int ppid;
|
||||||
int pgrp;
|
int pgrp;
|
||||||
@ -36,6 +36,9 @@ struct mu_proc {
|
|||||||
unsigned long long starttime;
|
unsigned long long starttime;
|
||||||
unsigned long vsize;
|
unsigned long vsize;
|
||||||
long rss;
|
long rss;
|
||||||
|
|
||||||
|
/* from cmdline */
|
||||||
|
char cmdline[PATH_MAX + 1];
|
||||||
};
|
};
|
||||||
|
|
||||||
int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s);
|
int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s);
|
||||||
|
28
src/ps.c
28
src/ps.c
@ -13,28 +13,6 @@
|
|||||||
|
|
||||||
static char c_flag;
|
static char c_flag;
|
||||||
|
|
||||||
static char *get_cmdline(const pid_t pid) {
|
|
||||||
static char path[PATH_MAX + 1];
|
|
||||||
snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
|
|
||||||
|
|
||||||
int fd = open(path, O_RDONLY);
|
|
||||||
if (fd < 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
ssize_t size = read(fd, path, sizeof(path));
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
if (size == 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
for (ssize_t i = 0; i < size; i++) {
|
|
||||||
if (path[i] == '\0' && i != size - 1)
|
|
||||||
path[i] = ' ';
|
|
||||||
}
|
|
||||||
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int pscan(const pid_t pid) {
|
static int pscan(const pid_t pid) {
|
||||||
struct mu_proc proc;
|
struct mu_proc proc;
|
||||||
if (mu_proc_parser("ps", pid, &proc))
|
if (mu_proc_parser("ps", pid, &proc))
|
||||||
@ -56,9 +34,9 @@ static int pscan(const pid_t pid) {
|
|||||||
char rss[MU_HUMAN_BUF_SIZE + 1];
|
char rss[MU_HUMAN_BUF_SIZE + 1];
|
||||||
strcpy(rss, mu_humansize(proc.vmrss * 1024, 1024));
|
strcpy(rss, mu_humansize(proc.vmrss * 1024, 1024));
|
||||||
|
|
||||||
char *prog = (c_flag) ? get_cmdline(pid) : proc.cmdline;
|
char *prog = (c_flag) ? proc.cmdline : proc.cmd;
|
||||||
if (prog == NULL)
|
if (prog[0] == '\0')
|
||||||
prog = proc.cmdline;
|
return 0;
|
||||||
|
|
||||||
printf("%6d %8s %4ld %4ld %8s %8s %2c %02um:%02us %2s\n", proc.pid, name, proc.priority, proc.nice, virt, rss, proc.state, rtime / 60, rtime % 60, prog);
|
printf("%6d %8s %4ld %4ld %8s %8s %2c %02um:%02us %2s\n", proc.pid, name, proc.priority, proc.nice, virt, rss, proc.state, rtime / 60, rtime % 60, prog);
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user