• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeinit
 

tdeinit

  • tdeinit
setproctitle.cpp
1 /*
2  * Copyright (c) 1998 Sendmail, Inc. All rights reserved.
3  * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
4  * Copyright (c) 1988, 1993
5  * The Regents of the University of California. All rights reserved.
6  *
7  * By using this file, you agree to the terms and conditions set
8  * forth in the LICENSE file which can be found at the top level of
9  * the sendmail distribution.
10  *
11  * A copy of the above mentioned LICENSE file can be found in
12  * LICENSE.setproctitle.
13  *
14  * Ported for use with KDE by Waldo Bastian <bastian@kde.org>
15  */
16 
17 #include "setproctitle.h"
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 /* _GNU_SOURCE might already be defined in <config.h> */
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE
26 #endif
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <sys/ioctl.h>
30 #include <sys/param.h>
31 #include <limits.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 /* _PATH_KMEM should be defined in <paths.h> */
37 #ifndef _PATH_KMEM
38 # define _PATH_KMEM "/dev/kmem"
39 #endif
40 
41 #define SPACELEFT(buf, ptr) (sizeof buf - ((ptr) - buf))
42 
43 
44 /*
45 ** SETPROCTITLE -- set process title for ps
46 **
47 ** Parameters:
48 ** fmt -- a printf style format string.
49 ** a, b, c -- possible parameters to fmt.
50 **
51 ** Returns:
52 ** none.
53 **
54 ** Side Effects:
55 ** Clobbers argv of our main procedure so ps(1) will
56 ** display the title.
57 */
58 
59 #define SPT_NONE 0 /* don't use it at all */
60 #define SPT_REUSEARGV 1 /* cover argv with title information */
61 #define SPT_BUILTIN 2 /* use libc builtin */
62 #define SPT_PSTAT 3 /* use pstat(PSTAT_SETCMD, ...) */
63 #define SPT_PSSTRINGS 4 /* use PS_STRINGS->... */
64 #define SPT_SYSMIPS 5 /* use sysmips() supported by NEWS-OS 6 */
65 #define SPT_SCO 6 /* write kernel u. area */
66 #define SPT_CHANGEARGV 7 /* write our own strings into argv[] */
67 
68 #ifndef SPT_TYPE
69 # define SPT_TYPE SPT_REUSEARGV
70 #endif
71 
72 #if SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN
73 
74 # if SPT_TYPE == SPT_PSTAT
75 # include <sys/pstat.h>
76 # endif
77 # if SPT_TYPE == SPT_PSSTRINGS
78 # include <machine/vmparam.h>
79 # ifdef HAVE_SYS_EXEC_H
80 # include <sys/exec.h>
81 # endif
82 # ifndef PS_STRINGS /* hmmmm.... apparently not available after all */
83 # undef SPT_TYPE
84 # define SPT_TYPE SPT_REUSEARGV
85 # else
86 # ifndef NKPDE /* FreeBSD 2.0 */
87 # define NKPDE 63
88 typedef unsigned int *pt_entry_t;
89 # endif
90 # endif
91 # endif
92 
93 # if SPT_TYPE == SPT_PSSTRINGS || SPT_TYPE == SPT_CHANGEARGV
94 # define SETPROC_STATIC static
95 # else
96 # define SETPROC_STATIC
97 # endif
98 
99 # if SPT_TYPE == SPT_SYSMIPS
100 # include <sys/sysmips.h>
101 # include <sys/sysnews.h>
102 # endif
103 
104 # if SPT_TYPE == SPT_SCO
105 # include <sys/immu.h>
106 # include <sys/dir.h>
107 # include <sys/user.h>
108 # include <sys/fs/s5param.h>
109 # if PSARGSZ > MAXLINE
110 # define SPT_BUFSIZE PSARGSZ
111 # endif
112 # endif
113 
114 # ifndef SPT_PADCHAR
115 # ifdef _AIX
116 # define SPT_PADCHAR '\0'
117 # else
118 # define SPT_PADCHAR ' '
119 # endif
120 # endif
121 
122 #endif /* SPT_TYPE != SPT_NONE && SPT_TYPE != SPT_BUILTIN */
123 
124 # ifndef SPT_BUFSIZE
125 # define SPT_BUFSIZE 2048
126 # endif
127 
128 /*
129 ** Pointers for setproctitle.
130 ** This allows "ps" listings to give more useful information.
131 */
132 
133 char **Argv = NULL; /* pointer to argument vector */
134 char *LastArgv = NULL; /* end of argv */
135 
136 void
137 tdeinit_initsetproctitle(int argc, char **argv, char **envp)
138 {
139  int i, envpsize = 0;
140 #if !defined(HAVE_NSGETENVIRON) || !defined(HAVE_CRT_EXTERNS_H)
141  extern char **environ;
142 #endif
143 
144  /*
145  ** Move the environment so setproctitle can use the space at
146  ** the top of memory.
147  */
148 
149  for (i = 0; envp[i] != NULL; i++)
150  envpsize += strlen(envp[i]) + 1;
151  environ = (char **) malloc(sizeof (char *) * (i + 1));
152  if (environ == NULL)
153  return;
154 
155  for (i = 0; envp[i] != NULL; i++)
156  environ[i] = strdup(envp[i]);
157  environ[i] = NULL;
158 
159  /*
160  ** Save start and extent of argv for setproctitle.
161  */
162 
163  Argv = argv;
164 
165  /*
166  ** Determine how much space we can use for setproctitle.
167  ** Use all contiguous argv and envp pointers starting at argv[0]
168  */
169  for (i = 0; i < argc; i++)
170  {
171  if (i==0 || LastArgv + 1 == argv[i])
172  LastArgv = argv[i] + strlen(argv[i]);
173  else
174  continue;
175  }
176 
177  /*
178  * On linux, we don't want to reuse the memory allocated for
179  * the environment, as there are tools that try to read our environment
180  * variables while we're running (ConsoleKit does that).
181  * There is no way to move or resize it, so just not touchint it
182  * seems to be the only option
183  */
184 #ifndef __linux__
185  for (i=0; envp[i] != NULL; i++)
186  {
187  if (LastArgv + 1 == envp[i])
188  LastArgv = envp[i] + strlen(envp[i]);
189  else
190  continue;
191  }
192 #endif
193 }
194 
195 #if SPT_TYPE != SPT_BUILTIN
196 
197 /*VARARGS1*/
198 static void
199 setproctitle(const char *fmt, ...)
200 {
201 # if SPT_TYPE != SPT_NONE
202  char *p;
203  int i;
204  SETPROC_STATIC char buf[SPT_BUFSIZE];
205  va_list ap;
206 # if SPT_TYPE == SPT_PSTAT
207  union pstun pst;
208 # endif
209 # if SPT_TYPE == SPT_SCO
210  off_t seek_off;
211  static int kmem = -1;
212 #warning (rikkus) kmempid is declared as int, should be long
213  static int kmempid = -1;
214  struct user u;
215 # endif
216 
217  p = buf;
218 
219  /* print the argument string */
220  va_start(ap, fmt);
221  (void) vsnprintf(p, SPACELEFT(buf, p), fmt, ap);
222  va_end(ap);
223 
224  i = strlen(buf);
225 
226 # if SPT_TYPE == SPT_PSTAT
227  pst.pst_command = buf;
228  pstat(PSTAT_SETCMD, pst, i, 0, 0);
229 # endif
230 # if SPT_TYPE == SPT_PSSTRINGS
231  PS_STRINGS->ps_nargvstr = 1;
232  PS_STRINGS->ps_argvstr = buf;
233 # endif
234 # if SPT_TYPE == SPT_SYSMIPS
235  sysmips(SONY_SYSNEWS, NEWS_SETPSARGS, buf);
236 # endif
237 # if SPT_TYPE == SPT_SCO
238  if (kmem < 0 || kmempid != getpid())
239  {
240  if (kmem >= 0)
241  close(kmem);
242  kmem = open(_PATH_KMEM, O_RDWR, 0);
243  if (kmem < 0)
244  return;
245  (void) fcntl(kmem, F_SETFD, 1);
246  kmempid = getpid();
247  }
248  buf[PSARGSZ - 1] = '\0';
249  seek_off = UVUBLK + (off_t) u.u_psargs - (off_t) &u;
250  if (lseek(kmem, (off_t) seek_off, SEEK_SET) == seek_off)
251  (void) write(kmem, buf, PSARGSZ);
252 # endif
253 # if SPT_TYPE == SPT_REUSEARGV
254  if (i > LastArgv - Argv[0] - 2)
255  {
256  i = LastArgv - Argv[0] - 2;
257  buf[i] = '\0';
258  }
259  (void) strcpy(Argv[0], buf);
260  p = &Argv[0][i];
261  while (p < LastArgv)
262  *p++ = SPT_PADCHAR;
263  Argv[1] = NULL;
264 # endif
265 # if SPT_TYPE == SPT_CHANGEARGV
266  Argv[0] = buf;
267  Argv[1] = 0;
268 # endif
269 # endif /* SPT_TYPE != SPT_NONE */
270 }
271 
272 #endif /* SPT_TYPE != SPT_BUILTIN */
273 ␌/*
274 ** SM_SETPROCTITLE -- set process task and set process title for ps
275 **
276 ** Possibly set process status and call setproctitle() to
277 ** change the ps display.
278 **
279 ** Parameters:
280 ** status -- whether or not to store as process status
281 ** fmt -- a printf style format string.
282 ** a, b, c -- possible parameters to fmt.
283 **
284 ** Returns:
285 ** none.
286 */
287 
288 /*VARARGS2*/
289 void
290 tdeinit_setproctitle(const char *fmt, ...)
291 {
292  char buf[SPT_BUFSIZE];
293 
294  va_list ap;
295  /* print the argument string */
296  va_start(ap, fmt);
297  (void) vsnprintf(buf, SPT_BUFSIZE, fmt, ap);
298  va_end(ap);
299 
300  setproctitle("%s", buf);
301 }
302 

tdeinit

Skip menu "tdeinit"
  • Main Page
  • File List
  • Related Pages

tdeinit

Skip menu "tdeinit"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeinit by doxygen 1.9.1
This website is maintained by Timothy Pearson.