Ruby  2.7.1p83(2020-03-31revisiona0c7c23c9cec0d0ffcba012279cd652d28ad5bf3)
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  /* fmt must be non-NULL */
156  va_start(ap, fmt);
157  vsnprintf(ptitle, sizeof(ptitle), fmt, ap);
158  va_end(ap);
159 
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;
168  /* argv[1], argv[2], etc. are no longer valid. */
169  *argv1_addr = NULL;
170 #endif
171 
172 #endif /* SPT_NONE */
173 }
174 
175 #endif /* HAVE_SETPROCTITLE */
i
uint32_t i
Definition: rb_mjit_min_header-2.7.1.h:5425
fmt
const VALUE int int int int int int VALUE char * fmt
Definition: rb_mjit_min_header-2.7.1.h:6423
NULL
#define NULL
Definition: _sdbm.c:101
compat_init_setproctitle
#define compat_init_setproctitle
Definition: setproctitle.c:37
strlen
size_t strlen(const char *)
SPT_PADCHAR
#define SPT_PADCHAR
Definition: setproctitle.c:71
RUBY_FUNC_EXPORTED
#define RUBY_FUNC_EXPORTED
Definition: defines.h:391
va_start
#define va_start(v, l)
Definition: rb_mjit_min_header-2.7.1.h:3946
ruby.h
va_list
__gnuc_va_list va_list
Definition: rb_mjit_min_header-2.7.1.h:831
strlcpy
RUBY_EXTERN size_t strlcpy(char *, const char *, size_t)
Definition: strlcpy.c:29
vsnprintf
int int vsnprintf(char *__restrict, size_t, const char *__restrict, __gnuc_va_list) __attribute__((__format__(__printf__
crt_externs.h
ruby_init_setproctitle
RUBY_FUNC_EXPORTED void ruby_init_setproctitle(int argc, char *argv[])
argv
char ** argv
Definition: ruby.c:223
argc
int argc
Definition: ruby.c:222
ruby_strdup
char * ruby_strdup(const char *)
Definition: util.c:527
environ
#define environ
Definition: crt_externs.h:6
setproctitle
void setproctitle(const char *fmt,...)
Definition: setproctitle.c:139
len
uint8_t len
Definition: escape.c:17
util.h
calloc
void * calloc(size_t, size_t) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__(1
va_end
#define va_end(v)
Definition: rb_mjit_min_header-2.7.1.h:3947