Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

setproctitle.c
Go to the documentation of this file.
1 /* Based on setproctitle.c from openssh-5.6p1 */
2 /* Based on conf.c from UCB sendmail 8.8.8 */
3
4 /*
5 * Copyright 2003 Damien Miller
6 * Copyright (c) 1983, 1995-1997 Eric P. Allman
7 * Copyright (c) 1988, 1993
8 * The Regents of the University of California. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include "ruby.h"
36 #include "ruby/util.h"
37 #define compat_init_setproctitle ruby_init_setproctitle
38
39 #ifndef HAVE_SETPROCTITLE
40
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46 #ifdef HAVE_SYS_PSTAT_H
47 #include <sys/pstat.h>
48 #endif
49 #include <string.h>
50
51 #if defined(__APPLE__)
52 # ifdef HAVE_CRT_EXTERNS_H
53 # include <crt_externs.h>
54 # undef environ
55 # define environ (*_NSGetEnviron())
56 # else
57 # include "crt_externs.h"
58 # endif
59 #endif
60
61 #define SPT_NONE 0 /* don't use it at all */
62 #define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
63 #define SPT_REUSEARGV 2 /* cover argv with title information */
64
65 #ifndef SPT_TYPE
66 # define SPT_TYPE SPT_NONE
67 #endif
68
69 #ifndef SPT_PADCHAR
70 # define SPT_PADCHAR '\0'
71 #endif
72
73 #if SPT_TYPE == SPT_REUSEARGV
74 static char *argv_start = NULL;
75 static size_t argv_env_len = 0;
76 static size_t argv_len = 0;
77 #endif
78
79 #endif /* HAVE_SETPROCTITLE */
80
81 void
82 compat_init_setproctitle(int argc, char *argv[])
83 {
84 #if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV
85 extern char **environ;
86 char *lastargv = NULL;
87 char *lastenvp = NULL;
88 char **envp = environ;
89 int i;
90
91 /*
92 * NB: This assumes that argv has already been copied out of the
93 * way. This is true for sshd, but may not be true for other
94 * programs. Beware.
95 */
96
97 if (argc == 0 || argv[0] == NULL)
98 return;
99
100 /* Fail if we can't allocate room for the new environment */
101 for (i = 0; envp[i] != NULL; i++)
102 ;
103 if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
104 environ = envp; /* put it back */
105 return;
106 }
107
108 /*
109 * Find the last argv string or environment variable within
110 * our process memory area.
111 */
112 for (i = 0; i < argc; i++) {
113 if (lastargv == NULL || lastargv + 1 == argv[i])
114 lastargv = argv[i] + strlen(argv[i]);
115 }
116 lastenvp = lastargv;
117 for (i = 0; envp[i] != NULL; i++) {
118 if (lastenvp + 1 == envp[i])
119 lastenvp = envp[i] + strlen(envp[i]);
120 }
121
122 argv[1] = NULL;
123 argv_start = argv[0];
124 argv_len = lastargv - argv[0];
125 argv_env_len = lastenvp - argv[0];
126
127 for (i = 0; envp[i] != NULL; i++)
128 environ[i] = ruby_strdup(envp[i]);
129 environ[i] = NULL;
130 #endif /* SPT_REUSEARGV */
131 }
132
133 #ifndef HAVE_SETPROCTITLE
134 void
135 setproctitle(const char *fmt, ...)
136 {
137 #if SPT_TYPE != SPT_NONE
138 va_list ap;
139 char ptitle[1024];
140 size_t len;
141 size_t argvlen;
142 #if SPT_TYPE == SPT_PSTAT
143 union pstun pst;
144 #endif
145
146 #if SPT_TYPE == SPT_REUSEARGV
147 if (argv_env_len <= 0)
148 return;
149 #endif
150
151 va_start(ap, fmt);
152 if (fmt != NULL) {
153 vsnprintf(ptitle, sizeof(ptitle) , fmt, ap);
154 }
155 va_end(ap);
156
157 #if SPT_TYPE == SPT_PSTAT
158 pst.pst_command = ptitle;
159 pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
160 #elif SPT_TYPE == SPT_REUSEARGV
161 len = strlcpy(argv_start, ptitle, argv_env_len);
162 argvlen = len > argv_len ? argv_env_len : argv_len;
163 for(; len < argvlen; len++)
164 argv_start[len] = SPT_PADCHAR;
165 #endif
166
167 #endif /* SPT_NONE */
168 }
169
170 #endif /* HAVE_SETPROCTITLE */
171