~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to extra/mysql_waitpid.c

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 <mystrings/m_string.h>
19
 
#include <mysys/my_sys.h>
20
 
#include <mysys/my_getopt.h>
21
 
#include <signal.h>
22
 
#include <errno.h>
23
 
 
24
 
static const char *VER= "1.1";
25
 
static char *progname;
26
 
static bool verbose;
27
 
 
28
 
void usage(void);
29
 
 
30
 
static struct my_option my_long_options[] =
31
 
{
32
 
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
33
 
   0, 0, 0, 0, 0},
34
 
  {"help", 'I', "Synonym for -?.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
35
 
   0, 0, 0, 0, 0},
36
 
  {"verbose", 'v',
37
 
   "Be more verbose. Give a warning, if kill can't handle signal 0.", 
38
 
   (char**) &verbose, (char**) &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
39
 
  {"version", 'V', "Print version information and exit.", 0, 0, 0,
40
 
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
41
 
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
42
 
};
43
 
 
44
 
static bool
45
 
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
46
 
               char *argument __attribute__((unused)))
47
 
{
48
 
  switch(optid) {
49
 
  case 'V':
50
 
    printf("%s version %s by Jani Tolonen\n", progname, VER);
51
 
    exit(-1);
52
 
  case 'I':
53
 
  case '?':
54
 
    usage();
55
 
  }
56
 
  return 0;
57
 
}
58
 
 
59
 
 
60
 
int main(int argc, char *argv[])
61
 
{
62
 
  int pid= 0, t= 0, sig= 0;
63
 
 
64
 
  progname= argv[0];
65
 
 
66
 
  if (handle_options(&argc, &argv, my_long_options, get_one_option))
67
 
    exit(-1);
68
 
  if (!argv[0] || !argv[1] || (pid= atoi(argv[0])) <= 0 ||
69
 
      (t= atoi(argv[1])) <= 0)
70
 
    usage();
71
 
  for (; t > 0; t--)
72
 
  {
73
 
    if (kill((pid_t) pid, sig))
74
 
    {
75
 
      if (errno == EINVAL)
76
 
      {
77
 
        if (verbose)
78
 
          printf("WARNING: kill couldn't handle signal 0, using signal 1.\n");
79
 
        sig= 1;
80
 
        t++;
81
 
        continue;
82
 
      }
83
 
      return 0;
84
 
    }
85
 
    sleep(1);
86
 
  }
87
 
  return 1;
88
 
}
89
 
 
90
 
void usage(void)
91
 
{
92
 
  printf("%s version %s by Jani Tolonen\n\n", progname, VER);
93
 
  printf("usage: %s [options] #pid #time\n\n", progname);
94
 
  printf("Description: Waits for a program, which program id is #pid, to\n");
95
 
  printf("terminate within #time seconds. If the program terminates within\n");
96
 
  printf("this time, or if the #pid no longer exists, value 0 is returned.\n");
97
 
  printf("Otherwise 1 is returned. Both #pid and #time must be positive\n");
98
 
  printf("integer arguments.\n\n");
99
 
  printf("Options:\n");
100
 
  my_print_help(my_long_options);
101
 
  exit(-1);
102
 
}