37#define compat_init_setproctitle ruby_init_setproctitle
38RUBY_FUNC_EXPORTED
void ruby_init_setproctitle(
int argc,
char *argv[]);
40#ifndef HAVE_SETPROCTITLE
47#ifdef HAVE_SYS_PSTAT_H
53# ifdef HAVE_CRT_EXTERNS_H
54# include <crt_externs.h>
56# define environ (*_NSGetEnviron())
58# include "crt_externs.h"
64#define SPT_REUSEARGV 2
67# define SPT_TYPE SPT_NONE
71# define SPT_PADCHAR '\0'
74#if SPT_TYPE == SPT_REUSEARGV
75static char *argv_start = NULL;
76static size_t argv_env_len = 0;
77static size_t argv_len = 0;
78static char **argv1_addr = NULL;
83#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
84# define ALLOCATE_ENVIRON 1
86# define ALLOCATE_ENVIRON 0
94static char **system_environ = NULL;
105static char **orig_environ = NULL;
113static char **alloc_environ = NULL;
117compat_init_setproctitle(
int argc,
char *argv[])
120 extern char **environ;
121 char *lastargv = NULL;
122 char *lastenvp = NULL;
123 char **envp = environ;
132 if (argc == 0 || argv[0] == NULL)
136 for (i = 0; envp[i] != NULL; i++);
138 system_environ = environ;
140 alloc_environ =
xcalloc(i + 1,
sizeof(*environ));
141 orig_environ = environ =
xcalloc(i + 1,
sizeof(*environ));
142 if (environ == NULL) {
151 for (i = 0; i < argc; i++) {
152 if (lastargv == NULL || lastargv + 1 == argv[i])
153 lastargv = argv[i] + strlen(argv[i]);
156 for (i = 0; envp[i] != NULL; i++) {
157 if (lastenvp + 1 == envp[i])
158 lastenvp = envp[i] + strlen(envp[i]);
163 argv1_addr = &argv[1];
164 argv_start = argv[0];
165 argv_len = lastargv - argv[0];
166 argv_env_len = lastenvp - argv[0];
168 for (i = 0; envp[i] != NULL; i++)
169 alloc_environ[i] = environ[i] =
ruby_strdup(envp[i]);
170 alloc_environ[i] = environ[i] = NULL;
175ruby_free_proctitle(
void)
178 extern char **environ;
180 if (!orig_environ)
return;
182 for (
int i = 0; alloc_environ[i] != NULL; i++) {
183 xfree(alloc_environ[i]);
185 xfree(alloc_environ);
188 environ = system_environ;
192#ifndef HAVE_SETPROCTITLE
195setproctitle(
const char *fmt, ...)
197#if SPT_TYPE != SPT_NONE
202#if SPT_TYPE == SPT_PSTAT
206#if SPT_TYPE == SPT_REUSEARGV
207 if (argv_env_len <= 0)
213 vsnprintf(ptitle,
sizeof(ptitle), fmt, ap);
216#if SPT_TYPE == SPT_PSTAT
217 pst.pst_command = ptitle;
218 pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
219#elif SPT_TYPE == SPT_REUSEARGV
220 len = strlcpy(argv_start, ptitle, argv_env_len);
221 argvlen =
len > argv_len ? argv_env_len : argv_len;
222 for(;
len < argvlen;
len++)
223 argv_start[
len] = SPT_PADCHAR;
#define xfree
Old name of ruby_xfree.
#define xcalloc
Old name of ruby_xcalloc.
int len
Length of the buffer.
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...