/* * Call this early in startup to save the original argc/argv values. * * argv[] will not be overwritten by this routine, but may be overwritten * during setproctitle. 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. */ void init_setproctitle(int argc, char *argv[]) { #if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV char *end_of_area = NULL; char **new_environ; int i; #endif save_argc = argc; save_argv = argv; _init__progname (argv[0]); #if SETPROCTITLE_STRATEGY == 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. */ /* * 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]); } /* probably can't happen? */ if (end_of_area == NULL) { ps_buffer = NULL; ps_buffer_size = 0; return; } /* * 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] - 1; /* * Duplicate and move the environment out of the way */ new_environ = malloc(sizeof(char *) * (i + 1)); for (i = 0; environ[i] != NULL; i++) { new_environ[i] = strdup(environ[i]); //free(environ[i]); } /* if(environ) */ /* free(environ); */ new_environ[i] = NULL; environ = new_environ; #endif /* PS_USE_CLOBBER_ARGV */ }
/* * Call this early in startup to save the original argc/argv values. * * argv[] will not be overwritten by this routine, but may be overwritten * during setproctitle. 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. */ void init_setproctitle(int argc, char *argv[]) { #if SETPROCTITLE_STRATEGY == PS_USE_CLOBBER_ARGV char *end_of_area = NULL; int i; #endif save_argc = argc; save_argv = argv; #if defined(__NetBSD__) || defined(__FreeBSD__) setprogname (argv[0]); #else _init__progname (argv[0]); #endif #if SETPROCTITLE_STRATEGY == 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. */ /* * 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]); } /* probably can't happen? */ if (end_of_area == NULL) { ps_buffer = NULL; ps_buffer_size = 0; return; } /* * 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] - 1; /* * Duplicate and move the environment out of the way */ new_environ = malloc(sizeof(char *) * (i + 1)); if (!new_environ) { fprintf(stderr, "ERROR: [%s:%d] %s: %s\n", __FILE__, __LINE__, "init_setproctitle", strerror(errno)); abort(); } for (i = 0; environ[i] != NULL; i++) { new_environ[i] = strdup(environ[i]); } new_environ[i] = NULL; environ = new_environ; #endif /* PS_USE_CLOBBER_ARGV */ }