Ruby  2.5.0dev(2017-10-22revision60238)
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
39 
40 #ifndef HAVE_SETPROCTITLE
41 
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #ifdef HAVE_UNISTD_H
45 #include <unistd.h>
46 #endif
47 #ifdef HAVE_SYS_PSTAT_H
48 #include <sys/pstat.h>
49 #endif
50 #include <string.h>
51 
52 #if defined(__APPLE__)
53 # ifdef HAVE_CRT_EXTERNS_H
54 # include <crt_externs.h>
55 # undef environ
56 # define environ (*_NSGetEnviron())
57 # else
58 # include "crt_externs.h"
59 # endif
60 #endif
61 
62 #define SPT_NONE 0 /* don't use it at all */
63 #define SPT_PSTAT 1 /* use pstat(PSTAT_SETCMD, ...) */
64 #define SPT_REUSEARGV 2 /* cover argv with title information */
65 
66 #ifndef SPT_TYPE
67 # define SPT_TYPE SPT_NONE
68 #endif
69 
70 #ifndef SPT_PADCHAR
71 # define SPT_PADCHAR '\0'
72 #endif
73 
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;
79 #endif
80 
81 #endif /* HAVE_SETPROCTITLE */
82 
83 void
85 {
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;
91  int i;
92 
93  /*
94  * NB: This assumes that argv has already been copied out of the
95  * way. This is true for sshd, but may not be true for other
96  * programs. Beware.
97  */
98 
99  if (argc == 0 || argv[0] == NULL)
100  return;
101 
102  /* Fail if we can't allocate room for the new environment */
103  for (i = 0; envp[i] != NULL; i++)
104  ;
105  if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) {
106  environ = envp; /* put it back */
107  return;
108  }
109 
110  /*
111  * Find the last argv string or environment variable within
112  * our process memory area.
113  */
114  for (i = 0; i < argc; i++) {
115  if (lastargv == NULL || lastargv + 1 == argv[i])
116  lastargv = argv[i] + strlen(argv[i]);
117  }
118  lastenvp = lastargv;
119  for (i = 0; envp[i] != NULL; i++) {
120  if (lastenvp + 1 == envp[i])
121  lastenvp = envp[i] + strlen(envp[i]);
122  }
123 
124  /* We keep argv[1], argv[2], etc. at this moment,
125  because the ps command of AIX refers to them. */
126  argv1_addr = &argv[1];
127  argv_start = argv[0];
128  argv_len = lastargv - argv[0];
129  argv_env_len = lastenvp - argv[0];
130 
131  for (i = 0; envp[i] != NULL; i++)
132  environ[i] = ruby_strdup(envp[i]);
133  environ[i] = NULL;
134 #endif /* SPT_REUSEARGV */
135 }
136 
137 #ifndef HAVE_SETPROCTITLE
138 void
139 setproctitle(const char *fmt, ...)
140 {
141 #if SPT_TYPE != SPT_NONE
142  va_list ap;
143  char ptitle[1024];
144  size_t len;
145  size_t argvlen;
146 #if SPT_TYPE == SPT_PSTAT
147  union pstun pst;
148 #endif
149 
150 #if SPT_TYPE == SPT_REUSEARGV
151  if (argv_env_len <= 0)
152  return;
153 #endif
154 
155  va_start(ap, fmt);
156  if (fmt != NULL) {
157  vsnprintf(ptitle, sizeof(ptitle) , fmt, ap);
158  }
159  va_end(ap);
160 
161 #if SPT_TYPE == SPT_PSTAT
162  pst.pst_command = ptitle;
163  pstat(PSTAT_SETCMD, pst, strlen(ptitle), 0, 0);
164 #elif SPT_TYPE == SPT_REUSEARGV
165  len = strlcpy(argv_start, ptitle, argv_env_len);
166  argvlen = len > argv_len ? argv_env_len : argv_len;
167  for(; len < argvlen; len++)
168  argv_start[len] = SPT_PADCHAR;
169  /* argv[1], argv[2], etc. are no longer valid. */
170  *argv1_addr = NULL;
171 #endif
172 
173 #endif /* SPT_NONE */
174 }
175 
176 #endif /* HAVE_SETPROCTITLE */
size_t strlen(const char *)
RUBY_FUNC_EXPORTED void ruby_init_setproctitle(int argc, char *argv[])
char ** environ
char * ruby_strdup(const char *)
Definition: util.c:496
#define calloc
Definition: ripper.c:360
int argc
Definition: ruby.c:187
#define RUBY_FUNC_EXPORTED
Definition: defines.h:263
#define compat_init_setproctitle
Definition: setproctitle.c:37
RUBY_EXTERN size_t strlcpy(char *, const char *, size_t)
Definition: strlcpy.c:29
void setproctitle(const char *fmt,...)
Definition: setproctitle.c:139
register unsigned int len
Definition: zonetab.h:51
#define SPT_PADCHAR
Definition: setproctitle.c:71
#define vsnprintf
Definition: subst.h:7
#define NULL
Definition: _sdbm.c:102
char ** argv
Definition: ruby.c:188