From 0fee2289bed72bc88404cfed9ce9b4530210e788 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Fri, 8 Feb 2013 22:48:45 +0100 Subject: [PATCH] used a more sane setproctitle --- src/main.c | 7 ++-- src/setproctitle.c | 94 ++++++++++++---------------------------------- src/setproctitle.h | 7 ++-- 3 files changed, 30 insertions(+), 78 deletions(-) diff --git a/src/main.c b/src/main.c index 6254bb09..ef232acf 100644 --- a/src/main.c +++ b/src/main.c @@ -499,8 +499,7 @@ int main(int argc, char** argv) exit(1); } - initproctitle(argc, argv); - setproctitle(PACKAGE_NAME, "main"); + setproctitle(PACKAGE_NAME"-main"); if (getuid() != 0) { fprintf(stderr, "This server requires root access to operate.\n"); @@ -617,7 +616,7 @@ int main(int argc, char** argv) /* close any open descriptors before * running the server */ - setproctitle(PACKAGE_NAME, "worker"); + setproctitle(PACKAGE_NAME"-worker"); close(cmd_fd[0]); clear_lists(&s); @@ -688,7 +687,7 @@ fork_failed: need_maintainance = 0; pid = fork(); if (pid == 0) { /* child */ - setproctitle(PACKAGE_NAME, "maintainance"); + setproctitle(PACKAGE_NAME"-maint"); mslog(&s, NULL, LOG_INFO, "Performing maintainance"); clear_lists(&s); diff --git a/src/setproctitle.c b/src/setproctitle.c index e5da7501..37cafbd9 100644 --- a/src/setproctitle.c +++ b/src/setproctitle.c @@ -1,77 +1,31 @@ -/* This is taken from util-linux-2.22.2 which is under GPLv2. - */ - /* - * set process title for ps (from sendmail) + * Copyright (C) 2013 Nikos Mavrogiannopoulos * - * Clobbers argv of our main procedure so ps(1) will display the title. + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include -#include -#include -#include -#include "setproctitle.h" +#include +#include -#ifndef SPT_BUFSIZE -# define SPT_BUFSIZE 2048 +/* This sets the proccess title as shown in top, but not in ps (*@#%@). + * To change the ps name in Linux, one needs to do master black magic + * trickery (set util-linux setproctitle). + */ +void setproctitle (const char *prog) +{ +#ifdef PR_SET_NAME + prctl (PR_SET_NAME, prog); #endif - -extern char **environ; - -static char **argv0; -static int argv_lth; - -void initproctitle (int argc, char **argv) -{ - int i; - char **envp = environ; - - /* - * Move the environment so we can reuse the memory. - * (Code borrowed from sendmail.) - * WARNING: ugly assumptions on memory layout here; - * if this ever causes problems, #undef DO_PS_FIDDLING - */ - for (i = 0; envp[i] != NULL; i++) - continue; - - environ = (char **) malloc(sizeof(char *) * (i + 1)); - if (environ == NULL) - return; - - for (i = 0; envp[i] != NULL; i++) - if ((environ[i] = strdup(envp[i])) == NULL) - return; - environ[i] = NULL; - - argv0 = argv; - if (i > 0) - argv_lth = envp[i-1] + strlen(envp[i-1]) - argv0[0]; - else - argv_lth = argv0[argc-1] + strlen(argv0[argc-1]) - argv0[0]; -} - -void setproctitle (const char *prog, const char *txt) -{ - int i; - char buf[SPT_BUFSIZE]; - - if (!argv0) - return; - - if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE) - return; - - sprintf(buf, "%s -- %s", prog, txt); - - i = strlen(buf); - if (i > argv_lth - 2) { - i = argv_lth - 2; - buf[i] = '\0'; - } - memset(argv0[0], '\0', argv_lth); /* clear the memory area */ - strcpy(argv0[0], buf); - - argv0[1] = NULL; } diff --git a/src/setproctitle.h b/src/setproctitle.h index 70a9efa1..1c811bca 100644 --- a/src/setproctitle.h +++ b/src/setproctitle.h @@ -1,7 +1,6 @@ -#ifndef UTIL_LINUX_SETPROCTITLE_H -#define UTIL_LINUX_SETPROCTITLE_H +#ifndef SETPROCTITLE_H +# define SETPROCTITLE_H -extern void initproctitle (int argc, char **argv); -extern void setproctitle (const char *prog, const char *txt); +extern void setproctitle (const char *prog); #endif