KInit
proctitle.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "proctitle.h"
00023 #include <config.h>
00024 #include <config-kdeinit.h>
00025
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <stdarg.h>
00029 #include <stdlib.h>
00030
00031 #define PF_ARGV_NONE 0
00032 #define PF_ARGV_NEW 1
00033 #define PF_ARGV_WRITEABLE 2
00034 #define PF_ARGV_PSTAT 3
00035 #define PF_ARGV_PSSTRINGS 4
00036
00037 #ifdef HAVE_SETPROCTITLE
00038 # define PF_ARGV_TYPE PF_ARGV_NONE
00039 # ifdef HAVE_SYS_TYPES_H
00040 # include <sys/types.h>
00041 # endif
00042 # ifdef HAVE_UNISTD_H
00043 # include <unistd.h>
00044 # endif
00045
00046 #else
00047 # ifdef __GNU_HURD__
00048 # define PF_ARGV_TYPE PF_ARGV_NEW
00049 # else
00050 # define PF_ARGV_TYPE PF_ARGV_WRITEABLE
00051
00052 # if defined(HAVE_SYS_PSTAT_H) && defined(HAVE_PSTAT)
00053 # include <sys/pstat.h>
00054 # undef PF_ARGV_TYPE
00055 # define PF_ARGV_TYPE PF_ARGV_PSTAT
00056 # endif
00057
00058 # ifdef HAVE_SYS_EXEC_H
00059 # include <sys/exec.h>
00060 # ifdef PS_STRINGS
00061 # include <machine/vmparam.h>
00062 # undef PF_ARGV_TYPE
00063 # define PF_ARGV_TYPE PF_ARGV_PSSTRINGS
00064 # endif
00065 # endif
00066
00067 # endif
00068
00069 #endif
00070
00071 #ifdef HAVE___PROGNAME
00072 extern char *__progname;
00073 #endif
00074 #ifdef HAVE___PROGNAME_FULL
00075 extern char *__progname_full;
00076 #endif
00077 extern char **environ;
00078
00079 static char **Argv = NULL;
00080 static char *LastArgv = NULL;
00081
00085 void proctitle_init(int argc, char *argv[], char *envp[]) {
00086 register int i, envpsize;
00087 char **p;
00088
00089
00090 for ( i = envpsize = 0; envp[i] != NULL; i++ ) {
00091 envpsize += strlen(envp[i]) + 1;
00092 }
00093
00094 if ((p = (char **) malloc((i + 1) * sizeof(char *))) != NULL) {
00095 environ = p;
00096
00097 for (i = 0; envp[i] != NULL; i++) {
00098 if ((environ[i] = static_cast<char *>(malloc(strlen(envp[i]) + 1))) != NULL) {
00099 strcpy(environ[i], envp[i]);
00100 }
00101 }
00102
00103 environ[i] = NULL;
00104 }
00105
00106 Argv = argv;
00107
00108 for (i = 0; i < argc; i++) {
00109 if (!i || (LastArgv + 1 == argv[i])) {
00110 LastArgv = argv[i] + strlen(argv[i]);
00111 }
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 #ifndef __linux__
00123 for (i = 0; envp[i] != NULL; i++) {
00124 if ((LastArgv + 1) == envp[i]) {
00125 LastArgv = envp[i] + strlen(envp[i]);
00126 }
00127 }
00128 #endif
00129
00130 # ifdef HAVE___PROGNAME
00131
00132
00133
00134 __progname = strdup("kdeinit4");
00135 # endif
00136 # ifdef HAVE____PROGNAME_FULL
00137
00138 __progname_full = strdup(argv[0]);
00139 # endif
00140 }
00141
00142 void proctitle_set(const char *fmt, ...) {
00143 va_list msg;
00144 static char statbuf[BUFSIZ];
00145
00146 #ifndef HAVE_SETPROCTITLE
00147 # if PF_ARGV_TYPE == PF_ARGV_PSTAT
00148 union pstun pst;
00149 # endif
00150 char *p;
00151 int i, maxlen = (LastArgv - Argv[0]) - 2;
00152 #endif
00153
00154 if ( !fmt ) {
00155 return;
00156 }
00157
00158 va_start(msg, fmt);
00159
00160 memset(statbuf, 0, sizeof(statbuf));
00161
00162 #ifdef HAVE_SETPROCTITLE
00163 # if __FreeBSD__ >= 4 && !defined(FREEBSD4_0) && !defined(FREEBSD4_1)
00164
00165 vsnprintf(statbuf, sizeof(statbuf), fmt, msg);
00166
00167 # else
00168
00169 snprintf(statbuf, sizeof(statbuf), "%s", "kdeinit4: ");
00170 vsnprintf(statbuf + strlen(statbuf),
00171 sizeof(statbuf) - strlen(statbuf),
00172 fmt,
00173 msg);
00174
00175 # endif
00176 setproctitle("%s", statbuf);
00177
00178 #else
00179
00180 snprintf(statbuf, sizeof(statbuf), "%s", "kdeinit4: ");
00181 vsnprintf(statbuf + strlen(statbuf),
00182 sizeof(statbuf) - strlen(statbuf),
00183 fmt,
00184 msg);
00185
00186 #endif
00187
00188 va_end(msg);
00189
00190 #ifdef HAVE_SETPROCTITLE
00191 return;
00192 #else
00193 i = strlen(statbuf);
00194
00195 # if PF_ARGV_TYPE == PF_ARGV_NEW
00196
00197 Argv[0] = statbuf;
00198 Argv[1] = NULL;
00199 # endif
00200
00201 # if PF_ARGV_TYPE == PF_ARGV_WRITEABLE
00202
00203 snprintf(Argv[0], maxlen, "%s", statbuf);
00204 p = &Argv[0][i];
00205
00206 while ( p < LastArgv ) {
00207 *p++ = '\0';
00208 }
00209 Argv[1] = NULL;
00210 # endif
00211
00212 # if PF_ARGV_TYPE == PF_ARGV_PSTAT
00213 pst.pst_command = statbuf;
00214 pstat(PSTAT_SETCMD, pst, i, 0, 0);
00215 # endif
00216
00217 # if PF_ARGV_TYPE == PF_ARGV_PSSTRINGS
00218 PS_STRINGS->ps_nargvstr = 1;
00219 PS_STRINGS->ps_argvstr = statbuf;
00220 # endif
00221
00222 #endif
00223 }