1 | /* |
2 | Copyright (c) 2003, 2010, Oracle and/or its affiliates |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; version 2 of the License. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
16 | |
17 | /* Wait until a program dies */ |
18 | |
19 | #include <my_global.h> |
20 | #include <m_string.h> |
21 | #include <my_sys.h> |
22 | #include <my_getopt.h> |
23 | #include <signal.h> |
24 | #include <errno.h> |
25 | |
26 | static const char *VER= "1.1" ; |
27 | static char *progname; |
28 | static my_bool verbose; |
29 | |
30 | void usage(void); |
31 | |
32 | static struct my_option my_long_options[] = |
33 | { |
34 | {"help" , '?', "Display this help and exit." , 0, 0, 0, GET_NO_ARG, NO_ARG, 0, |
35 | 0, 0, 0, 0, 0}, |
36 | {"help" , 'I', "Synonym for -?." , 0, 0, 0, GET_NO_ARG, NO_ARG, 0, |
37 | 0, 0, 0, 0, 0}, |
38 | {"verbose" , 'v', |
39 | "Be more verbose. Give a warning, if kill can't handle signal 0." , |
40 | &verbose, &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0}, |
41 | {"version" , 'V', "Print version information and exit." , 0, 0, 0, |
42 | GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, |
43 | { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} |
44 | }; |
45 | |
46 | static my_bool |
47 | get_one_option(int optid, const struct my_option *opt __attribute__((unused)), |
48 | char *argument __attribute__((unused))) |
49 | { |
50 | switch(optid) { |
51 | case 'V': |
52 | printf("%s version %s by Jani Tolonen\n" , progname, VER); |
53 | exit(0); |
54 | case 'I': |
55 | case '?': |
56 | usage(); |
57 | exit(0); |
58 | } |
59 | return 0; |
60 | } |
61 | |
62 | |
63 | int main(int argc, char *argv[]) |
64 | { |
65 | int pid= 0, t= 0, sig= 0; |
66 | |
67 | progname= argv[0]; |
68 | |
69 | if (handle_options(&argc, &argv, my_long_options, get_one_option)) |
70 | exit(-1); |
71 | if (!argv[0] || !argv[1] || (pid= atoi(argv[0])) <= 0 || |
72 | (t= atoi(argv[1])) <= 0) |
73 | { |
74 | usage(); |
75 | exit(-1); |
76 | } |
77 | for (; t > 0; t--) |
78 | { |
79 | if (kill((pid_t) pid, sig)) |
80 | { |
81 | if (errno == EINVAL) |
82 | { |
83 | if (verbose) |
84 | printf("WARNING: kill couldn't handle signal 0, using signal 1.\n" ); |
85 | sig= 1; |
86 | t++; |
87 | continue; |
88 | } |
89 | return 0; |
90 | } |
91 | sleep(1); |
92 | } |
93 | return 1; |
94 | } |
95 | |
96 | void usage(void) |
97 | { |
98 | printf("%s version %s by Jani Tolonen\n\n" , progname, VER); |
99 | printf("usage: %s [options] #pid #time\n\n" , progname); |
100 | printf("Description: Waits for a program, which program id is #pid, to\n" ); |
101 | printf("terminate within #time seconds. If the program terminates within\n" ); |
102 | printf("this time, or if the #pid no longer exists, value 0 is returned.\n" ); |
103 | printf("Otherwise 1 is returned. Both #pid and #time must be positive\n" ); |
104 | printf("integer arguments.\n\n" ); |
105 | printf("Options:\n" ); |
106 | my_print_help(my_long_options); |
107 | } |
108 | |