More work-related lols. Two folks were having a lengthy discussion about how they could possibly find out how long a process had been running. `ps -ef` completely escaped mention, but I'll let that slide as they wanted it to be automated. Cropping stuff from ps is a pain. I get it. The very first option they came up with was ... kernel modules. -_-
So here's a watchdog in 25 lines. 13 if you don't count includes, whitespace, or squigglies. Probably less if I dedicated some time to it. It makes several assumptions: 1) your system supports procfs, 2) you will not be changing any inode info on the procfs directory (links, ownership, file mode), 3) you are not a shitbag who will give it a massive PID and go lololol buffer overflow.
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <time.h> #include <unistd.h> int main( int argc, char **argv ) { char wut[32]; struct stat lol; time_t starttime, curtime; if (argc != 2) return 1; sprintf(wut, "/proc/%s", argv[1]); if (stat(wut, &lol) < 0) return -1; starttime = lol.st_ctime; curtime = time(NULL); printf("%ld\n", (curtime - starttime)); return 0; }
A patch for the shitbags:
--- main.c 2009-08-06 15:04:55.000000000 -0700
+++ main2.c 2009-08-06 15:04:16.000000000 -0700
@@ -1,3 +1,4 @@
+#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -13,7 +14,7 @@
if (argc != 2)
return 1;
- sprintf(wut, "/proc/%s", argv[1]);
+ snprintf(wut, sizeof(wut), "/proc/%s", argv[1]);
if (stat(wut, &lol) < 0)
return -1;
starttime = lol.st_ctime;