Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - how to change info of forked processes in C

Tags:

c

linux

fork

gcc

The title may sound a bit strange, with ps aux I see this:

root     20953  0.0  0.0   9528  1280 ?        Ss   Apr28   0:07 sendmail: accepting connections

where "accepting connections" is something like a title to sendmail process. It's not an argument because cat /proc/20953/cmdline returns sendmail: accepting connections (space instead of 0x00):

# cat /proc/20953/cmdline |hexdump -C
00000000  73 65 6e 64 6d 61 69 6c  3a 20 61 63 63 65 70 74  |sendmail: accept|
00000010  69 6e 67 20 63 6f 6e 6e  65 63 74 69 6f 6e 73     |ing connections|
0000001f

Arguments in /proc fs are separated with null byte:

# cat /proc/26511/cmdline |hexdump -C
00000000  2f 62 69 6e 2f 62 61 73  68 00 2f 77 65 62 72 6f  |/bin/bash./webro|
00000010  6f 74 2f 70 72 6f 72 61  69 6c 2f 73 63 72 69 70  |ot/prorail/scrip|
00000020  74 73 2f 73 79 6e 63 6c  6f 6f 70 2e 73 68 00     |ts/syncloop.sh.|
0000002f

So, when I do fork() in C, how can I set this process information, so I can recognize which process which is?

like image 313
NickSoft Avatar asked Oct 11 '22 03:10

NickSoft


1 Answers

sendmail has a number of ways that it does, depending on on the system.see setproctitle in sendmail/conf.c in the source:

#define SPT_NONE        0       /* don't use it at all */
#define SPT_REUSEARGV   1       /* cover argv with title information */
#define SPT_BUILTIN     2       /* use libc builtin */
#define SPT_PSTAT       3       /* use pstat(PSTAT_SETCMD, ...) */
#define SPT_PSSTRINGS   4       /* use PS_STRINGS->... */
#define SPT_SYSMIPS     5       /* use sysmips() supported by NEWS-OS 6 */
#define SPT_SCO         6       /* write kernel u. area */
#define SPT_CHANGEARGV  7       /* write our own strings into argv[] */

See the setproctitle routine in conf.c for details.

like image 69
linuts Avatar answered Oct 15 '22 09:10

linuts