File: prctl.c

package info (click to toggle)
prctl 1.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 96 kB
  • ctags: 12
  • sloc: ansic: 268; makefile: 97
file content (375 lines) | stat: -rw-r--r-- 8,140 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * prctl - a tool to set process options
 *
 * Copyright (C) 2000 Hewlett-Packard Co
 * Copyright (C) 2000 Khalid Aziz <khalid_aziz@hp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as 
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <pwd.h>
#include <linux/prctl.h>
#include <string.h>

/* Version */
#define VERSION	"1.4"

/* Shell to fall back on if no other shell can be found */
#define DEFAULT_SHELL	"/bin/bash"

#define PSR_UC		0x08
/* Command line options */

struct option longopts[] = {
	{"unaligned", 1, (int *)0, 'u'},
	{"fpemu", 1, (int *)0, 'f'},
	{"version", 0, (int *)0, 'V'},
	{"help", 0, (int *)0, 'h'},
	{0, 0, (int *)0, 0}
};

/* Verbose mode */
int verbose=0;

print_version(char *progname)
{
	printf("%s version %s\n", progname, VERSION);
}

usage(char *progname)
{
	print_version(progname);
	printf("Usage: %s [-v] [-h|--help] [--version]\n", progname);
	printf("             <-q|<options>> [command]\n");
	printf("   Options:\n");
#ifdef __ia64__
	printf("       --unaligned=[silent|signal|always-signal|default]\n");
#else
	printf("       --unaligned=[silent|signal|default]\n");
#endif
	printf("       --fpemu=[silent|signal|default]\n");
}

int set_unaligned(int prctl_val)
{
	int alignval, retval;
	int umask;

	/*
	 * Check if we need to display the value or set it.
	 */
	if (prctl_val == -1) {
		if ((retval = prctl(PR_GET_UNALIGN, &alignval)) != -1) {
			printf("%-13s= ", "unaligned");
			switch (alignval) {
			case 0:
				printf("default\n");
				break;

			case PR_UNALIGN_NOPRINT:
				printf("silent\n");
				break;

			case PR_UNALIGN_SIGBUS:
#ifdef __ia64__
				/* Get user mask */
				__asm__ __volatile__ ("mov %0=psr.um;;": "=r"(umask) :: "memory");
				if (umask & PSR_UC)
					printf("always-");
#endif
				printf("signal\n");
				break;
			}
		}
	} else {
		if ((retval = prctl(PR_SET_UNALIGN, prctl_val)) != -1) {
			if (verbose) {
				printf("Set \"unaligned\" to ");
				switch (prctl_val) {
				case 0:
					printf("\"default\"\n");
					break;

				case PR_UNALIGN_NOPRINT:
					printf("\"silent\"\n");
					break;

				case PR_UNALIGN_SIGBUS:
					printf("\"signal\"\n");
					break;
				}
			}
		}
	}
	if (retval == -1) {
		fprintf(stderr, "Failed to %s \"unalign\" value: %s\n",
			((prctl_val == -1)?"get":"set"), strerror(errno));
	}
	return(retval);
}

int set_fpemu(int prctl_val)
{
	int fpemuval, retval;

	/*
	 * Check if we need to display the value or set it.
	 */
	if (prctl_val == -1) {
		if ((retval = prctl(PR_GET_FPEMU, &fpemuval)) != -1) {
			printf("%-13s= ", "FP emulation");
			switch (fpemuval) {
			case 0:
				printf("default\n");
				break;

			case PR_FPEMU_NOPRINT:
				printf("silent\n");
				break;

			case PR_FPEMU_SIGFPE:
				printf("signal\n");
				break;
			}
		}
	} else {
		if ((retval = prctl(PR_SET_FPEMU, prctl_val)) != -1) {
			if (verbose) {
				printf("Set \"fpemu\" to ");
				switch (prctl_val) {
				case 0:
					printf("\"default\"\n");
					break;

				case PR_FPEMU_NOPRINT:
					printf("\"silent\"\n");
					break;

				case PR_FPEMU_SIGFPE:
					printf("\"signal\"\n");
					break;
				}
			}
		}
	}

	/*
	 * If this was a PR_GET_FPEMU operation and the error is EINVAL,
	 * this platform does not implement PR_GET_FPEMU (It may be
	 * implemented only on IA64 platforms). Do not complain in that
	 * case.
	 */
	if ((retval == -1) && ((prctl_val != -1) || (errno != EINVAL))) {
		fprintf(stderr, "Failed to %s \"fpemu\" value: %s\n",
			((prctl_val == -1)?"get":"set"), strerror(errno));
		if ((prctl_val != -1) && (errno ==EINVAL)) {
			fprintf(stderr, "   Kernel on this platform may not implement the PR_SET_FPEMU feature.\n");
		}
	}
	return(retval);
}
int main(int argc, char **argv)
{
	int opt, cmd_start;
	char *progname;
	char fullpath[512];
	char shellname[128];
	int unaligned_val=-99;
	int fpemu_val=-99;
	int always_signal=0;
	int display = 0;
	int display_all = 0;
	int umask;

	strcpy(fullpath, argv[0]);
	if ((progname = strrchr(fullpath, '/')) != NULL) {
		progname++;
	} else {
		progname = fullpath;
	}

	/*
	 * Parse command line options
	 */
	if (argc == 1) {
		usage(progname);
		exit(1);
	}
	opterr = 0;
	while ((opt = getopt_long(argc, argv, "+qhv", longopts, 
					(int *) NULL)) != EOF) {
		switch (opt) {
		case 'u':
			if (strcmp(optarg, "silent") == 0) {
				unaligned_val = PR_UNALIGN_NOPRINT;
			} else if (strcmp(optarg, "signal") == 0) {
				unaligned_val = PR_UNALIGN_SIGBUS;
#ifdef __ia64__
			} else if (strcmp(optarg, "always-signal") == 0) {
				unaligned_val = PR_UNALIGN_SIGBUS;
				always_signal = 1;
#endif
			} else if (strcmp(optarg, "default") == 0) {
				unaligned_val = 0;
			} else if (optarg[0] == 0) {
				unaligned_val = -1;
				display = 1;
			} else {
				usage(progname);
				exit(1);
			}
			break;

		case 'f':
			if (strcmp(optarg, "silent") == 0) {
				fpemu_val = PR_FPEMU_NOPRINT;
			} else if (strcmp(optarg, "signal") == 0) {
				fpemu_val = PR_FPEMU_SIGFPE;
			} else if (strcmp(optarg, "default") == 0) {
				fpemu_val = 0;
			} else if (optarg[0] == 0) {
				fpemu_val = -1;
				display = 1;
			} else {
				usage(progname);
				exit(1);
			}
			break;

		case 'q':
			display_all = 1;
			display = 1;
			break;

		case 'h':
			usage(progname);
			exit(0);
			break;

		case 'v':
			verbose = 1;
			break;

		case 'V':
			print_version(progname);
			exit(0);
			break;

		case '?':
			fprintf(stderr, "%s: invalid option - %c\n", 
					progname, optopt);
			exit (1);
			break;
		}
	}

	/*
	 * Is there a command on the command line? If there is, there 
	 * there should be atleast one other option on the line and it
	 * should be somethingother than -v.
	 */
	cmd_start = optind;
	if ((cmd_start == 1) || ((cmd_start == 2) && (verbose))) {
		usage(progname);
		exit(1);
	}

	/*
	 * If -q option was given, we will ignore all other options
	 * and simply display current settings.
	 */
	if (display_all) {
		set_unaligned(-1);
		set_fpemu(-1);
	} else {
		/*
		 * Now set the correct prctl options if needed
		 */
		if (unaligned_val != -99) {
			if (set_unaligned(unaligned_val) == -1) {
				exit(1);
			}
#ifdef __ia64__
			if (always_signal) {
				/* Set alignment check in user mask */
				__asm__ __volatile__ ("sum psr.ac;;"::: "memory");
			}
#endif
		}
		if (fpemu_val != -99) {
			if (set_fpemu(fpemu_val) == -1) {
				exit(1);
			}
		}
	}
	
	/*
	 * Check if we need to run a command or start a new shell.
	 *
	 * If the command line options asked to display any of the values
	 * and a command was not given on the command line, we will not
	 * start a shell.
	 *
	 */
	if (argv[cmd_start] == 0) {
		if (display) {
			exit(0);
		}

		strcpy(shellname, getenv("SHELL"));
		
		/*
		 * Make sure SHELL environment variable is not unset. If it
		 * is, start bash.
		 */
		if (shellname[0] == 0) {
			struct passwd *pwd_entry;

			pwd_entry = getpwuid(getuid());
			if (pwd_entry == NULL) {
				strcpy(shellname, DEFAULT_SHELL);
			} else {
				if (pwd_entry->pw_shell != NULL) {
					strcpy(shellname, pwd_entry->pw_shell);
				} else {
					strcpy(shellname, DEFAULT_SHELL);
				}
			}
		}

		/*
		 * Now exec the shell
		 */
		if (execlp(shellname, (char *)shellname, (char *) 0) == -1) {
			fprintf(stderr, "Failed to exec the shell: %s\n",
				strerror(errno));
			exit(1);
		}
	} else {
		/*
		 * Now exec the user command 
		 */
		if (execvp(argv[cmd_start], argv+cmd_start) == -1) {
			fprintf(stderr, "Failed to exec command \"%s\": %s\n",
				argv[cmd_start], strerror(errno));
			exit(1);
		}
	}
}