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. |
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. |
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? |
Code:
#define _GNU_SOURCE |
Why not just read?
Kevin Barry |
Quote:
Quote:
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. |
Just so we're all clear:
Code:
/proc/[number]/cmdline Kevin Barry |
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). |
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. |