setproctitle.c 6.2 KB
Newer Older
1 2
/*
 * Copyright © 2010 William Ahern
3
 * Copyright © 2012-2013 Guillem Jover <guillem@hadrons.org>
4 5 6 7 8 9 10 11
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to permit
 * persons to whom the Software is furnished to do so, subject to the
 * following conditions:
12
 *
13 14
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
15
 *
16 17 18 19 20 21 22 23 24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

25 26 27 28 29 30
#include <errno.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
31
#include <err.h>
32 33
#include <unistd.h>
#include <string.h>
34 35

static struct {
36
	/* Original value. */
37 38
	const char *arg0;

39
	/* Title space available. */
40 41
	char *base, *end;

42
	 /* Pointer to original nul character within base. */
43 44
	char *nul;

45
	bool warned;
46
	bool reset;
47 48 49 50
	int error;
} SPT;


51 52 53 54 55
static inline size_t
spt_min(size_t a, size_t b)
{
	return a < b ? a : b;
}
56 57 58

/*
 * For discussion on the portability of the various methods, see
59
 * https://lists.freebsd.org/pipermail/freebsd-stable/2008-June/043136.html
60
 */
61 62 63 64 65
static int
spt_clearenv(void)
{
#ifdef HAVE_CLEARENV
	return clearenv();
66 67 68
#else
	char **tmp;

69 70
	tmp = malloc(sizeof(*tmp));
	if (tmp == NULL)
71 72
		return errno;

73
	tmp[0] = NULL;
74 75 76 77
	environ = tmp;

	return 0;
#endif
78
}
79

80
static int
81
spt_copyenv(int envc, char *envp[])
82
{
83
	char **envcopy;
84
	char *eq;
85
	int envsize;
86 87
	int i, error;

88
	if (environ != envp)
89 90
		return 0;

91 92 93 94 95 96 97 98 99 100
	/* Make a copy of the old environ array of pointers, in case
	 * clearenv() or setenv() is implemented to free the internal
	 * environ array, because we will need to access the old environ
	 * contents to make the new copy. */
	envsize = (envc + 1) * sizeof(char *);
	envcopy = malloc(envsize);
	if (envcopy == NULL)
		return errno;
	memcpy(envcopy, envp, envsize);

101 102
	error = spt_clearenv();
	if (error) {
103 104
		environ = envp;
		free(envcopy);
105 106
		return error;
	}
107

108 109
	for (i = 0; envcopy[i]; i++) {
		eq = strchr(envcopy[i], '=');
110
		if (eq == NULL)
111 112 113
			continue;

		*eq = '\0';
114
		if (setenv(envcopy[i], eq + 1, 1) < 0)
115
			error = errno;
116 117
		*eq = '=';

118
		if (error) {
119 120 121 122 123 124 125 126
#ifdef HAVE_CLEARENV
			/* Because the old environ might not be available
			 * anymore we will make do with the shallow copy. */
			environ = envcopy;
#else
			environ = envp;
			free(envcopy);
#endif
127 128
			return error;
		}
129 130
	}

131 132 133 134
	/* Dispose of the shallow copy, now that we've finished transfering
	 * the old environment. */
	free(envcopy);

135
	return 0;
136
}
137

138 139 140
static int
spt_copyargs(int argc, char *argv[])
{
141 142 143 144
	char *tmp;
	int i;

	for (i = 1; i < argc || (i >= argc && argv[i]); i++) {
145
		if (argv[i] == NULL)
146 147
			continue;

148 149
		tmp = strdup(argv[i]);
		if (tmp == NULL)
150 151 152 153 154 155
			return errno;

		argv[i] = tmp;
	}

	return 0;
156
}
157

158 159
void
setproctitle_init(int argc, char *argv[], char *envp[])
160
{
161
	char *base, *end, *nul, *tmp;
162
	int i, envc, error;
163

164 165 166 167
	/* Try to make sure we got called with main() arguments. */
	if (argc < 0)
		return;

168 169
	base = argv[0];
	if (base == NULL)
170 171 172 173 174 175
		return;

	nul = &base[strlen(base)];
	end = nul + 1;

	for (i = 0; i < argc || (i >= argc && argv[i]); i++) {
176
		if (argv[i] == NULL || argv[i] != end)
177 178 179 180 181 182
			continue;

		end = argv[i] + strlen(argv[i]) + 1;
	}

	for (i = 0; envp[i]; i++) {
183
		if (envp[i] != end)
184 185 186 187
			continue;

		end = envp[i] + strlen(envp[i]) + 1;
	}
188
	envc = i;
189

190 191 192 193 194
	SPT.arg0 = strdup(argv[0]);
	if (SPT.arg0 == NULL) {
		SPT.error = errno;
		return;
	}
195

196 197 198 199 200
	tmp = strdup(getprogname());
	if (tmp == NULL) {
		SPT.error = errno;
		return;
	}
201 202
	setprogname(tmp);

203
	error = spt_copyenv(envc, envp);
204 205 206 207
	if (error) {
		SPT.error = error;
		return;
	}
208

209 210 211 212 213
	error = spt_copyargs(argc, argv);
	if (error) {
		SPT.error = error;
		return;
	}
214 215 216 217

	SPT.nul  = nul;
	SPT.base = base;
	SPT.end  = end;
218
}
219 220 221 222 223

#ifndef SPT_MAXTITLE
#define SPT_MAXTITLE 255
#endif

224
void
225
setproctitle_impl(const char *fmt, ...)
226 227 228
{
	/* Use buffer in case argv[0] is passed. */
	char buf[SPT_MAXTITLE + 1];
229 230
	va_list ap;
	char *nul;
231
	int len;
232

233 234 235 236 237 238
	if (SPT.base == NULL) {
		if (!SPT.warned) {
			warnx("setproctitle not initialized, please either call "
			      "setproctitle_init() or link against libbsd-ctor.");
			SPT.warned = true;
		}
239
		return;
240
	}
241 242

	if (fmt) {
243 244 245 246 247 248 249 250 251 252
		if (fmt[0] == '-') {
			/* Skip program name prefix. */
			fmt++;
			len = 0;
		} else {
			/* Print program name heading for grep. */
			snprintf(buf, sizeof(buf), "%s: ", getprogname());
			len = strlen(buf);
		}

253
		va_start(ap, fmt);
254
		len += vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
255 256
		va_end(ap);
	} else {
257
		len = snprintf(buf, sizeof(buf), "%s", SPT.arg0);
258 259
	}

260 261 262 263
	if (len <= 0) {
		SPT.error = errno;
		return;
	}
264 265 266

	if (!SPT.reset) {
		memset(SPT.base, 0, SPT.end - SPT.base);
267
		SPT.reset = true;
268
	} else {
269
		memset(SPT.base, 0, spt_min(sizeof(buf), SPT.end - SPT.base));
270 271
	}

272
	len = spt_min(len, spt_min(sizeof(buf), SPT.end - SPT.base) - 1);
273 274 275 276 277 278 279 280 281
	memcpy(SPT.base, buf, len);
	nul = &SPT.base[len];

	if (nul < SPT.nul) {
		*SPT.nul = '.';
	} else if (nul == SPT.nul && &nul[1] < SPT.end) {
		*SPT.nul = ' ';
		*++nul = '\0';
	}
282
}
283 284 285 286 287 288
__asm__(".symver setproctitle_impl,setproctitle@@LIBBSD_0.5");

/* The original function introduced in 0.2 was a stub, it only got implemented
 * in 0.5, make the implementation available in the old version as an alias
 * for code linking against that version, and change the default to use the
 * new version, so that new code depends on the implemented version. */
289
#ifdef HAVE_TYPEOF
290
extern typeof(setproctitle_impl) setproctitle_stub __attribute__((alias("setproctitle_impl")));
291 292 293
#else
void setproctitle_stub(const char *fmt, ...)
	__attribute__((alias("setproctitle_impl")));
294
#endif
295
__asm__(".symver setproctitle_stub,setproctitle@LIBBSD_0.2");