b966126fad23528fd96adf7947e01975d02e6561
[abartlet/samba.git/.git] / source4 / cluster / ctdb / direct / ctdbd.c
1 /* 
2    standalone ctdb daemon
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 3 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with this library; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "system/filesys.h"
24 #include "popt.h"
25 #include "system/wait.h"
26 #include "cmdline.h"
27
28 static void block_signal(int signum)
29 {
30         struct sigaction act;
31
32         memset(&act, 0, sizeof(act));
33
34         act.sa_handler = SIG_IGN;
35         sigemptyset(&act.sa_mask);
36         sigaddset(&act.sa_mask, signum);
37         sigaction(signum, &act, NULL);
38 }
39
40
41 /*
42   main program
43 */
44 int main(int argc, const char *argv[])
45 {
46         struct ctdb_context *ctdb;
47         const char *db_list = "test.tdb";
48         char *s, *tok;
49
50         struct poptOption popt_options[] = {
51                 POPT_AUTOHELP
52                 POPT_CTDB_CMDLINE
53                 { "dblist", 0, POPT_ARG_STRING, &db_list, 0, "list of databases", NULL },
54                 POPT_TABLEEND
55         };
56         int opt;
57         const char **extra_argv;
58         int extra_argc = 0;
59         int ret;
60         poptContext pc;
61         struct event_context *ev;
62
63         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
64
65         while ((opt = poptGetNextOpt(pc)) != -1) {
66                 switch (opt) {
67                 default:
68                         fprintf(stderr, "Invalid option %s: %s\n", 
69                                 poptBadOption(pc, 0), poptStrerror(opt));
70                         exit(1);
71                 }
72         }
73
74         /* setup the remaining options for the main program to use */
75         extra_argv = poptGetArgs(pc);
76         if (extra_argv) {
77                 extra_argv++;
78                 while (extra_argv[extra_argc]) extra_argc++;
79         }
80
81         block_signal(SIGPIPE);
82
83         ev = event_context_init(NULL);
84
85         ctdb = ctdb_cmdline_init(ev);
86
87         /* attach to the list of databases */
88         s = talloc_strdup(ctdb, db_list);
89         for (tok=strtok(s, ", "); tok; tok=strtok(NULL, ", ")) {
90                 struct ctdb_db_context *ctdb_db;
91                 ctdb_db = ctdb_attach(ctdb, tok, TDB_DEFAULT, 
92                                       O_RDWR|O_CREAT|O_TRUNC, 0666);
93                 if (!ctdb_db) {
94                         printf("ctdb_attach to '%s'failed - %s\n", tok, 
95                                ctdb_errstr(ctdb));
96                         exit(1);
97                 }
98                 printf("Attached to database '%s'\n", tok);
99         }
100
101         /* start the protocol running */
102         ret = ctdb_start(ctdb);
103
104 /*      event_loop_wait(ev);*/
105         while (1) {
106                 event_loop_once(ev);
107         }
108
109         /* shut it down */
110         talloc_free(ev);
111         return 0;
112 }