I wrote a program that captures the packets from the network interface. As it listens the network adapter, I need to run it with sudo
. The question is, why when I run it, does it add two processes to the processes list?
Note : lwdpi
is my program
Before execution:
ghasemi@ghasemi-MS-7693:~/Desktop/lwdpi_cpp$ ps ax | grep lwdpi
4665 pts/21 S+ 0:00 grep --color=auto lwdpi
ghasemi@ghasemi-MS-7693:~/Desktop/lwdpi_cpp$
Execution:
ghasemi@ghasemi-MS-7693:~/Desktop/lwdpi_cpp$ sudo ./lwdpi -i enp5s0
2016:10:26 11:07:29 :: 192.168.1.25 9918 --> 239.1.99.222 9918 UDP
2016:10:26 11:07:29 :: 192.168.1.111 5353 --> 224.0.0.251 5353 UDP
2016:10:26 11:07:30 :: 192.168.1.153 5353 --> 224.0.0.251 5353 UDP
2016:10:26 11:07:30 :: 192.168.1.154 5353 --> 224.0.0.251 5353 UDP
2016:10:26 11:07:30 :: 192.168.1.88 5353 --> 224.0.0.251 5353 UDP
2016:10:26 11:07:30 :: 192.168.1.60 5353 --> 224.0.0.251 5353 UDP
2016:10:26 11:07:37 :: 192.168.1.131 17500 --> 255.255.255.255 17500 UDP
2016:10:26 11:07:37 :: 192.168.1.131 17500 --> 192.168.1.255 17500 UDP
2016:10:26 11:07:37 :: 192.168.1.169 5546 --> 192.168.1.38 53 UDP
2016:10:26 11:07:37 :: 192.168.1.169 30955 --> 192.168.1.38 53 UDP
2016:10:26 11:07:38 :: 192.168.1.110 17500 --> 255.255.255.255 17500 UDP
2016:10:26 11:07:38 :: 192.168.1.110 17500 --> 192.168.1.255 17500 UDP
2016:10:26 11:07:42 :: 192.168.1.169 57189 --> 192.168.1.38 53 UDP
2016:10:26 11:07:42 :: 192.168.1.169 26072 --> 192.168.1.38 53 UDP
2016:10:26 11:07:42 :: 192.168.1.169 41674 --> 199.30.228.113 80 TCP
2016:10:26 11:07:43 :: 192.168.1.169 41676 --> 199.30.228.113 80 TCP
2016:10:26 11:07:43 :: 192.168.1.169 7190 --> 192.168.1.38 53 UDP
2016:10:26 11:07:43 :: 192.168.1.169 30029 --> 192.168.1.38 53 UDP
2016:10:26 11:07:43 :: 192.168.1.169 41678 --> 199.30.228.113 80 TCP
2016:10:26 11:07:43 :: 192.168.1.169 64975 --> 192.168.1.38 53 UDP
2016:10:26 11:07:43 :: 192.168.1.169 12625 --> 192.168.1.38 53 UDP
2016:10:26 11:07:43 :: 192.168.1.169 29973 --> 192.168.1.38 53 UDP
2016:10:26 11:07:43 :: 192.168.1.169 53300 --> 216.58.211.4 443 TCP
2016:10:26 11:07:43 :: 192.168.1.169 41682 --> 199.30.228.113 80 TCP
.
.
.
Processes list while execution:
ghasemi@ghasemi-MS-7693:~/Desktop/lwdpi_cpp$ ps ax | grep lwdpi
4685 pts/22 S+ 0:00 sudo ./lwdpi -i enp5s0
4686 pts/22 S+ 0:00 ./lwdpi -i enp5s0
4691 pts/21 S+ 0:00 grep --color=auto lwdpi
ghasemi@ghasemi-MS-7693:~/Desktop/lwdpi_cpp$
As you see above, after execution, processes with PID = 4685 and PID = 4686 added to process list. Why? I didn't called my program inside it!
When you do:
sudo
is the parent process, itfork(2)
s a child, which then doexecve(2)
with./lwdpi
as the executable nameso
lwdpi
issudo
's child processThis results in two processes as you can see, one is
sudo
and another islwdpi
.The best way to see the details is to check the PPID (parent process ID) too:
you'll see that
lwdpi
's parent issudo
itself.Here is
sudo
's process model, fromman sudo
:This happens when you start any process with
sudo
One process is thesudo
program and the other is the program launched withsudo
, which is a child process of the first. Thesudo
program will exit only when the process it is running for (its child) exits. Here's an extract from mypstree
when runningsudo apt update