LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C: Get cmdline from /proc filesystem (https://www.linuxquestions.org/questions/programming-9/c-get-cmdline-from-proc-filesystem-772529/)

nwarrenfl 11-30-2009 04:08 PM

C: Get cmdline from /proc filesystem
 
Greetings.

I'm currently reading the content of the /proc directory in my program to get information about a running process, everything worked so far but it seems i discovered a bug or something like that.

I need to get the cmdline, it works by using fopen() and fgets(), however it doesn't read arguments that have been passed to the executable.

When i do a simple "cat ./cmdline" arguments are shown, when opening it in a text editor too, but not with fgets(). I tried to open with nano and it displays the spacings as "^@".

Does anyone know what the problem is and how i can make it work?
Are there maybe Kernel functions (API) to do such things?

Kind regards, Warren.

johnsfine 11-30-2009 04:15 PM

It sounds like the delimiter is (char)0 where the original command line had one or more blanks.

Then I'm not sure whether your fgets is stopping on that (char)0 so you need another to get more. Or whether the fgets was OK and you just thought you got too little. Simple methods of displaying what you read would of course stop on the (char)0.

Such discussion as I have seen appears says fgets does not do anything special with '\0' in its input, which makes it quite difficult to distinguish an embedded '\0' in the string it read from the '\0' it adds at the end. Probably difficult enough that you should not use fgets for this purpose. Use some other function for reading.

nwarrenfl 11-30-2009 05:15 PM

Thanks for answering.
I tried using open() (non-standard C, but the code is Linux-specific...), but i had the same problem.
How can i read it with another function?

tuxdev 11-30-2009 05:27 PM

Code:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  FILE *cmdline = fopen("/proc/self/cmdline", "rb");
  char *arg = 0;
  size_t size = 0;
  while(getdelim(&arg, &size, 0, cmdline) != -1)
  {
      puts(arg);
  }
  free(arg);
  fclose(cmdline);
  return 0;
}

If you want to stay POSIX compliant, getdelim is a pretty trivial function to implement. C++ has something just like getdelim for free in the standard library, but we are talking about C here, right?

ta0kira 11-30-2009 08:40 PM

Why not just read?
Kevin Barry

johnsfine 11-30-2009 08:53 PM

Quote:

Originally Posted by nwarrenfl (Post 3774946)
I tried using open()

I don't think changing the way you open the file helps.

Quote:

How can i read it with another function?
The obvious (though certainly not only) choice is fread().

Assuming the requested length is large enough and there is no '\n' character in the buffer, I think fgets and fread would read the same thing into the buffer. The important difference is the return value from fread tells you how much it read.

If you just look in the buffer, the first '\0' you find is just the end of the argv[0] part. Just past that '\0' is the argv[1] part and so on for however many args there are. But you need to know where to stop. Knowing how much was read is the advantage of fread over fgets.

ta0kira 12-01-2009 01:04 AM

Just so we're all clear:
Code:

      /proc/[number]/cmdline
              This  holds  the complete command line for the process, unless the process is a zombie.
              In the latter case, there is nothing in this file: that is, a read on  this  file  will
              return  0 characters.  The command-line arguments appear in this file as a set of null-
              separated strings, with a further null byte ('\0') after the last string.

That tells me that read (with open proper) is the most appropriate because it doesn't involve a libc buffer and it returns the number of bytes read, a combination that isn't provided by other functions.
Kevin Barry

johnsfine 12-01-2009 07:32 AM

1) What is wrong with involving a libc buffer?

2) I should have looked for the documentation you just quoted, rather than guessing based on just the info the OP provided.

Now that I've seen the documentation, I think even fgets() would be fine for reading this file. The trick is what to do with the result.

Each part will be terminated with '\0' so a '\0' in the buffer doesn't mark the end of the whole command line, just the end of each part. The end of the whole command line will be marked with three '\0\ in a row (two in a row from the file, plus one more added by fgets).

ta0kira 12-01-2009 02:57 PM

I suggested read because it's a system call and the file isn't a text file, per se, because of the embedded null characters. For that reason, buffered operations, e.g. fgets, might interpret the input incorrectly, especially if for some reason an argument contained a newline. It's essentially just a binary data file that happens to be text and null characters merely by the convenience of the shell being a textual world. You gain nothing over read by using a libc stream operation, other than overhead.
Kevin Barry


All times are GMT -5. The time now is 06:54 PM.