-/*
- * /proc/pid/stat format:
- * pid (cmdname) S
- * [0] ppid pgid sid tty_nr tty_pgrp flags min_flt cmin_flt maj_flt cmaj_flt
- * [10] utime stime cutime cstime prio nice threads start_time vsize rss
- * [20] rsslim start_code end_code start_stack esp eip pending blocked sigign sigcatch
- * [30] wchan 0 0 exit_signal cpu rt_prio policy
- */
-
-static gint64
-get_process_time (int pid, int pos, int sum)
-{
- char buf [512];
- char *s, *end;
- FILE *f;
- int len, i;
- gint64 value;
-
- g_snprintf (buf, sizeof (buf), "/proc/%d/stat", pid);
- f = fopen (buf, "r");
- if (!f)
- return 0;
- len = fread (buf, 1, sizeof (buf), f);
- fclose (f);
- if (len <= 0)
- return 0;
- s = strchr (buf, ')');
- if (!s)
- return 0;
- s++;
- while (g_ascii_isspace (*s)) s++;
- if (!*s)
- return 0;
- /* skip the status char */
- while (*s && !g_ascii_isspace (*s)) s++;
- if (!*s)
- return 0;
- for (i = 0; i < pos; ++i) {
- while (g_ascii_isspace (*s)) s++;
- if (!*s)
- return 0;
- while (*s && !g_ascii_isspace (*s)) s++;
- if (!*s)
- return 0;
- }
- /* we are finally at the needed item */
- value = strtoul (s, &end, 0);
- /* add also the following value */
- if (sum) {
- while (g_ascii_isspace (*s)) s++;
- if (!*s)
- return 0;
- value += strtoul (s, &end, 0);
- }
- return value;
-}
-
-static gint64
-get_pid_stat_item (int pid, const char *item)
-{
- char buf [256];
- char *s;
- FILE *f;
- int len = strlen (item);
-
- g_snprintf (buf, sizeof (buf), "/proc/%d/status", pid);
- f = fopen (buf, "r");
- if (!f)
- return 0;
- while ((s = fgets (buf, sizeof (buf), f))) {
- if (*item != *buf)
- continue;
- if (strncmp (buf, item, len))
- continue;
- if (buf [len] != ':')
- continue;
- fclose (f);
- return atoi (buf + len + 1);
- }
- fclose (f);
- return 0;
-}
-