Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

klish / bin / sigexec.c @ 1a733d28

History | View | Annotate | Download (2.28 KB)

1
/*
2
 * sigexec.c
3
 *
4
 * Programm to execute processes with unblocked signals.
5
 *
6
 */
7

    
8
#ifdef HAVE_CONFIG_H
9
#include "config.h"
10
#endif /* HAVE_CONFIG_H */
11

    
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <errno.h>
15
#include <string.h>
16
#include <unistd.h>
17
#include <signal.h>
18

    
19
#if WITH_INTERNAL_GETOPT
20
#include "libc/getopt.h"
21
#else
22
#ifdef HAVE_GETOPT_H
23
#include <getopt.h>
24
#endif
25
#endif
26

    
27
#ifndef VERSION
28
#define VERSION 1.5.6
29
#endif
30
#define QUOTE(t) #t
31
#define version(v) printf("%s\n", v)
32

    
33
static void help(int status, const char *argv0);
34

    
35
int main(int argc, char *argv[])
36
{
37
        char **child_argv;
38
        sigset_t sigs;
39

    
40
        static const char *shortopts = "+hv";
41
#ifdef HAVE_GETOPT_LONG
42
        static const struct option longopts[] = {
43
                {"help",        0, NULL, 'h'},
44
                {"version",        0, NULL, 'v'},
45
                {NULL,                0, NULL, 0}
46
        };
47
#endif
48

    
49
        while(1) {
50
                int opt;
51
#ifdef HAVE_GETOPT_LONG
52
                opt = getopt_long(argc, argv, shortopts, longopts, NULL);
53
#else
54
                opt = getopt(argc, argv, shortopts);
55
#endif
56
                if (-1 == opt)
57
                        break;
58
                switch (opt) {
59
                case 0:
60
                        break;
61
                case 'h':
62
                        help(0, argv[0]);
63
                        exit(0);
64
                        break;
65
                case 'v':
66
                        version(VERSION);
67
                        exit(0);
68
                        break;
69
                default:
70
                        help(1, argv[0]);
71
                        break;
72
                }
73
        }
74

    
75
        child_argv = &argv[optind];
76
        /* Check user command */
77
        if (! child_argv[0]) {
78
                fprintf(stderr, "Error: Nothing to execute.\n");
79
                return 1;
80
        }
81

    
82
        /* Unblock signals */
83
        sigemptyset(&sigs);
84
        sigprocmask(SIG_SETMASK, &sigs, NULL);
85

    
86
        /* Execute user command */
87
/*        fprintf(stderr, "%s %s %s\n", child_argv[0], child_argv[1], child_argv[2]); */
88
        if (execvp(child_argv[0], child_argv) < 0) {
89
                fprintf(stderr, "Error: Cannot execute %s: %s\n",
90
                        child_argv[0], strerror(errno));
91
                return 1;
92
        }
93

    
94
        return 0;
95
}
96

    
97
/*--------------------------------------------------------- */
98
/* Print help message */
99
static void help(int status, const char *argv0)
100
{
101
        const char *name = NULL;
102

    
103
        if (!argv0)
104
                return;
105

    
106
        /* Find the basename */
107
        name = strrchr(argv0, '/');
108
        if (name)
109
                name++;
110
        else
111
                name = argv0;
112

    
113
        if (status != 0) {
114
                fprintf(stderr, "Try `%s -h' for more information.\n",
115
                        name);
116
        } else {
117
                printf("Usage: %s [options] -- <command to execute>\n", name);
118
                printf("Utility to execute process with unblocked signals.\n");
119
                printf("Options:\n");
120
                printf("\t-v, --version\tPrint utility version.\n");
121
                printf("\t-h, --help\tPrint this help.\n");
122
        }
123
}