Skip to content

Instantly share code, notes, and snippets.

@mnunberg
Created June 11, 2012 22:16
Show Gist options
  • Save mnunberg/2913069 to your computer and use it in GitHub Desktop.
Save mnunberg/2913069 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#define PROCSCAN_XFLD(X) \
X(0, PID, pid,\
"d", int)\
X(1, COMM, comm,\
"s", char*)\
X(2, STATE, state,\
"c", char)\
X(3, PPID, ppid,\
"d", int)\
X(4, PGRP, pgrp,\
"d", int)\
X(5, SID, sid,\
"d", int)\
X(6, TTYNR, ttynr,\
"d", int)\
X(7, TGPID, tgpid,\
"d", int)\
X(8, FLAGS, flags,\
"lu", long unsigned int)\
X(9, MINFLT, minflt,\
"lu", long unsigned int)\
X(10, CMINFLT, cminflt,\
"lu", long unsigned int)\
X(11, MAJFLT, majflt,\
"lu", long unsigned int)\
X(12, CMAJFLT, cmajflt,\
"lu", long unsigned int)\
X(13, UTIME, utime,\
"lu", long unsigned int)\
X(14, STIME, stime,\
"lu", long unsigned int)\
X(15, CUTIME, cutime,\
"ld", long int)\
X(16, CSTIME, cstime,\
"ld", long int)\
X(17, PRIORITY, priority,\
"ld", long int)\
X(18, NICE, nice,\
"ld", long int)\
X(19, NTHREADS, nthreads,\
"ld", long int)\
X(20, ITREAL, itreal,\
"ld", long int)\
X(21, STARTTIME, starttime,\
"llu", long long unsigned int)\
X(22, VSIZE, vsize,\
"lu", long unsigned int)\
X(23, RSS, rss,\
"ld", long int)\
X(24, RSSLIM, rsslim,\
"lu", long unsigned int)\
X(25, STARTCODE, startcode,\
"lu", long unsigned int)\
X(26, ENDCODE, endcode,\
"lu", long unsigned int)\
X(27, STARTSTACK, startstack,\
"lu", long unsigned int)\
X(28, ESP, esp,\
"lu", long unsigned int)\
X(29, EIP, eip,\
"lu", long unsigned int)\
X(30, SIGPENDING, sigpending,\
"lu", long unsigned int)\
X(31, SIGBLOCKED, sigblocked,\
"lu", long unsigned int)\
X(32, SIGCATCH, sigcatch,\
"lu", long unsigned int)\
X(33, WCHAN, wchan,\
"lu", long unsigned int)\
X(34, NSWAP, nswap,\
"lu", long unsigned int)\
X(35, CNSWAP, cnswap,\
"lu", long unsigned int)\
X(36, EXITSIGNAL, exitsignal,\
"lu", long unsigned int)\
X(37, PROCESSOR, processor,\
"d", int)\
X(38, RTPRIORITY, rtpriority,\
"u", unsigned int)\
X(39, POLICY, policy,\
"u", unsigned int)\
X(40, BLKIOTICKS, blkioticks,\
"llu", long long unsigned int)\
X(41, GUESTTIME, guesttime,\
"lu", long unsigned int)
struct procstat {
char buf[512];
#define X(idx, constname, fld_name, fmt, ctp) \
ctp pst_##fld_name;
PROCSCAN_XFLD(X)
#undef X
};
int
procinfo_get(pid_t pid, struct procstat *pstb)
{
FILE *fp;
char path[NAME_MAX];
char *buf;
char *fields[128] = { NULL };
int ii, ret;
int nbuf, nfields, bufmax;
bufmax = sizeof(pstb->buf);
buf = pstb->buf;
snprintf(path, sizeof(path), "/proc/%d/stat", pid);
fp = fopen(path, "r");
if (!fp) {
perror(path);
ret = -1;
goto GT_RET;
}
if ( (nbuf = fread(buf, 1, bufmax, fp)) == 0) {
fprintf(stderr, "Nothing in file (%s)\n", path);
ret = -1;
goto GT_RET;
}
buf[nbuf-1] = '\0';
if (!feof(fp)) {
fprintf(stderr, "%s too large\n", path);
ret = -1;
goto GT_RET;
}
nfields = 0;
for (ii = nbuf; ii >= 0; ii--) {
if (buf[ii] == ' ') {
buf[ii] = '\0';
fields[nfields++] = buf + ii + 1;
} else if (buf[ii] == ')') {
break;
}
}
/**
* Now, let's parse in the last fields, but doing it from the beginning.
*/
fields[nfields + 1] = buf;
for (ii = 0; ii < nbuf; ii++) {
if (buf[ii] == ' ') {
buf[ii] = '\0';
fields[nfields] = buf + ii + 1;
break;
}
}
nfields++;
#define X(idx, cnst, fld, fmt, t) \
if (fields[nfields-idx]) { \
if (*fmt == 's') { \
*(char**)&(pstb->pst_##fld) = fields[nfields-idx]; \
} else { \
sscanf(fields[nfields-idx], "%"fmt, &(pstb->pst_##fld)); \
} \
}
PROCSCAN_XFLD(X)
#undef X
GT_RET:
if (fp) {
fclose(fp);
}
return ret;
}
int
procinfo_cmp(struct procstat* p1, struct procstat *p2)
{
return 0;
}
int main(void) {
struct procstat pstb;
procinfo_get(getpid(), &pstb);
#define X(idx, cnst, fld, fmt, t) \
printf("Found field %s at index %d: %"fmt"\n", \
#fld, idx, pstb.pst_##fld);
PROCSCAN_XFLD(X)
#undef X
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment