Linux - how to change info of forked processes in C
Asked Answered
L

3

5

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?

Lucialucian answered 4/5, 2011 at 9:51 Comment(2)
Possibly duplicate of #140359Gladden
Well the question is a bit different, although there is an answer of my question there. I ask how to do it in C. And answer accepted was not the answer I was looking for. I'll copy the correct answer and leave decision to close it to moderators. Thanks for the link!Lucialucian
U
4

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.

Unduly answered 4/5, 2011 at 10:49 Comment(0)
L
0

in this article: setproctitle() in Linux the author explains how to change the process title. I guess setproctitle is available in BSD, so he tried to implement it for linux. Below is the function from lxc library

I haven't tested it yet. I also haven't checked if it uses other function from the library, so the code may be incomplete. Feel free to test it and leave a coment.

/*
 * Sets the process title to the specified title. Note that this may fail if
 * the kernel doesn't support PR_SET_MM_MAP (kernels <3.18).
 */
int setproctitle(char *title)
{
    static char *proctitle = NULL;
    char buf[2048], *tmp;
    FILE *f;
    int i, len, ret = 0;

    /* We don't really need to know all of this stuff, but unfortunately
     * PR_SET_MM_MAP requires us to set it all at once, so we have to
     * figure it out anyway.
     */
    unsigned long start_data, end_data, start_brk, start_code, end_code,
            start_stack, arg_start, arg_end, env_start, env_end,
            brk_val;
    struct prctl_mm_map prctl_map;

    f = fopen_cloexec("/proc/self/stat", "r");
    if (!f) {
        return -1;
    }

    tmp = fgets(buf, sizeof(buf), f);
    fclose(f);
    if (!tmp) {
        return -1;
    }

    /* Skip the first 25 fields, column 26-28 are start_code, end_code,
     * and start_stack */
    tmp = strchr(buf, ' ');
    for (i = 0; i < 24; i++) {
        if (!tmp)
            return -1;
        tmp = strchr(tmp+1, ' ');
    }
    if (!tmp)
        return -1;

    i = sscanf(tmp, "%lu %lu %lu", &start_code, &end_code, &start_stack);
    if (i != 3)
        return -1;

    /* Skip the next 19 fields, column 45-51 are start_data to arg_end */
    for (i = 0; i < 19; i++) {
        if (!tmp)
            return -1;
        tmp = strchr(tmp+1, ' ');
    }

    if (!tmp)
        return -1;

    i = sscanf(tmp, "%lu %lu %lu %*u %*u %lu %lu",
        &start_data,
        &end_data,
        &start_brk,
        &env_start,
        &env_end);
    if (i != 5)
        return -1;

    /* Include the null byte here, because in the calculations below we
     * want to have room for it. */
    len = strlen(title) + 1;

    proctitle = realloc(proctitle, len);
    if (!proctitle)
        return -1;

    arg_start = (unsigned long) proctitle;
    arg_end = arg_start + len;

    brk_val = syscall(__NR_brk, 0);

    prctl_map = (struct prctl_mm_map) {
        .start_code = start_code,
        .end_code = end_code,
        .start_stack = start_stack,
        .start_data = start_data,
        .end_data = end_data,
        .start_brk = start_brk,
        .brk = brk_val,
        .arg_start = arg_start,
        .arg_end = arg_end,
        .env_start = env_start,
        .env_end = env_end,
        .auxv = NULL,
        .auxv_size = 0,
        .exe_fd = -1,
    };

    ret = prctl(PR_SET_MM, PR_SET_MM_MAP, (long) &prctl_map, sizeof(prctl_map), 0);
    if (ret == 0)
        strcpy((char*)arg_start, title);
    else
        INFO("setting cmdline failed - %s", strerror(errno));

    return ret;
}
Lucialucian answered 31/5, 2017 at 20:43 Comment(2)
This is cool and if you know of the argc/argv info, you can very much simplify with the PR_SET_MM_ARGV_START/END directly. (which the author misspelled as PR_SET_MM_ENV_START/END...) That is, if you do want to keep the argv[1] to argv[argc] parameters (which is certainly not a requirement).Goosegog
Well... somehow the prctl(PR_SET_MM, PR_SET_MM_MAP, ...); works, but the ARGV tell me Operation not permitted. Unfortunately, though, it doesn't help for threads. All threads get the exact same name. I guess Linux is not yet that powerful that it can keep a distinct name of each thread.Goosegog
L
-1

I found the answer here although the question is not the same question. All credits should go to Chris Jester-Young

#include <string.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    strcpy(argv[0], "Hello, world!");
    sleep(10);
    return 0;
}
Lucialucian answered 4/5, 2011 at 15:0 Comment(1)
well that'll overwrite other stuff. I found another solution - I'll post it in separate answerLucialucian

© 2022 - 2024 — McMap. All rights reserved.