37 #define compat_init_setproctitle ruby_init_setproctitle
38 RUBY_FUNC_EXPORTED
void ruby_init_setproctitle(
int argc,
char *argv[]);
40 #ifndef HAVE_SETPROCTITLE
47 #ifdef HAVE_SYS_PSTAT_H
48 #include <sys/pstat.h>
52 #if defined(__APPLE__)
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
75 static char *argv_start = NULL;
76 static size_t argv_env_len = 0;
77 static size_t argv_len = 0;
78 static char **argv1_addr = NULL;
84 compat_init_setproctitle(
int argc,
char *argv[])
86 #if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
87 extern char **environ;
88 char *lastargv = NULL;
89 char *lastenvp = NULL;
90 char **envp = environ;
99 if (argc == 0 || argv[0] == NULL)
103 for (i = 0; envp[i] != NULL; i++)
105 if ((environ = calloc(i + 1,
sizeof(*environ))) == NULL) {
114 for (i = 0; i < argc; i++) {
115 if (lastargv == NULL || lastargv + 1 == argv[i])
116 lastargv = argv[i] + strlen(argv[i]);
119 for (i = 0; envp[i] != NULL; i++) {
120 if (lastenvp + 1 == envp[i])
121 lastenvp = envp[i] + strlen(envp[i]);
126 argv1_addr = &argv[1];
127 argv_start = argv[0];
128 argv_len = lastargv - argv[0];
129 argv_env_len = lastenvp - argv[0];
131 for (i = 0; envp[i] != NULL; i++)
137 #ifndef HAVE_SETPROCTITLE
139 setproctitle(
const char *fmt, ...)
141 #if SPT_TYPE != SPT_NONE
146 #if SPT_TYPE == SPT_PSTAT
150 #if SPT_TYPE == SPT_REUSEARGV
151 if (argv_env_len <= 0)
157 vsnprintf(ptitle,
sizeof(ptitle), fmt, ap);
160 #if SPT_TYPE == SPT_PSTAT
161 pst.pst_command = ptitle;
162 pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
163 #elif SPT_TYPE == SPT_REUSEARGV
164 len = strlcpy(argv_start, ptitle, argv_env_len);
165 argvlen = len > argv_len ? argv_env_len : argv_len;
166 for(; len < argvlen; len++)
167 argv_start[len] = SPT_PADCHAR;
char * ruby_strdup(const char *str)
This is our own version of strdup(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...