| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2005 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | |
| 21 | #include <config.h> |
| 22 | #include "types.h" |
| 23 | |
| 24 | #include <getopt.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <db.h> |
| 28 | #include <locale.h> |
| 29 | #include <errno.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <unistd.h> |
| 32 | #include <pcre.h> |
| 33 | #include <string.h> |
| 34 | #include <syslog.h> |
| 35 | |
| 36 | #include "configuration.h" |
| 37 | #include "syscalls.h" |
| 38 | #include "log.h" |
| 39 | #include "defs.h" |
| 40 | #include "mem.h" |
| 41 | #include "kvp.h" |
| 42 | #include "trackdb.h" |
| 43 | #include "trackdb-int.h" |
| 44 | |
| 45 | static const struct option options[] = { |
| 46 | { "help", no_argument, 0, 'h' }, |
| 47 | { "version", no_argument, 0, 'V' }, |
| 48 | { "config", required_argument, 0, 'c' }, |
| 49 | { "debug", no_argument, 0, 'd' }, |
| 50 | { "no-debug", no_argument, 0, 'D' }, |
| 51 | { 0, 0, 0, 0 } |
| 52 | }; |
| 53 | |
| 54 | /* display usage message and terminate */ |
| 55 | static void help(void) { |
| 56 | xprintf("Usage:\n" |
| 57 | " disorder-deadlock [OPTIONS]\n" |
| 58 | "Options:\n" |
| 59 | " --help, -h Display usage message\n" |
| 60 | " --version, -V Display version number\n" |
| 61 | " --config PATH, -c PATH Set configuration file\n" |
| 62 | " --debug, -d Turn on debugging\n" |
| 63 | "\n" |
| 64 | "Deadlock manager for DisOrder. Not intended to be run\n" |
| 65 | "directly.\n"); |
| 66 | xfclose(stdout); |
| 67 | exit(0); |
| 68 | } |
| 69 | |
| 70 | /* display version number and terminate */ |
| 71 | static void version(void) { |
| 72 | xprintf("disorder-deadlock version %s\n", disorder_version_string); |
| 73 | xfclose(stdout); |
| 74 | exit(0); |
| 75 | } |
| 76 | |
| 77 | int main(int argc, char **argv) { |
| 78 | int n, err, aborted; |
| 79 | |
| 80 | set_progname(argv); |
| 81 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
| 82 | while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) { |
| 83 | switch(n) { |
| 84 | case 'h': help(); |
| 85 | case 'V': version(); |
| 86 | case 'c': configfile = optarg; break; |
| 87 | case 'd': debugging = 1; break; |
| 88 | case 'D': debugging = 0; break; |
| 89 | default: fatal(0, "invalid option"); |
| 90 | } |
| 91 | } |
| 92 | /* if stderr is a TTY then log there, otherwise to syslog */ |
| 93 | if(!isatty(2)) { |
| 94 | openlog(progname, LOG_PID, LOG_DAEMON); |
| 95 | log_default = &log_syslog; |
| 96 | } |
| 97 | if(config_read()) fatal(0, "cannot read configuration"); |
| 98 | info("started"); |
| 99 | trackdb_init(0); |
| 100 | while(getppid() != 1) { |
| 101 | if((err = trackdb_env->lock_detect(trackdb_env, |
| 102 | 0, |
| 103 | DB_LOCK_DEFAULT, |
| 104 | &aborted))) |
| 105 | fatal(0, "trackdb_env->lock_detect: %s", db_strerror(err)); |
| 106 | if(aborted) |
| 107 | D(("aborted %d lock requests", aborted)); |
| 108 | sleep(1); |
| 109 | } |
| 110 | /* if our parent goes away, it's time to stop */ |
| 111 | info("stopped (parent terminated)"); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | Local Variables: |
| 117 | c-basic-offset:2 |
| 118 | comment-column:40 |
| 119 | fill-column:79 |
| 120 | indent-tabs-mode:nil |
| 121 | End: |
| 122 | */ |