U E D R , A S I H C RSS

Linux/Making Linux Daemon

์‚ฌ์ „ ์ง€์‹

root@zeropage:~# ps -efjc
UID        PID  PPID  PGID   SID CLS PRI STIME TTY          TIME CMD
root         1     0     0     0 TS   23 Oct15 ?        00:00:00 init [2]         
root         2     1     0     0 TS    5 Oct15 ?        00:00:00 [ksoftirqd/0]
root         3     1     0     0 TS   29 Oct15 ?        00:00:00 [events/0]
root         4     1     0     0 TS   28 Oct15 ?        00:00:00 [khelper]
root         5     1     0     0 TS   23 Oct15 ?        00:00:00 [kthread]
root         7     5     0     0 TS   19 Oct15 ?        00:00:00 [kacpid]
root        60     5     0     0 TS   29 Oct15 ?        00:00:00 [kblockd/0]
root        86     5     0     0 TS   24 Oct15 ?        00:00:00 [pdflush]
root        87     5     0     0 TS   24 Oct15 ?        00:00:00 [pdflush]
root        89     5     0     0 TS   22 Oct15 ?        00:00:00 [aio/0]
root        88     1     1     1 TS   24 Oct15 ?        00:00:01 [kswapd0]
root       674     1     1     1 TS   22 Oct15 ?        00:00:00 [kseriod]
root       943     1     1     1 TS   24 Oct15 ?        00:00:00 [kjournald]
root      1095     1     1     1 TS   24 Oct15 ?        00:00:01 [kjournald]
root      1096     1     1     1 TS   24 Oct15 ?        00:00:02 [kjournald]
root      1097     1     1     1 TS   24 Oct15 ?        00:00:00 [kjournald]
root      1098     1     1     1 TS   24 Oct15 ?        00:00:01 [kjournald]
root      1099     1     1     1 TS   24 Oct15 ?        00:00:00 [kjournald]
daemon    1191     1  1191  1191 TS   22 Oct15 ?        00:00:00 /sbin/portmap
root      1314     1  1313  1313 TS   24 Oct15 ?        00:00:00 /usr/sbin/syslogd
bind      1323     1  1323  1323 TS   21 Oct15 ?        00:00:00 /usr/sbin/named -u bind
105       1334     1  1334  1334 TS   21 Oct15 ?        00:00:00 /usr/bin/dbus-daemon-1 --system
104       1366     1  1366  1366 TS   23 Oct15 ?        00:00:00 /usr/sbin/exim4 -bd -q30m
root      1372     1  1371  1371 TS   18 Oct15 ?        00:00:00 /usr/sbin/inetutils-inetd
root      1387     1  1303  1303 TS   14 Oct15 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
root      1417  1387  1303  1303 TS   14 Oct15 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql     1418  1417  1303  1303 TS   23 Oct15 ?        00:06:23 /usr/sbin/mysqld --basedir=/usr --datad
root      1419  1417  1303  1303 TS   23 Oct15 ?        00:00:00 logger -p daemon.err -t mysqld_safe -i
root      1461     1  1461  1461 TS   24 Oct15 ?        00:00:00 /usr/sbin/sshd
root      1475     1  1303  1303 TS   14 Oct15 ?        00:03:00 /usr/local/jdk/jre/bin/java -Djava.util
root      1481     1  1481  1481 TS   23 Oct15 ?        00:00:00 /usr/sbin/vsftpd
root      1485     1  1485  1485 TS   21 Oct15 ?        00:00:00 /usr/sbin/famd -T 0
daemon    1497     1  1497  1497 TS   23 Oct15 ?        00:00:00 /usr/sbin/atd
root      1500     1  1500  1500 TS   23 Oct15 ?        00:00:00 /usr/sbin/cron
root      1538     1  1538  1538 TS   23 Oct15 tty1     00:00:00 /sbin/getty 38400 tty1
root      1540     1  1540  1540 TS   23 Oct15 tty2     00:00:00 /sbin/getty 38400 tty2
root      1541     1  1541  1541 TS   23 Oct15 tty3     00:00:00 /sbin/getty 38400 tty3
root      1542     1  1542  1542 TS   23 Oct15 tty4     00:00:00 /sbin/getty 38400 tty4
root      1543     1  1543  1543 TS   23 Oct15 tty5     00:00:00 /sbin/getty 38400 tty5
root      1544     1  1544  1544 TS   23 Oct15 tty6     00:00:00 /sbin/getty 38400 tty6
root      5722     1  5722  5722 TS   23 Oct16 ?        00:00:01 /usr/sbin/apache2 -k start -DSSL
www-data  5724  5722  5722  5722 TS   23 Oct16 ?        00:00:00 /usr/sbin/fcgi-pm -k start -DSSL
www-data 11061  5722  5722  5722 TS   24 22:30 ?        00:00:01 /usr/sbin/apache2 -k start -DSSL
www-data 11123  5722  5722  5722 TS   22 22:54 ?        00:00:04 /usr/sbin/apache2 -k start -DSSL
www-data 11184  5722  5722  5722 TS   23 23:05 ?        00:00:02 /usr/sbin/apache2 -k start -DSSL
www-data 11360  5722  5722  5722 TS   23 23:30 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11392  5722  5722  5722 TS   20 23:32 ?        00:00:03 /usr/sbin/apache2 -k start -DSSL
www-data 11393  5722  5722  5722 TS   23 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11394  5722  5722  5722 TS   23 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11395  5722  5722  5722 TS   24 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11396  5722  5722  5722 TS   23 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11397  5722  5722  5722 TS   23 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
root     11434  1461 11434 11434 TS   24 23:41 ?        00:00:00 sshd: sapius@pts/0
root     11436 11434 11436 11436 TS   24 23:41 pts/0    00:00:00 -bash
root     11453 11436 11453 11436 TS   22 23:41 pts/0    00:00:00 ps -efjc
์ƒ๊ธฐ์—์„œ ํ™•์ธ๊ฐ€๋Šฅ ํ•˜๋“ฏ daemon ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์€ ํ„ฐ๋ฏธ๋„์ด ํ• ๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค. ๋˜ํ•œ ๊ธฐ๋ณธ์ ์œผ๋กœ ParentPID (ProcessID)๊ฐ€ 1๋ฒˆ์œผ๋กœ ์„ค์ •๋˜์–ด์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” 1๋ฒˆ PID๋ฅผ ๊ฐ€์ง„ init ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ฐ๋ชฌ์˜ ๋™์ž‘์„ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

์‚ฌ์ „์ง€์‹

PPID ๊ฐ€ 1์ธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ง€์ผœ์•ผํ•  rule
1. fork ํ˜ธ์ถœํ•˜์—ฌ ์ž์‹ ํ”„๋กœ์„ธ์Šค ์ƒ์„ฑ. ๋ถ€๋ชจํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ
2. setsid ์ด์šฉ ์ƒˆ๋กœ์šด ์„ธ์…˜ ์ƒ์„ฑ, ํ˜„์žฌ ํ”„๋กœ์„ธ์Šค์˜ PID๊ฐ€ ์„ธ์…˜ ์ œ์–ด๊ถŒ์„ ๊ฐ€์ง€๋„๋ก ์„ค์ •
3. chdir ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ฃจํŠธ์—์„œ ์ž‘์—…ํ•˜๋„๋ก ๋ณ€๊ฒฝ
1 ๋ถ€์—ฐ์„ค๋ช…
์ž์‹์˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์ „์— ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ž์‹์˜ PPID๋Š” 1๋ฒˆ PID๋กœ ์„ค์ •๋œ๋‹ค.
2 ๋ถ€์—ฐ์„ค๋ช…
์ƒˆ๋กœ์šด ์„ธ์…˜์„ ๋งŒ๋“ ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ tty ๋ฅผ ๋ถ€์—ฌํ•ด์•ผํ•˜์ง€๋งŒ, ๋ถ€์—ฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ํ„ฐ๋ฏธ๋„์ด ์—†๋Š” ์„ธ์…˜์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.
3 ๋ถ€์—ฐ์„ค๋ช…
์ƒ๋Œ€๊ฒฝ๋กœ ์ง€์ •์œผ๋กœ ์ผ์–ด๋‚˜๋Š” ํ”„๋กœ๊ทธ๋žจ๋‚ด์˜ ๋ฌธ์ œํ•ด๊ฒฐ (์•ˆํ•ด๋„ ๋ฌด๊ด€)

sample code

~cpp
#include <sys/types.h> 
#include <sys/stat.h> 
#include <stdio.h> 
#include <fcntl.h> 

int main()
{
    pid_t   pid;

    if (( pid = fork()) < 0)
    exit(0);

    // ๋ถ€๋ชจํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. 
    else if(pid != 0)
    exit(0);
    
    //setsid();
    chdir("/");

    // ์—ฌ๊ธฐ์— ํ”„๋กœ๊ทธ๋žจ ๋ณธ์ฒด๋ฅผ ๋„ฃ๋Š”๋‹ค. 
    setsid();    
    while(1)
    {
    sleep(1);
    }
}

result

root@zeropage:~# ps -ejfc
UID        PID  PPID  PGID   SID CLS PRI STIME TTY          TIME CMD
root         1     0     0     0 TS   23 Oct15 ?        00:00:00 init [2]         
root         2     1     0     0 TS    5 Oct15 ?        00:00:00 [ksoftirqd/0]
root         3     1     0     0 TS   29 Oct15 ?        00:00:00 [events/0]
root         4     1     0     0 TS   28 Oct15 ?        00:00:00 [khelper]
root         5     1     0     0 TS   23 Oct15 ?        00:00:00 [kthread]
root         7     5     0     0 TS   19 Oct15 ?        00:00:00 [kacpid]
root        60     5     0     0 TS   29 Oct15 ?        00:00:00 [kblockd/0]
root        86     5     0     0 TS   24 Oct15 ?        00:00:00 [pdflush]
root        87     5     0     0 TS   24 Oct15 ?        00:00:00 [pdflush]
root        89     5     0     0 TS   22 Oct15 ?        00:00:00 [aio/0]
root        88     1     1     1 TS   24 Oct15 ?        00:00:01 [kswapd0]
root       674     1     1     1 TS   22 Oct15 ?        00:00:00 [kseriod]
root       943     1     1     1 TS   24 Oct15 ?        00:00:00 [kjournald]
root      1095     1     1     1 TS   24 Oct15 ?        00:00:01 [kjournald]
root      1096     1     1     1 TS   23 Oct15 ?        00:00:02 [kjournald]
root      1097     1     1     1 TS   24 Oct15 ?        00:00:00 [kjournald]
root      1098     1     1     1 TS   24 Oct15 ?        00:00:01 [kjournald]
root      1099     1     1     1 TS   24 Oct15 ?        00:00:00 [kjournald]
daemon    1191     1  1191  1191 TS   22 Oct15 ?        00:00:00 /sbin/portmap
root      1314     1  1313  1313 TS   23 Oct15 ?        00:00:00 /usr/sbin/syslogd
bind      1323     1  1323  1323 TS   21 Oct15 ?        00:00:00 /usr/sbin/named -u bind
105       1334     1  1334  1334 TS   21 Oct15 ?        00:00:00 /usr/bin/dbus-daemon-1 --system
104       1366     1  1366  1366 TS   23 Oct15 ?        00:00:00 /usr/sbin/exim4 -bd -q30m
root      1372     1  1371  1371 TS   18 Oct15 ?        00:00:00 /usr/sbin/inetutils-inetd
root      1387     1  1303  1303 TS   14 Oct15 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
root      1417  1387  1303  1303 TS   14 Oct15 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql     1418  1417  1303  1303 TS   24 Oct15 ?        00:06:23 /usr/sbin/mysqld --basedir=/usr --datad
root      1419  1417  1303  1303 TS   23 Oct15 ?        00:00:00 logger -p daemon.err -t mysqld_safe -i
root      1461     1  1461  1461 TS   24 Oct15 ?        00:00:00 /usr/sbin/sshd
root      1475     1  1303  1303 TS   14 Oct15 ?        00:03:01 /usr/local/jdk/jre/bin/java -Djava.util
root      1481     1  1481  1481 TS   23 Oct15 ?        00:00:00 /usr/sbin/vsftpd
root      1485     1  1485  1485 TS   21 Oct15 ?        00:00:00 /usr/sbin/famd -T 0
daemon    1497     1  1497  1497 TS   23 Oct15 ?        00:00:00 /usr/sbin/atd
root      1500     1  1500  1500 TS   23 Oct15 ?        00:00:00 /usr/sbin/cron
root      1538     1  1538  1538 TS   23 Oct15 tty1     00:00:00 /sbin/getty 38400 tty1
root      1540     1  1540  1540 TS   23 Oct15 tty2     00:00:00 /sbin/getty 38400 tty2
root      1541     1  1541  1541 TS   23 Oct15 tty3     00:00:00 /sbin/getty 38400 tty3
root      1542     1  1542  1542 TS   23 Oct15 tty4     00:00:00 /sbin/getty 38400 tty4
root      1543     1  1543  1543 TS   23 Oct15 tty5     00:00:00 /sbin/getty 38400 tty5
root      1544     1  1544  1544 TS   23 Oct15 tty6     00:00:00 /sbin/getty 38400 tty6
root      5722     1  5722  5722 TS   23 Oct16 ?        00:00:01 /usr/sbin/apache2 -k start -DSSL
www-data  5724  5722  5722  5722 TS   23 Oct16 ?        00:00:00 /usr/sbin/fcgi-pm -k start -DSSL
www-data 11061  5722  5722  5722 TS   23 22:30 ?        00:00:01 /usr/sbin/apache2 -k start -DSSL
www-data 11123  5722  5722  5722 TS   23 22:54 ?        00:00:05 /usr/sbin/apache2 -k start -DSSL
www-data 11184  5722  5722  5722 TS   23 23:05 ?        00:00:02 /usr/sbin/apache2 -k start -DSSL
www-data 11360  5722  5722  5722 TS   23 23:30 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11392  5722  5722  5722 TS   23 23:32 ?        00:00:04 /usr/sbin/apache2 -k start -DSSL
www-data 11394  5722  5722  5722 TS   23 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11395  5722  5722  5722 TS   23 23:32 ?        00:00:01 /usr/sbin/apache2 -k start -DSSL
www-data 11396  5722  5722  5722 TS   23 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11397  5722  5722  5722 TS   23 23:32 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
root     11434  1461 11434 11434 TS   23 23:41 ?        00:00:00 sshd: sapius@pts/0
root     11436 11434 11436 11436 TS   24 23:41 pts/0    00:00:00 -bash
'''root     11503     1 11503 11503 TS   23 23:49 ?        00:00:00 ./test'''
www-data 11508  5722  5722  5722 TS   23 23:50 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11509  5722  5722  5722 TS   23 23:50 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
www-data 11511  5722  5722  5722 TS   24 23:50 ?        00:00:00 /usr/sbin/apache2 -k start -DSSL
root     11521 11436 11521 11436 TS   23 23:50 pts/0    00:00:00 ps -ejfc
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:39
Processing time 0.0082 sec