0

EDIT: Changed the title as the problem was misunderstood. I run the application automatically at startup but this is not my problem. My problem is that I cannot run Linux system command using popen() when the application is started up automatically, as I try to explain below.

I use popen() to run linux system command in a Qt C++ application to get information regarding my network connection, such as HW address, IP, mask, gateway, DHCP (if used).
To get this information I run the linux system command from my c++ code (assuming the ethernet adapter is eth0) : "nmcli dev list iface eth0".
This works fine in my test environment when I am (of course) logged in as a user.

However, the intention is that my application shall be set up to run right after boot in the background without the need to log in. This is working fine - my application starts after boot as expected.
BUT I am not able to run my Linux system command to get the network information I require. Is there any way to get around this? Can I call the system command from my c++ code in another manner or is there another way to get the required information?
The whole idea is that the application is run on a Linux box without the end-user having access the system, hence the reason for running without logging in.

Jon Helt-Hansen
  • 383
  • 1
  • 5
  • 19
  • 1
    Look at the `sysv` init system or `systemd`, depending on your distro. This question belongs to serverfault.com – Erbureth Jun 13 '14 at 12:39
  • I'm using Ubuntu, so systemd is relevant :) Could you please elaborate a little, what you mean? I'm afraid you lost me – Jon Helt-Hansen Jun 13 '14 at 12:45
  • The main point is that your app shall run as service started by the init system (`systemd` in your case). Don't worry about "logging in", every process is run under some user, all depends on who started it. I cannot give you more details, as I don't know anything about creating systemd services. – Erbureth Jun 13 '14 at 12:47
  • ahh I understand. I guess I wasn't clear enough. The problem is not to autostart my application - it starts up fine automatically after boot. The problem is that I cannot run the system command "nmcli dev list iface eth0", (which I run from my c++ code using popen) because I am not logged when the application is run automatically. – Jon Helt-Hansen Jun 13 '14 at 12:50
  • This sounds like a privilege issue -- which user is the program run under? – Erbureth Jun 13 '14 at 12:56
  • In short - if I log into the machine and run the application manually I can get the network information using "nmcli dev list iface" called from the application. If the application is run automatically after boot, I don't get the network information, hence my conclusion is that the system command cannot be run in this mode. The code used looks something like this: const char *cmd = QString("nmcli dev list iface eth0"); if (!(fp = popen(cmd,"r"))) return; while (fgets(buf, sizeof(buf), fp) != NULL) { } – Jon Helt-Hansen Jun 13 '14 at 13:00
  • just did a ssh to the machine. The program is run under the default user which is the same as when I log in on the machine. – Jon Helt-Hansen Jun 13 '14 at 13:06
  • Is the problem that NetworkManager doesn't start the network until you log in, or that your program doesn't have privileges? These are two different problems with different solutions. Can you tell us what is happening ? Do you see error messages? Note: with popen you lose stderr by default, which may have the error message, and you get the error code when you pclose. – dmansfield Jun 13 '14 at 13:07
  • The return value is 35584. I read this has to be divided by 256 to get the return value of the child process of interest, so in this case the return value is 139. As I understand this means segmentation fault. – Jon Helt-Hansen Jun 16 '14 at 06:43

0 Answers0