blob: 54f2cbb5c29eee55dbbc21e3931b04bdd4417308 [file] [log] [blame]
Rob Landleyd3904932013-07-16 00:04:56 -05001/* xwrap.c - wrappers around existing library functions.
2 *
3 * Functions with the x prefix are wrappers that either succeed or kill the
4 * program with an error message, but never return failure. They usually have
5 * the same arguments and return value as the function they wrap.
6 *
7 * Copyright 2006 Rob Landley <rob@landley.net>
8 */
9
10#include "toys.h"
11
Rob Landley2fb85a32014-12-04 21:41:12 -060012// strcpy and strncat with size checking. Size is the total space in "dest",
13// including null terminator. Exit if there's not enough space for the string
14// (including space for the null terminator), because silently truncating is
15// still broken behavior. (And leaving the string unterminated is INSANE.)
Rob Landleyd3904932013-07-16 00:04:56 -050016void xstrncpy(char *dest, char *src, size_t size)
17{
Rob Landley3704f822013-11-02 14:24:54 -050018 if (strlen(src)+1 > size) error_exit("'%s' > %ld bytes", src, (long)size);
Rob Landleyd3904932013-07-16 00:04:56 -050019 strcpy(dest, src);
20}
21
Rob Landley2fb85a32014-12-04 21:41:12 -060022void xstrncat(char *dest, char *src, size_t size)
23{
24 long len = strlen(src);
25
26 if (len+strlen(dest)+1 > size)
Rob Landley70a84a32015-03-01 15:58:40 -060027 error_exit("'%s%s' > %ld bytes", dest, src, (long)size);
Rob Landley2fb85a32014-12-04 21:41:12 -060028 strcpy(dest+len, src);
29}
30
Rob Landleyd3904932013-07-16 00:04:56 -050031void xexit(void)
32{
Rob Landleyaad492f2015-01-03 16:25:36 -060033 if (fflush(NULL) || ferror(stdout))
34 if (!toys.exitval) perror_msg("write");
Rob Landleyd3904932013-07-16 00:04:56 -050035 if (toys.rebound) longjmp(*toys.rebound, 1);
36 else exit(toys.exitval);
37}
38
39// Die unless we can allocate memory.
40void *xmalloc(size_t size)
41{
42 void *ret = malloc(size);
43 if (!ret) error_exit("xmalloc");
44
45 return ret;
46}
47
48// Die unless we can allocate prezeroed memory.
49void *xzalloc(size_t size)
50{
51 void *ret = xmalloc(size);
52 memset(ret, 0, size);
53 return ret;
54}
55
56// Die unless we can change the size of an existing allocation, possibly
57// moving it. (Notice different arguments from libc function.)
58void *xrealloc(void *ptr, size_t size)
59{
60 ptr = realloc(ptr, size);
61 if (!ptr) error_exit("xrealloc");
62
63 return ptr;
64}
65
66// Die unless we can allocate a copy of this many bytes of string.
67char *xstrndup(char *s, size_t n)
68{
Rob Landley2fb85a32014-12-04 21:41:12 -060069 char *ret = strndup(s, ++n);
70
71 if (!ret) error_exit("xstrndup");
72 ret[--n] = 0;
Rob Landleyd3904932013-07-16 00:04:56 -050073
74 return ret;
75}
76
77// Die unless we can allocate a copy of this string.
78char *xstrdup(char *s)
79{
80 return xstrndup(s, strlen(s));
81}
82
83// Die unless we can allocate enough space to sprintf() into.
Rob Landley59d85e22014-01-16 09:26:50 -060084char *xmprintf(char *format, ...)
Rob Landleyd3904932013-07-16 00:04:56 -050085{
86 va_list va, va2;
87 int len;
88 char *ret;
89
90 va_start(va, format);
91 va_copy(va2, va);
92
93 // How long is it?
94 len = vsnprintf(0, 0, format, va);
95 len++;
96 va_end(va);
97
98 // Allocate and do the sprintf()
99 ret = xmalloc(len);
100 vsnprintf(ret, len, format, va2);
101 va_end(va2);
102
103 return ret;
104}
105
106void xprintf(char *format, ...)
107{
108 va_list va;
109 va_start(va, format);
110
111 vprintf(format, va);
Rob Landley21f3c8d2014-10-20 19:56:05 -0500112 va_end(va);
Rob Landleyddbaa712014-05-26 12:25:47 -0500113 if (fflush(stdout) || ferror(stdout)) perror_exit("write");
Rob Landleyd3904932013-07-16 00:04:56 -0500114}
115
116void xputs(char *s)
117{
Rob Landleyddbaa712014-05-26 12:25:47 -0500118 if (EOF == puts(s) || fflush(stdout) || ferror(stdout)) perror_exit("write");
Rob Landleyd3904932013-07-16 00:04:56 -0500119}
120
121void xputc(char c)
122{
Rob Landleyddbaa712014-05-26 12:25:47 -0500123 if (EOF == fputc(c, stdout) || fflush(stdout) || ferror(stdout))
124 perror_exit("write");
Rob Landleyd3904932013-07-16 00:04:56 -0500125}
126
127void xflush(void)
128{
Rob Landleyddbaa712014-05-26 12:25:47 -0500129 if (fflush(stdout) || ferror(stdout)) perror_exit("write");;
Rob Landleyd3904932013-07-16 00:04:56 -0500130}
131
132// Die unless we can exec argv[] (or run builtin command). Note that anything
133// with a path isn't a builtin, so /bin/sh won't match the builtin sh.
134void xexec(char **argv)
135{
Rob Landley977e48e2014-10-20 19:52:29 -0500136 if (CFG_TOYBOX && !CFG_TOYBOX_NORECURSE) toy_exec(argv);
Rob Landleyd3904932013-07-16 00:04:56 -0500137 execvp(argv[0], argv);
138
139 perror_exit("exec %s", argv[0]);
140}
141
Rob Landley44e68a12014-06-03 06:27:24 -0500142// Spawn child process, capturing stdin/stdout.
143// argv[]: command to exec. If null, child returns to original program.
Rob Landley360d57f2014-09-14 12:29:44 -0500144// pipes[2]: stdin, stdout of new process. If -1 will not have pipe allocated.
Rob Landley44e68a12014-06-03 06:27:24 -0500145// return: pid of child process
Rob Landley360d57f2014-09-14 12:29:44 -0500146pid_t xpopen_both(char **argv, int *pipes)
Rob Landley44e68a12014-06-03 06:27:24 -0500147{
148 int cestnepasun[4], pid;
149
Rob Landley360d57f2014-09-14 12:29:44 -0500150 // Make the pipes? Not this won't set either pipe to 0 because if fds are
151 // allocated in order and if fd0 was free it would go to cestnepasun[0]
Rob Landley44e68a12014-06-03 06:27:24 -0500152 if (pipes) {
Rob Landley360d57f2014-09-14 12:29:44 -0500153 for (pid = 0; pid < 2; pid++) {
154 if (pipes[pid] == -1) continue;
155 if (pipe(cestnepasun+(2*pid))) perror_exit("pipe");
156 pipes[pid] = cestnepasun[pid+1];
157 }
Rob Landley44e68a12014-06-03 06:27:24 -0500158 }
159
160 // Child process
161 if (!(pid = xfork())) {
162 // Dance of the stdin/stdout redirection.
163 if (pipes) {
Rob Landley360d57f2014-09-14 12:29:44 -0500164 // if we had no stdin/out, pipe handles could overlap, so test for it
165 // and free up potentially overlapping pipe handles before reuse
166 if (pipes[1] != -1) close(cestnepasun[2]);
167 if (pipes[0] != -1) {
168 close(cestnepasun[1]);
169 if (cestnepasun[0]) {
170 dup2(cestnepasun[0], 0);
171 close(cestnepasun[0]);
172 }
Rob Landley44e68a12014-06-03 06:27:24 -0500173 }
Rob Landley360d57f2014-09-14 12:29:44 -0500174 if (pipes[1] != -1) {
175 dup2(cestnepasun[3], 1);
176 dup2(cestnepasun[3], 2);
177 if (cestnepasun[3] > 2 || !cestnepasun[3]) close(cestnepasun[3]);
178 }
Rob Landley44e68a12014-06-03 06:27:24 -0500179 }
180 if (argv) {
181 if (CFG_TOYBOX) toy_exec(argv);
182 execvp(argv[0], argv);
183 _exit(127);
184 }
185 return 0;
186
Rob Landley44e68a12014-06-03 06:27:24 -0500187 }
Rob Landley360d57f2014-09-14 12:29:44 -0500188
189 // Parent process
190 if (pipes) {
191 if (pipes[0] != -1) close(cestnepasun[0]);
192 if (pipes[1] != -1) close(cestnepasun[3]);
193 }
194
195 return pid;
Rob Landley44e68a12014-06-03 06:27:24 -0500196}
197
Rob Landley360d57f2014-09-14 12:29:44 -0500198int xpclose_both(pid_t pid, int *pipes)
Rob Landley44e68a12014-06-03 06:27:24 -0500199{
200 int rc = 127;
201
202 if (pipes) {
203 close(pipes[0]);
204 close(pipes[1]);
205 }
206 waitpid(pid, &rc, 0);
207
208 return WIFEXITED(rc) ? WEXITSTATUS(rc) : WTERMSIG(rc) + 127;
209}
210
Rob Landley360d57f2014-09-14 12:29:44 -0500211// Wrapper to xpopen with a pipe for just one of stdin/stdout
212pid_t xpopen(char **argv, int *pipe, int stdout)
213{
Rob Landley8a990712014-09-14 19:54:19 -0500214 int pipes[2], pid;
Rob Landley360d57f2014-09-14 12:29:44 -0500215
Rob Landley8a990712014-09-14 19:54:19 -0500216 pipes[!stdout] = -1;
217 pipes[!!stdout] = 0;
218 pid = xpopen_both(argv, pipes);
219 *pipe = pid ? pipes[!!stdout] : -1;
Rob Landley360d57f2014-09-14 12:29:44 -0500220
Rob Landley8a990712014-09-14 19:54:19 -0500221 return pid;
Rob Landley360d57f2014-09-14 12:29:44 -0500222}
223
224int xpclose(pid_t pid, int pipe)
225{
226 close(pipe);
227
228 return xpclose_both(pid, 0);
229}
230
231// Call xpopen and wait for it to finish, keeping existing stdin/stdout.
232int xrun(char **argv)
233{
234 return xpclose_both(xpopen_both(argv, 0), 0);
235}
236
Rob Landleyd3904932013-07-16 00:04:56 -0500237void xaccess(char *path, int flags)
238{
239 if (access(path, flags)) perror_exit("Can't access '%s'", path);
240}
241
242// Die unless we can delete a file. (File must exist to be deleted.)
243void xunlink(char *path)
244{
245 if (unlink(path)) perror_exit("unlink '%s'", path);
246}
247
248// Die unless we can open/create a file, returning file descriptor.
249int xcreate(char *path, int flags, int mode)
250{
Rob Landleyccb73f82014-07-26 13:27:07 -0500251 int fd = open(path, flags^O_CLOEXEC, mode);
Rob Landleyd3904932013-07-16 00:04:56 -0500252 if (fd == -1) perror_exit("%s", path);
253 return fd;
254}
255
256// Die unless we can open a file, returning file descriptor.
257int xopen(char *path, int flags)
258{
259 return xcreate(path, flags, 0);
260}
261
262void xclose(int fd)
263{
264 if (close(fd)) perror_exit("xclose");
265}
266
267int xdup(int fd)
268{
269 if (fd != -1) {
270 fd = dup(fd);
271 if (fd == -1) perror_exit("xdup");
272 }
273 return fd;
274}
275
Rob Landley1aa75112013-08-07 12:19:51 -0500276FILE *xfdopen(int fd, char *mode)
277{
278 FILE *f = fdopen(fd, mode);
279
280 if (!f) perror_exit("xfdopen");
281
282 return f;
283}
284
Rob Landleyd3904932013-07-16 00:04:56 -0500285// Die unless we can open/create a file, returning FILE *.
286FILE *xfopen(char *path, char *mode)
287{
288 FILE *f = fopen(path, mode);
289 if (!f) perror_exit("No file %s", path);
290 return f;
291}
292
293// Die if there's an error other than EOF.
294size_t xread(int fd, void *buf, size_t len)
295{
296 ssize_t ret = read(fd, buf, len);
297 if (ret < 0) perror_exit("xread");
298
299 return ret;
300}
301
302void xreadall(int fd, void *buf, size_t len)
303{
304 if (len != readall(fd, buf, len)) perror_exit("xreadall");
305}
306
307// There's no xwriteall(), just xwrite(). When we read, there may or may not
308// be more data waiting. When we write, there is data and it had better go
309// somewhere.
310
311void xwrite(int fd, void *buf, size_t len)
312{
313 if (len != writeall(fd, buf, len)) perror_exit("xwrite");
314}
315
316// Die if lseek fails, probably due to being called on a pipe.
317
318off_t xlseek(int fd, off_t offset, int whence)
319{
320 offset = lseek(fd, offset, whence);
321 if (offset<0) perror_exit("lseek");
322
323 return offset;
324}
325
326char *xgetcwd(void)
327{
328 char *buf = getcwd(NULL, 0);
329 if (!buf) perror_exit("xgetcwd");
330
331 return buf;
332}
333
334void xstat(char *path, struct stat *st)
335{
336 if(stat(path, st)) perror_exit("Can't stat %s", path);
337}
338
339// Cannonicalize path, even to file with one or more missing components at end.
340// if exact, require last path component to exist
Rob Landley2c1cf4a2015-01-18 14:06:14 -0600341char *xabspath(char *path, int exact)
Rob Landleyd3904932013-07-16 00:04:56 -0500342{
343 struct string_list *todo, *done = 0;
344 int try = 9999, dirfd = open("/", 0);;
345 char buf[4096], *ret;
346
347 // If this isn't an absolute path, start with cwd.
348 if (*path != '/') {
349 char *temp = xgetcwd();
350
351 splitpath(path, splitpath(temp, &todo));
352 free(temp);
353 } else splitpath(path, &todo);
354
355 // Iterate through path components
356 while (todo) {
357 struct string_list *new = llist_pop(&todo), **tail;
358 ssize_t len;
359
360 if (!try--) {
361 errno = ELOOP;
362 goto error;
363 }
364
365 // Removable path componenents.
366 if (!strcmp(new->str, ".") || !strcmp(new->str, "..")) {
367 int x = new->str[1];
368
369 free(new);
370 if (x) {
371 if (done) free(llist_pop(&done));
372 len = 0;
373 } else continue;
374
375 // Is this a symlink?
376 } else len=readlinkat(dirfd, new->str, buf, 4096);
377
378 if (len>4095) goto error;
379 if (len<1) {
380 int fd;
381 char *s = "..";
382
383 // For .. just move dirfd
384 if (len) {
385 // Not a symlink: add to linked list, move dirfd, fail if error
386 if ((exact || todo) && errno != EINVAL) goto error;
387 new->next = done;
388 done = new;
389 if (errno == EINVAL && !todo) break;
390 s = new->str;
391 }
392 fd = openat(dirfd, s, 0);
393 if (fd == -1 && (exact || todo || errno != ENOENT)) goto error;
394 close(dirfd);
395 dirfd = fd;
396 continue;
397 }
398
399 // If this symlink is to an absolute path, discard existing resolved path
400 buf[len] = 0;
401 if (*buf == '/') {
402 llist_traverse(done, free);
403 done=0;
404 close(dirfd);
405 dirfd = open("/", 0);
406 }
407 free(new);
408
409 // prepend components of new path. Note symlink to "/" will leave new NULL
410 tail = splitpath(buf, &new);
411
412 // symlink to "/" will return null and leave tail alone
413 if (new) {
414 *tail = todo;
415 todo = new;
416 }
417 }
418 close(dirfd);
419
420 // At this point done has the path, in reverse order. Reverse list while
421 // calculating buffer length.
422
423 try = 2;
424 while (done) {
425 struct string_list *temp = llist_pop(&done);;
426
427 if (todo) try++;
428 try += strlen(temp->str);
429 temp->next = todo;
430 todo = temp;
431 }
432
433 // Assemble return buffer
434
435 ret = xmalloc(try);
436 *ret = '/';
437 ret [try = 1] = 0;
438 while (todo) {
439 if (try>1) ret[try++] = '/';
440 try = stpcpy(ret+try, todo->str) - ret;
441 free(llist_pop(&todo));
442 }
443
444 return ret;
445
446error:
447 close(dirfd);
448 llist_traverse(todo, free);
449 llist_traverse(done, free);
450
451 return NULL;
452}
453
Rob Landleyd3904932013-07-16 00:04:56 -0500454void xchdir(char *path)
455{
456 if (chdir(path)) error_exit("chdir '%s'", path);
457}
458
Rob Landleyafba5b82013-12-23 06:49:38 -0600459void xchroot(char *path)
460{
461 if (chroot(path)) error_exit("chroot '%s'", path);
462 xchdir("/");
463}
464
Rob Landley9e44a582013-11-28 20:18:04 -0600465struct passwd *xgetpwuid(uid_t uid)
466{
467 struct passwd *pwd = getpwuid(uid);
Rob Landley5ec4ab32013-11-28 21:06:15 -0600468 if (!pwd) error_exit("bad uid %ld", (long)uid);
Rob Landley9e44a582013-11-28 20:18:04 -0600469 return pwd;
470}
471
472struct group *xgetgrgid(gid_t gid)
473{
474 struct group *group = getgrgid(gid);
Rob Landley4fd07e02014-07-21 19:57:36 -0500475
476 if (!group) perror_exit("gid %ld", (long)gid);
Rob Landley9e44a582013-11-28 20:18:04 -0600477 return group;
478}
479
Rob Landleyb8140d12015-03-12 11:11:08 -0500480struct passwd *xgetpwnamid(char *user)
481{
482 struct passwd *up = getpwnam(user);
483 uid_t uid;
484
485 if (!up) {
486 char *s = 0;
487
488 uid = estrtol(user, &s, 10);
489 if (!errno && s && !*s) up = getpwuid(uid);
490 }
491 if (!up) perror_exit("user '%s'", user);
492
493 return up;
494}
495
496struct group *xgetgrnamid(char *group)
497{
498 struct group *gr = getgrnam(group);
499 gid_t gid;
500
501 if (!gr) {
502 char *s = 0;
503
504 gid = estrtol(group, &s, 10);
505 if (!errno && s && !*s) gr = getgrgid(gid);
506 }
507 if (!gr) perror_exit("group '%s'", group);
508
509 return gr;
510}
511
Rob Landley5ec4ab32013-11-28 21:06:15 -0600512struct passwd *xgetpwnam(char *name)
513{
514 struct passwd *up = getpwnam(name);
Rob Landley4fd07e02014-07-21 19:57:36 -0500515
516 if (!up) perror_exit("user '%s'", name);
Rob Landley5ec4ab32013-11-28 21:06:15 -0600517 return up;
518}
519
Rob Landley60c35c42014-08-03 15:50:10 -0500520struct group *xgetgrnam(char *name)
521{
522 struct group *gr = getgrnam(name);
523
524 if (!gr) perror_exit("group '%s'", name);
525 return gr;
526}
527
Rob Landleyafba5b82013-12-23 06:49:38 -0600528// setuid() can fail (for example, too many processes belonging to that user),
529// which opens a security hole if the process continues as the original user.
530
531void xsetuser(struct passwd *pwd)
532{
533 if (initgroups(pwd->pw_name, pwd->pw_gid) || setgid(pwd->pw_uid)
534 || setuid(pwd->pw_uid)) perror_exit("xsetuser '%s'", pwd->pw_name);
535}
536
Rob Landleyd3904932013-07-16 00:04:56 -0500537// This can return null (meaning file not found). It just won't return null
538// for memory allocation reasons.
539char *xreadlink(char *name)
540{
541 int len, size = 0;
542 char *buf = 0;
543
544 // Grow by 64 byte chunks until it's big enough.
545 for(;;) {
546 size +=64;
547 buf = xrealloc(buf, size);
548 len = readlink(name, buf, size);
549
550 if (len<0) {
551 free(buf);
552 return 0;
553 }
554 if (len<size) {
555 buf[len]=0;
556 return buf;
557 }
558 }
559}
560
Rob Landleydc373172013-12-27 18:45:01 -0600561char *xreadfile(char *name, char *buf, off_t len)
Rob Landleyd3904932013-07-16 00:04:56 -0500562{
Rob Landleydc373172013-12-27 18:45:01 -0600563 if (!(buf = readfile(name, buf, len))) perror_exit("Bad '%s'", name);
564
Rob Landleyd3904932013-07-16 00:04:56 -0500565 return buf;
566}
567
568int xioctl(int fd, int request, void *data)
569{
570 int rc;
571
572 errno = 0;
573 rc = ioctl(fd, request, data);
574 if (rc == -1 && errno) perror_exit("ioctl %x", request);
575
576 return rc;
577}
578
579// Open a /var/run/NAME.pid file, dying if we can't write it or if it currently
580// exists and is this executable.
581void xpidfile(char *name)
582{
583 char pidfile[256], spid[32];
584 int i, fd;
585 pid_t pid;
586
587 sprintf(pidfile, "/var/run/%s.pid", name);
588 // Try three times to open the sucker.
589 for (i=0; i<3; i++) {
Felix Jandadccfb2a2013-08-26 21:55:33 +0200590 fd = open(pidfile, O_CREAT|O_EXCL|O_WRONLY, 0644);
Rob Landleyd3904932013-07-16 00:04:56 -0500591 if (fd != -1) break;
592
593 // If it already existed, read it. Loop for race condition.
594 fd = open(pidfile, O_RDONLY);
595 if (fd == -1) continue;
596
597 // Is the old program still there?
598 spid[xread(fd, spid, sizeof(spid)-1)] = 0;
599 close(fd);
600 pid = atoi(spid);
Rob Landley46e8e1d2013-09-06 04:45:36 -0500601 if (pid < 1 || (kill(pid, 0) && errno == ESRCH)) unlink(pidfile);
Rob Landleyd3904932013-07-16 00:04:56 -0500602
603 // An else with more sanity checking might be nice here.
604 }
605
606 if (i == 3) error_exit("xpidfile %s", name);
607
608 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid()));
609 close(fd);
610}
611
612// Copy the rest of in to out and close both files.
613
614void xsendfile(int in, int out)
615{
616 long len;
Rob Landleyd3904932013-07-16 00:04:56 -0500617
618 if (in<0) return;
619 for (;;) {
Rob Landley6d283702014-11-28 16:57:45 -0600620 len = xread(in, libbuf, sizeof(libbuf));
Rob Landleyd3904932013-07-16 00:04:56 -0500621 if (len<1) break;
Rob Landley6d283702014-11-28 16:57:45 -0600622 xwrite(out, libbuf, len);
Rob Landleyd3904932013-07-16 00:04:56 -0500623 }
624}
Rob Landley72756672013-07-17 17:22:46 -0500625
626// parse fractional seconds with optional s/m/h/d suffix
627long xparsetime(char *arg, long units, long *fraction)
628{
629 double d;
630 long l;
631
632 if (CFG_TOYBOX_FLOAT) d = strtod(arg, &arg);
633 else l = strtoul(arg, &arg, 10);
Rob Landley2c1cf4a2015-01-18 14:06:14 -0600634
Rob Landley72756672013-07-17 17:22:46 -0500635 // Parse suffix
636 if (*arg) {
637 int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg);
638
639 if (i == -1) error_exit("Unknown suffix '%c'", *arg);
640 if (CFG_TOYBOX_FLOAT) d *= ismhd[i];
641 else l *= ismhd[i];
642 }
643
644 if (CFG_TOYBOX_FLOAT) {
645 l = (long)d;
646 if (fraction) *fraction = units*(d-l);
647 } else if (fraction) *fraction = 0;
648
649 return l;
650}
Rob Landley5b405822014-03-29 18:11:00 -0500651
652// Compile a regular expression into a regex_t
653void xregcomp(regex_t *preg, char *regex, int cflags)
654{
655 int rc = regcomp(preg, regex, cflags);
656
657 if (rc) {
658 regerror(rc, preg, libbuf, sizeof(libbuf));
659 error_exit("xregcomp: %s", libbuf);
660 }
661}
Rob Landleyc277f342015-02-09 16:34:24 -0600662
663char *xtzset(char *new)
664{
665 char *tz = getenv("TZ");
666
667 if (tz) tz = xstrdup(tz);
668 if (setenv("TZ", new, 1)) perror_exit("setenv");
669 tzset();
670
671 return tz;
672}
Rob Landleye6abb612015-03-09 14:52:32 -0500673
674// Set a signal handler
675void xsignal(int signal, void *handler)
676{
677 struct sigaction *sa = (void *)libbuf;
678
679 memset(sa, 0, sizeof(struct sigaction));
680 sa->sa_handler = handler;
681
682 if (sigaction(signal, sa, 0)) perror_exit("xsignal %d", signal);
683}