LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-30-2009, 04:08 PM   #1
nwarrenfl
LQ Newbie
 
Registered: Jan 2009
Posts: 18

Rep: Reputation: 0
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.
 
Old 11-30-2009, 04:15 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
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.

Last edited by johnsfine; 11-30-2009 at 04:22 PM.
 
Old 11-30-2009, 05:15 PM   #3
nwarrenfl
LQ Newbie
 
Registered: Jan 2009
Posts: 18

Original Poster
Rep: Reputation: 0
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?
 
Old 11-30-2009, 05:27 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
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?
 
Old 11-30-2009, 08:40 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Why not just read?
Kevin Barry

Last edited by ta0kira; 11-30-2009 at 08:41 PM.
 
Old 11-30-2009, 08:53 PM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by nwarrenfl View Post
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.
 
Old 12-01-2009, 01:04 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 12-01-2009, 07:32 AM   #8
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
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).
 
Old 12-01-2009, 02:57 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pxelinux to network boot, /proc/cmdline truncated early jeriryan Linux - Networking 0 07-16-2008 12:36 PM
scripting question: Extract a particular word from /proc/cmdline kushalkoolwal Programming 3 05-14-2008 02:48 AM
many /proc/<id>/cmdline files empty kpachopoulos Linux - General 1 08-25-2006 09:16 AM
How do I get the cmdline of a process in proc DonCorleonne Programming 1 05-05-2005 09:48 AM
/proc filesystem dawsonbdky Linux - General 1 08-16-2004 10:06 AM


All times are GMT -5. The time now is 05:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration