Search code examples
clinuxparsingprocfs

Accepted safe/future-proof way to parse /proc/%d/stat?


The second field of Linux /proc/%d/stat files is a command name enclosed in parentheses, which itself might contain parentheses as part of the command. What is the correct way to deal with this when parsing the stat pseudo-files? My inclination would be to find the last ')' (e.g. using strrchr on the whole file contents) but I'm concerned this might not be future-proof against addition of new fields at the end. Is there any documentation of the correct way to handle this issue?


Solution

  • Looking for the last ) is the best way to go about it and will most likely be future-proof.

    strrchr is used for parsing in the procps source (the ps family of functions, ps, top, kill, etc).

    S = strchr(S, '(') + 1;
    tmp = strrchr(S, ')');
    num = tmp - S;
    if(unlikely(num >= sizeof P->cmd)) num = sizeof P->cmd - 1;
    memcpy(P->cmd, S, num);
    P->cmd[num] = '\0';
    S = tmp + 2;                 // skip ") "