| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "../libnetdata.h" |
| 4 | #include "setproctitle.h" |
| 5 | |
| 6 | void os_setproctitle(const char *new_name, const int argc, const char **argv) { |
| 7 | #ifdef HAVE_SYS_PRCTL_H |
| 8 | // Set the process name (comm) |
| 9 | prctl(PR_SET_NAME, new_name, 0, 0, 0); |
| 10 | #endif |
| 11 | |
| 12 | #ifdef __FreeBSD__ |
| 13 | // Set the process name on FreeBSD |
| 14 | setproctitle("%s", new_name); |
| 15 | #endif |
| 16 | |
| 17 | if(argc && argv) { |
| 18 | // replace with spaces all parameters found (except argv[0]) |
| 19 | for(int i = 1; i < argc ;i++) { |
| 20 | char *s = (char *)&argv[i][0]; |
| 21 | while(*s != '\0') *s++ = ' '; |
| 22 | } |
| 23 | |
| 24 | // overwrite argv[0] |
| 25 | size_t len = strlen(new_name); |
| 26 | const size_t argv0_len = strlen(argv[0]); |
| 27 | strncpyz((char *)argv[0], new_name, MIN(len, argv0_len)); |
| 28 | while(len < argv0_len) |
| 29 | ((char *)argv[0])[len++] = ' '; |
| 30 | } |
| 31 | } |