~drizzle-trunk/drizzle/development

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