~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to extra/mysql_waitpid.cc

  • Committer: Brian Aker
  • Date: 2009-05-26 02:54:31 UTC
  • mfrom: (1022.2.34 mordred)
  • Revision ID: brian@gaz-20090526025431-hpjmsrr4j6nftuic
Merge Monty

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