tags:

views:

98

answers:

2

Hi,

I need to run a linux command such as "df" from my linux daemon to know free space,used space, total size of the parition and other info. I have options like calling system,exec,popen etc..

  1. But as this each command spawn a new process , is this not possible to run the commands in the same process from which it is invoked?

  2. And at the same time as I need to run this command from a linux daemon, as my daemon should not hold any terminal. Will it effect my daemon behavior?

Or is their any C or C++ standard API for getting the mounted paritions information

+4  A: 

There is no standard API, as this is an OS-specific concept.

However,

  1. You can parse /proc/mounts (or /etc/mtab) with (non-portable) getmntent/getmntent_r helper functions.
  2. Using information about mounted filesystems, you can get its statistics with statfs.
Alex B
@Alex will this also give information about free and used space
siri
@siri, I have updated the answer with a better option (and yes it will).
Alex B
@Alex And I just want to confirm whether running a command from daemon is right or wrong and what side effects it may have
siri
@siri it's certainly not wrong. For example `cron` runs its jobs with `fork()`+`exec()`. But `df` uses `statfs`, so it's a more direct way.
Alex B
+1  A: 

You may find it useful to explore the i3status program source code: http://code.stapelberg.de/git/i3status/tree/src/print_disk_info.c

To answer your other questions:

But as this each command spawn a new process , is this not possible to run the commands in the same process from which it is invoked?

No; entire 'commands' are self-contained programs that must run in their own process.

Depending upon how often you wish to execute your programs, fork();exec() is not so bad. There's no hard limits beyond which it would be better to gather data yourself vs executing a helper program. Once a minute, you're probably fine executing the commands. Once a second, you're probably better off gathering the data yourself. I'm not sure where the dividing line is.

And at the same time as I need to run this command from a linux daemon, as my daemon should not hold any terminal. Will it effect my daemon behavior?

If the command calls setsid(2), then open(2) on a terminal without including O_NOCTTY, that terminal might become the controlling terminal for that process. But that wouldn't influence your program, because your program already disowned the terminal when becoming a daemon, and as the child process is a session leader, it cannot change your process's controlling terminal.

sarnold