我正在为运行debian jessie的嵌入式设备编写C ++系统监督守护程序。该守护进程需要能够重新配置网络设备。守护程序以root用户身份运行并使用调用系统(" ifdown --force -v eth0")和系统(" ifup --force -v eth0")来读取已更改的内容界面定义。当我在shell中以root用户身份键入这些命令时,我得到以下输出:
root@mds:/etc/network/interfaces.d# ifdown --force -v eth0
Reading directory /etc/network/interfaces.d
Parsing file /etc/network/interfaces.d/eth0
Parsing file /etc/network/interfaces.d/eth1
Parsing file /etc/network/interfaces.d/eth2
Configuring interface eth0=eth0 (inet)
run-parts --verbose /etc/network/if-down.d
run-parts: executing /etc/network/if-down.d/upstart
run-parts: executing /etc/network/if-down.d/wpasupplicant
ip -4 addr flush dev eth0 label eth0
ip link set dev eth0 down
run-parts --verbose /etc/network/if-post-down.d
run-parts: executing /etc/network/if-post-down.d/wpasupplicant
root@mds:/etc/network/interfaces.d# ifup --force -v eth0
Reading directory /etc/network/interfaces.d
Parsing file /etc/network/interfaces.d/eth0
Parsing file /etc/network/interfaces.d/eth1
Parsing file /etc/network/interfaces.d/eth2
Configuring interface eth0=eth0 (inet)
run-parts --exit-on-error --verbose /etc/network/if-pre-up.d
run-parts: executing /etc/network/if-pre-up.d/wpasupplicant
ip addr add 192.168.1.1/255.255.255.0 broadcast 192.168.1.255 dev eth0 label eth0
ip link set dev eth0 up
run-parts --exit-on-error --verbose /etc/network/if-up.d
run-parts: executing /etc/network/if-up.d/mountnfs
run-parts: executing /etc/network/if-up.d/openssh-server
run-parts: executing /etc/network/if-up.d/upstart
run-parts: executing /etc/network/if-up.d/wpasupplicant
但是当我在同一个shell中运行的守护进程中通过system()调用执行相同的命令时,我只能得到这个:
2017-07-10 17:25:16.591 +02:msg:MDSSV:15: executing ifdown command: ifdown --force -v eth0
Reading directory /etc/network/interfaces.d
Parsing file /etc/network/interfaces.d/eth0
Parsing file /etc/network/interfaces.d/eth1
Parsing file /etc/network/interfaces.d/eth2
2017-07-10 17:25:17.596 +02:msg:MDSSV:15: executing ifup command: ifup --force -v eth0
Reading directory /etc/network/interfaces.d
Parsing file /etc/network/interfaces.d/eth0
Parsing file /etc/network/interfaces.d/eth1
Parsing file /etc/network/interfaces.d/eth2
eth0设备保持不变。看起来ifup / ifdown只读取设备配置但不做任何其他操作。为什么ifup / ifdown在通过system()调用时表现不同?