From 456cd71bf4198e095afcc13f49d8f95f1e5e21fd Mon Sep 17 00:00:00 2001 From: Arvid Requate Date: Tue, 28 Aug 2012 13:45:50 +0200 Subject: [PATCH 113/113] s4: add and use setproctitle replace function --- lib/replace/replace.h | 11 ++ lib/replace/setproctitle.c | 319 +++++++++++++++++++++++++++++++++++++++ lib/replace/wscript | 5 +- source4/smbd/process_standard.c | 14 +- source4/smbd/server.c | 16 +- source4/smbd/wscript_build | 2 +- 6 files changed, 351 insertions(+), 16 deletions(-) create mode 100644 lib/replace/setproctitle.c diff --git a/lib/replace/replace.h b/lib/replace/replace.h index fa7cc78..33c92da 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -845,4 +845,15 @@ typedef long useconds_t; int usleep(useconds_t); #endif +#ifdef HAVE_SETPROCTITLE +#ifdef HAVE_SETPROCTITLE_H +#include +#endif +#else /* no HAVE_SETPROCTITLE */ +#define setproctitle rep_setproctitle +void rep_setproctitle(const char *format, ...); +#endif +char **rep_setproctitle_init(int argc, char **argv); +#define setproctitle_init rep_setproctitle_init + #endif /* _LIBREPLACE_REPLACE_H */ diff --git a/lib/replace/setproctitle.c b/lib/replace/setproctitle.c new file mode 100644 index 0000000..b5e7df8 --- /dev/null +++ b/lib/replace/setproctitle.c @@ -0,0 +1,319 @@ +/* +PostgreSQL Database Management System +(formerly known as Postgres, then as Postgres95) + +Portions Copyright (c) 1996-2012, The PostgreSQL Global Development Group + +Portions Copyright (c) 1994, The Regents of the University of California + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose, without fee, and without a written agreement +is hereby granted, provided that the above copyright notice and this paragraph +and the following two paragraphs appear in all copies. + +IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR +DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING +LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, +EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. + +THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS +ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS +TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + +*/ + +/*-------------------------------------------------------------------- + * setproctitle.c + * Support setproctitle() on platforms that don't have it. + * + * Copyright (c) 2000-2011, PostgreSQL Global Development Group + * various details abducted from various places + * + * Copyright (C) 2012 Arvid Requate. + * Adjusted for Samba to mimic API of BSD setproctitle. + *-------------------------------------------------------------------- + */ + +#include "includes.h" + +#include +#ifdef HAVE_SYS_PSTAT_H +#include /* for HP-UX */ +#endif +#ifdef HAVE_PS_STRINGS +#include /* for old BSD */ +#include +#endif +#if defined(__darwin__) +#include +#endif + +#include "string.h" +#include "malloc.h" +#include "assert.h" + +extern char **environ; + + +/* + * Alternative empulations of BSD setproctitle + * + * PS_USE_CLOBBER_ARGV + * write over the argv and environment area + * (Linux and most SysV-like systems) + * PS_USE_CHANGE_ARGV + * assign argv[0] = "string" + * (some other BSD systems) + * PS_USE_PS_STRINGS + * assign PS_STRINGS->ps_argvstr = "string" + * (some BSD systems) + * PS_USE_PSTAT + * use the pstat(PSTAT_SETCMD, ) + * (HPUX) + * PS_USE_NONE + * don't update ps display + * (This is the default, as it is safest.) + */ +#if defined(__linux__) || defined(_AIX) || defined(__sgi) || (defined(sun) && !defined(BSD)) || defined(ultrix) || defined(__ksr__) || defined(__osf__) || defined(__svr4__) || defined(__svr5__) || defined(__darwin__) +#define PS_USE_CLOBBER_ARGV +#elif (defined(BSD) || defined(__bsdi__) || defined(__hurd__)) && !defined(__darwin__) +#define PS_USE_CHANGE_ARGV +#elif defined(HAVE_PS_STRINGS) +#define PS_USE_PS_STRINGS +#elif defined(HAVE_PSTAT) && defined(PSTAT_SETCMD) +#define PS_USE_PSTAT +#else +#define PS_USE_NONE +#endif + + +/* Different systems want the buffer padded differently */ +#if defined(_AIX) || defined(__linux__) || defined(__svr4__) || defined(__darwin__) +#define PS_PADDING '\0' +#else +#define PS_PADDING ' ' +#endif + + +#ifndef PS_USE_CLOBBER_ARGV +/* all but one option need a buffer to write their ps line in */ +#define PS_BUFFER_SIZE 256 +static char ps_buffer[PS_BUFFER_SIZE]; +static const size_t ps_buffer_size = PS_BUFFER_SIZE; +#else /* PS_USE_CLOBBER_ARGV */ +static char *ps_buffer; /* will point to argv area */ +static size_t ps_buffer_size; /* space determined at run time */ +#endif /* PS_USE_CLOBBER_ARGV */ + +static size_t ps_buffer_cur_len; /* nominal strlen(ps_buffer) */ + +/* save the original argv[] location here */ +static int orig_argc; +static char **orig_argv; + +/* save the new argv[] location here */ +static char **copied_argv; + + +/* + * Call this early in startup to save the original argc/argv values. + * If needed, we make a copy of the original argv[] array to preserve it + * from being clobbered by subsequent ps_display actions. + * + * (The original argv[] will not be overwritten by this routine, but may be + * overwritten during init_ps_display. Also, the physical location of the + * environment strings may be moved, so this should be called before any code + * that might try to hang onto a getenv() result.) + */ +char ** +rep_setproctitle_init(int argc, char **argv) +{ + +#ifdef HAVE_SETPROCTITLE + return; +#else + + orig_argc = argc; + orig_argv = argv; + +#if defined(PS_USE_CLOBBER_ARGV) + + /* + * If we're going to overwrite the argv area, count the available space. + * Also move the environment to make additional room. + */ + { + char *end_of_area = NULL; + char **copied_environ; + int i; + + /* + * check for contiguous argv strings + */ + for (i = 0; i < argc; i++) + { + if (i == 0 || end_of_area + 1 == argv[i]) + end_of_area = argv[i] + strlen(argv[i]); + } + + if (end_of_area == NULL) /* probably can't happen? */ + { + ps_buffer = NULL; + ps_buffer_size = 0; + return argv; + } + + /* + * check for contiguous environ strings following argv + */ + for (i = 0; environ[i] != NULL; i++) + { + if (end_of_area + 1 == environ[i]) + end_of_area = environ[i] + strlen(environ[i]); + } + + ps_buffer = argv[0]; + ps_buffer_size = end_of_area - argv[0]; + + /* + * move the environment out of the way + */ + copied_environ = (char **) malloc((i + 1) * sizeof(char *)); + for (i = 0; environ[i] != NULL; i++) + copied_environ[i] = strdup(environ[i]); + copied_environ[i] = NULL; + environ = copied_environ; + } +#endif /* PS_USE_CLOBBER_ARGV */ + +#if defined(PS_USE_CHANGE_ARGV) || defined(PS_USE_CLOBBER_ARGV) + + /* + * If we're going to change the original argv[] then make a copy for + * argument parsing purposes. + * + * (NB: do NOT think to remove the copying of argv[], even though + * postmaster.c finishes looking at argv[] long before we ever consider + * changing the ps display. On some platforms, getopt() keeps pointers + * into the argv array, and will get horribly confused when it is + * re-called to analyze a subprocess' argument string if the argv storage + * has been clobbered meanwhile. Other platforms have other dependencies + * on argv[]. + */ + { + int i; + + copied_argv = (char **) malloc((argc + 1) * sizeof(char *)); + for (i = 0; i < argc; i++) { + copied_argv[i] = strdup(argv[i]); + } + copied_argv[argc] = NULL; + +#if defined(__darwin__) + + /* + * Darwin (and perhaps other NeXT-derived platforms?) has a static + * copy of the argv pointer, which we may fix like so: + */ + *_NSGetArgv() = copied_argv; +#endif + + return copied_argv; + } +#endif /* PS_USE_CHANGE_ARGV or PS_USE_CLOBBER_ARGV */ + +#endif /* HAVE_SETPROCTITLE */ + return argv; +} + + +void rep_setproctitle(const char *format, ...) +{ + va_list ap; + +#ifdef PS_USE_NONE + return; +#endif /* not PS_USE_NONE */ + + /* no ps display if you didn't call setproctitle_init() */ + if (!orig_argv) { + return; + } + +#ifdef PS_USE_CLOBBER_ARGV + /* If ps_buffer is a dynamic pointer, it might still be null */ + if (!ps_buffer) { + return; + } +#endif /* PS_USE_CLOBBER_ARGV */ + + /* + * write title to ps_buffer + */ + + va_start(ap, format); + + if (format) { + ps_buffer[ps_buffer_size - 1] = '\0'; + + if (format[0] == '-') { + /* skip program name prefix */ + format++; + ps_buffer_cur_len = 0; + } else { + /* print program name heading for grep */ + (void)snprintf(ps_buffer, ps_buffer_size, "%s: ", copied_argv[0]); + ps_buffer_cur_len = strlen(ps_buffer); + } + + /* print the argument string */ + (void) vsnprintf(ps_buffer + ps_buffer_cur_len, ps_buffer_size - ps_buffer_cur_len, format, ap); + ps_buffer_cur_len = strlen(ps_buffer); + + } else { + return; + } + + va_end(ap); + + /* + * Overwrite argv[] to point at appropriate space, if needed + */ + +#ifdef PS_USE_CLOBBER_ARGV + { + int i; + + /* make extra argv slots point at end_of_area (a NUL) */ + for (i = 1; i < orig_argc; i++) { + orig_argv[i] = ps_buffer + ps_buffer_size; + } + } + + /* pad unused memory */ + memset(ps_buffer + ps_buffer_cur_len, PS_PADDING, + ps_buffer_size - ps_buffer_cur_len); +#endif /* PS_USE_CLOBBER_ARGV */ + +#ifdef PS_USE_CHANGE_ARGV + orig_argv[0] = ps_buffer; + orig_argv[1] = NULL; +#endif /* PS_USE_CHANGE_ARGV */ + +#ifdef PS_USE_PS_STRINGS + PS_STRINGS->ps_nargvstr = 1; + PS_STRINGS->ps_argvstr = ps_buffer; +#endif /* PS_USE_PS_STRINGS */ + +#ifdef PS_USE_PSTAT + { + union pstun pst; + + pst.pst_command = ps_buffer; + pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0); + } +#endif /* PS_USE_PSTAT */ +} diff --git a/lib/replace/wscript b/lib/replace/wscript index e178cca..bdc4c20 100644 --- a/lib/replace/wscript +++ b/lib/replace/wscript @@ -182,6 +182,8 @@ def configure(conf): checklibc=True) if not conf.CHECK_FUNCS('getpeereid'): conf.CHECK_FUNCS_IN('getpeereid', 'bsd', headers='sys/types.h bsd/unistd.h') + if not conf.CHECK_FUNCS('setproctitle'): + conf.CHECK_FUNCS_IN('setproctitle', 'bsd', headers='bsd/unistd.h') conf.CHECK_CODE(''' struct ucred cred; @@ -460,7 +462,7 @@ REPLACEMENT_FUNCTIONS = { 'utime', 'utimes', 'dup2', 'chown', 'link', 'readlink', 'symlink', 'lchown', 'realpath', 'memmem', 'vdprintf', 'dprintf', 'get_current_dir_name', - 'strerror_r', 'clock_gettime'], + 'strerror_r', 'clock_gettime', 'setproctitle'], 'timegm.c': ['timegm'], # Note: C99_VSNPRINTF is not a function, but a special condition # for replacement @@ -511,6 +513,7 @@ def build(bld): if not bld.CONFIG_SET('HAVE_INET_PTON'): REPLACE_SOURCE += ' inet_pton.c' if not bld.CONFIG_SET('HAVE_GETXATTR') or bld.CONFIG_SET('XATTR_ADDITIONAL_OPTIONS'): REPLACE_SOURCE += ' xattr.c' + if not bld.CONFIG_SET('HAVE_SETPROCTITLE'): REPLACE_SOURCE += ' setproctitle.c' bld.SAMBA_LIBRARY('replace', source=REPLACE_SOURCE, diff --git a/source4/smbd/process_standard.c b/source4/smbd/process_standard.c index dd5f958..f798000 100644 --- a/source4/smbd/process_standard.c +++ b/source4/smbd/process_standard.c @@ -28,19 +28,7 @@ #include "cluster/cluster.h" #include "param/param.h" #include "ldb_wrap.h" - -#ifdef HAVE_SETPROCTITLE -#ifdef HAVE_SETPROCTITLE_H -#include -#endif -#else -#define setproctitle none_setproctitle -static int none_setproctitle(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2); -static int none_setproctitle(const char *fmt, ...) -{ - return 0; -} -#endif +#include "lib/replace/replace.h" NTSTATUS process_model_standard_init(void); diff --git a/source4/smbd/server.c b/source4/smbd/server.c index b3d8ae5..83f2f53 100644 --- a/source4/smbd/server.c +++ b/source4/smbd/server.c @@ -43,6 +43,7 @@ #include "cluster/cluster.h" #include "dynconfig/dynconfig.h" #include "lib/util/samba_modules.h" +#include "lib/replace/replace.h" /* recursively delete a directory tree @@ -492,7 +493,20 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[ return 0; } -int main(int argc, const char *argv[]) +int main(int argc, char *argv[]) { + /* + * Remember the physical location of the initially given argv[] array for + * possible use by ps display. On some platforms, the argv[] storage must + * be overwritten in order to set the process title for ps. In such cases + * setproctitle_init makes and returns a new copy of the argv[] array. + * + * setproctitle_init may also move the environment strings to make + * extra room. Therefore this should be done as early as possible during + * startup, to avoid entanglements with code that might save a getenv() + * result pointer. + */ + argv = setproctitle_init(argc, argv); + return binary_smbd_main("samba", argc, argv); } diff --git a/source4/smbd/wscript_build b/source4/smbd/wscript_build index bfa1312..cad217d 100644 --- a/source4/smbd/wscript_build +++ b/source4/smbd/wscript_build @@ -41,7 +41,7 @@ bld.SAMBA_MODULE('process_model_standard', source='process_standard.c', subsystem='process_model', init_function='process_model_standard_init', - deps='events ldbsamba process_model samba-sockets cluster', + deps='events ldbsamba process_model samba-sockets cluster replace', internal_module=False ) -- 1.7.10.4