--process-name-prefix

This commit is contained in:
Devaev Maxim
2019-10-11 06:23:32 +03:00
parent 890b248563
commit 70c7bcc209
8 changed files with 113 additions and 9 deletions

View File

@@ -111,6 +111,9 @@ enum _OPT_VALUES {
#ifdef HAS_PDEATHSIG
_O_EXIT_ON_PARENT_DEATH,
#endif
#ifdef WITH_SETPROCTITLE
_O_PROCESS_NAME_PREFIX,
#endif
_O_LOG_LEVEL,
_O_PERF,
@@ -179,6 +182,9 @@ static const struct option _LONG_OPTS[] = {
#ifdef HAS_PDEATHSIG
{"exit-on-parent-death", no_argument, NULL, _O_EXIT_ON_PARENT_DEATH},
#endif
#ifdef WITH_SETPROCTITLE
{"process-name-prefix", required_argument, NULL, _O_PROCESS_NAME_PREFIX},
#endif
{"log-level", required_argument, NULL, _O_LOG_LEVEL},
{"perf", no_argument, NULL, _O_PERF},
@@ -261,6 +267,9 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
int short_index;
int opt_index;
char short_opts[1024] = {0};
# ifdef WITH_SETPROCTITLE
char *process_name_prefix = NULL;
# endif
for (short_index = 0, opt_index = 0; _LONG_OPTS[opt_index].name != NULL; ++opt_index) {
if (isalpha(_LONG_OPTS[opt_index].val)) {
@@ -344,6 +353,9 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
};
break;
# endif
# ifdef WITH_SETPROCTITLE
case _O_PROCESS_NAME_PREFIX: OPT_SET(process_name_prefix, optarg);
# endif
case _O_LOG_LEVEL: OPT_NUMBER("--log-level", log_level, LOG_LEVEL_INFO, LOG_LEVEL_DEBUG, 0);
case _O_PERF: OPT_SET(log_level, LOG_LEVEL_PERF);
@@ -360,6 +372,12 @@ int parse_options(int argc, char *argv[], struct device_t *dev, struct encoder_t
}
}
# ifdef WITH_SETPROCTITLE
if (process_name_prefix != NULL) {
process_set_name_prefix(argc, argv, process_name_prefix);
}
# endif
# undef OPT_CTL_AUTO
# undef OPT_CTL
# undef OPT_PARSE
@@ -446,6 +464,15 @@ static void _version(bool nl) {
# endif
# ifdef WITH_GPIO
printf(" + GPIO");
# endif
# ifdef WITH_PTHREAD_NP
printf(" + PThreadNP");
# endif
# ifdef WITH_SETPROCTITLE
printf(" + SetProcTitle");
# endif
# ifdef HAS_PDEATHSIG
printf(" + PDeathSig");
# endif
if (nl) {
putchar('\n');
@@ -548,10 +575,16 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s
printf(" --gpio-workers-busy-at <pin> ── Set 1 on (pin + N) while worker with number N has a job.\n");
printf(" The worker's numbering starts from 0. Default: disabled\n\n");
#endif
#ifdef HAS_PDEATHSIG
#if (defined(HAS_PDEATHSIG) || defined(WITH_SETPROCTITLE))
printf("Process options:\n");
printf("════════════════\n");
printf(" --exit-on-parent-death ─ Exit the program if the parent process is dead. Default: disabled.\n\n");
#endif
#ifdef HAS_PDEATHSIG
printf(" --exit-on-parent-death ─────── Exit the program if the parent process is dead. Default: disabled.\n\n");
#endif
#ifdef WITH_SETPROCTITLE
printf(" --process-name-prefix <str> ── Set process name prefix which will be displayed in the process list\n");
printf(" like '<str>: ustreamer --blah-blah-blah'. Default: disabled.\n\n");
#endif
printf("Logging options:\n");
printf("════════════════\n");

View File

@@ -31,19 +31,46 @@
# endif
#endif
#ifdef HAS_PDEATHSIG
# include <signal.h>
# include <unistd.h>
#endif
#ifdef WITH_SETPROCTITLE
# include <stdlib.h>
# include <string.h>
# if defined(__linux__)
# include <bsd/unistd.h>
# include <sys/types.h>
# elif (defined(__FreeBSD__) || defined(__DragonFly__))
# include <unistd.h>
# include <sys/types.h>
# elif (defined(__NetBSD__) || defined(__OpenBSD__)) // setproctitle() placed in stdlib.h
# else
# error setproctitle() not implemented, you can disable it using WITH_SETPROCTITLE=0
# endif
#endif
#ifdef HAS_PDEATHSIG
# if defined(__linux__)
# include <sys/prctl.h>
# elif defined(__FreeBSD__)
# include <sys/procctl.h>
# endif
#endif
#ifdef WITH_SETPROCTITLE
# include "tools.h"
#endif
#ifdef HAS_PDEATHSIG
# include "logging.h"
#endif
#ifdef WITH_SETPROCTITLE
extern char **environ;
#endif
#ifdef HAS_PDEATHSIG
INLINE int process_track_parent_death(void) {
int signum = SIGTERM;
# if defined(__linux__)
@@ -65,5 +92,39 @@ INLINE int process_track_parent_death(void) {
return 0;
}
#endif
#endif // HAS_PDEATHSIG
#ifdef WITH_SETPROCTITLE
# pragma GCC diagnostic ignored "-Wunused-parameter"
# pragma GCC diagnostic push
INLINE void process_set_name_prefix(int argc, char *argv[], const char *prefix) {
# pragma GCC diagnostic pop
char *cmdline = NULL;
size_t allocated = 2048;
size_t used = 0;
size_t arg_len = 0;
A_REALLOC(cmdline, allocated);
cmdline[0] = '\0';
for (int index = 0; index < argc; ++index) {
arg_len = strlen(argv[index]);
if (used + arg_len + 16 >= allocated) {
allocated += arg_len + 2048;
A_REALLOC(cmdline, allocated);
}
strcat(cmdline, " ");
strcat(cmdline, argv[index]);
used = strlen(cmdline); // Не считаем вручную, так надежнее
}
# ifdef __linux__
setproctitle_init(argc, argv, environ);
# endif
setproctitle("-%s:%s", prefix, cmdline);
free(cmdline);
}
#endif