4

I know that in a DOS/Windows application, you can issue system commands from code using lines like:

system("pause");

or

system("myProgram.exe");

...from stdlib.h. Is there a similar Linux command, and if so which header file would I find it in?

Also, is this considered bad programming practice? I am considering trying to get a list of loaded kernal modules using the lsmod command. Is that a good idea or bad idea? I found some websites that seemed to view system calls (at least system("pause");) in a negative light.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
8bitcartridge
  • 1,629
  • 6
  • 25
  • 38
  • i think you can use system("lsmod") or system("/usr/bin/lsmod") to do it... (use `which lsmod` to see where it is)... is this what you want? – nonopolarity Jan 07 '11 at 05:13
  • 1
    Why would you want to call "pause"? if you want to wait for users press a button before continuing use getch(); or std::cin.get(); I agree very bad form to use system("pause"); – DeveloperChris Jan 07 '11 at 05:20

4 Answers4

16

system is a bad idea for several reasons:

  • Your program is suspended until the command finishes.
  • It runs the command through a shell, which means you have to worry about making sure the string you pass is safe for the shell to evaluate.
  • If you try to run a backgrounded command with &, it ends up being a grandchild process and gets orphaned and taken in by the init process (pid 1), and you have no way of checking its status after that.
  • There's no way to read the command's output back into your program.

For the first and final issues, popen is one solution, but it doesn't address the other issues. You should really use fork and exec (or posix_spawn) yourself for running any external command/program.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • Great explanation of the dangers. And yes, `posix_spawn()` is the preferred method. – chrisaycock Jan 07 '11 at 05:34
  • 2
    Also note that for OP's specific need, parsing `/proc/modules` rather than using `lsmod` is the correct solution. – R.. GitHub STOP HELPING ICE Jan 07 '11 at 05:36
  • 1
    I've not seen posix_spawn() before, but WOW what a list of parameters. I feel it's worth adding that these downsides to system() are specific to what you're trying to achieve. I frequently use it specifically for the shell interpretation of the command (backgrounding the command excepted of course) where all I want is the side effect or exit code. – boycy Apr 26 '11 at 13:42
  • @boycy: Well `system` gives you a lot more than what you want it for. It messes up your signal handling, does non-thread-safe things, etc. in addition to bringing in the dangers of the shell. – R.. GitHub STOP HELPING ICE Oct 23 '13 at 01:31
  • "`system` gives you a lot more than what you want it for" - my point is that this isn't a _universal_ truth. Sometimes the listed behaviours are exactly what I want, so although `system` isn't always _right_, neither is it always wrong. (Though it is wrong in OP's case of course.) – boycy Oct 23 '13 at 08:28
11

Not surprisingly, the command is still

system("whatever");

and the header is still stdlib.h. That header file's name means "standard library", which means it's on every standard platform that supports C.

And yes, calling system() is often a bad idea. There are usually more programmatic ways of doing things.

If you want to see how lsmod works, you can always look-up its source code and see what the major system calls are that it makes. Then use those calls yourself.

A quick Google search turns up this link, which indicates that lsmod is reading the contents of /proc/modules.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
3

Well, lsmod does it by parsing the /proc/modules file. That would be my preferred method.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
0

I think what you are looking for are fork and exec.

Nick
  • 8,181
  • 4
  • 38
  • 63