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