ctdb-daemon: Stop using tevent compatibility definitions
[vlendec/samba-autobuild/.git] / ctdb / common / cmdline.c
1 /* 
2    common commandline code to ctdb test tools
3
4    Copyright (C) Andrew Tridgell  2007
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program 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
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "popt.h"
23 #include "../include/ctdb_client.h"
24 #include "../include/ctdb_private.h"
25 #include "../common/rb_tree.h"
26 #include <ctype.h>
27
28 #include "common/cmdline.h"
29
30 /* Handle common command line options for ctdb test progs
31  */
32
33 static struct {
34         const char *socketname;
35         const char *debuglevel;
36         int torture;
37         const char *events;
38 } ctdb_cmdline = {
39         .torture = 0,
40         .debuglevel = "NOTICE",
41 };
42
43 enum {OPT_EVENTSYSTEM=1};
44
45 static void ctdb_cmdline_callback(poptContext con, 
46                                   enum poptCallbackReason reason,
47                                   const struct poptOption *opt,
48                                   const char *arg, const void *data)
49 {
50         switch (opt->val) {
51         case OPT_EVENTSYSTEM:
52                 tevent_set_default_backend(arg);
53                 break;
54         }
55 }
56
57
58 struct poptOption popt_ctdb_cmdline[] = {
59         { NULL, 0, POPT_ARG_CALLBACK, (void *)ctdb_cmdline_callback },  
60         { "socket", 0, POPT_ARG_STRING, &ctdb_cmdline.socketname, 0, "local socket name", "filename" },
61         { "debug", 'd', POPT_ARG_STRING, &ctdb_cmdline.debuglevel, 0, "debug level"},
62         { "torture", 0, POPT_ARG_NONE, &ctdb_cmdline.torture, 0, "enable nastiness in library", NULL },
63         { "events", 0, POPT_ARG_STRING, NULL, OPT_EVENTSYSTEM, "event system", NULL },
64         { NULL }
65 };
66
67
68 /*
69   startup daemon side of ctdb according to command line options
70  */
71 struct ctdb_context *ctdb_cmdline_init(struct tevent_context *ev)
72 {
73         struct ctdb_context *ctdb;
74         int ret;
75
76         /* initialise ctdb */
77         ctdb = ctdb_init(ev);
78         if (ctdb == NULL) {
79                 printf("Failed to init ctdb\n");
80                 exit(1);
81         }
82
83         if (ctdb_cmdline.torture) {
84                 ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
85         }
86
87         /* command line specified a socket name */
88         if (ctdb_cmdline.socketname != NULL) {
89                 setenv("CTDB_SOCKET", ctdb_cmdline.socketname, 1);
90                 ret = ctdb_set_socketname(ctdb, ctdb_cmdline.socketname);
91                 if (ret == -1) {
92                         printf("ctdb_set_socketname failed - %s\n",
93                                                     ctdb_errstr(ctdb));
94                         exit(1);
95                 }
96         }
97
98         /* Set the debug level */
99         if (!parse_debug(ctdb_cmdline.debuglevel, &DEBUGLEVEL)) {
100                 DEBUGLEVEL = DEBUG_NOTICE;
101         }
102
103         /* set up the tree to store server ids */
104         ctdb->server_ids = trbt_create(ctdb, 0);
105
106         return ctdb;
107 }
108
109
110 /*
111   startup a client only ctdb context
112  */
113 struct ctdb_context *ctdb_cmdline_client(struct tevent_context *ev,
114                                          struct timeval req_timeout)
115 {
116         struct ctdb_context *ctdb;
117         char *socket_name;
118         int ret;
119
120         /* initialise ctdb */
121         ctdb = ctdb_init(ev);
122         if (ctdb == NULL) {
123                 fprintf(stderr, "Failed to init ctdb\n");
124                 exit(1);
125         }
126
127         /* tell ctdb the socket address */
128         socket_name = getenv("CTDB_SOCKET");
129         if (socket_name != NULL) {
130                 ret = ctdb_set_socketname(ctdb, socket_name);
131                 if (ret == -1) {
132                         printf("ctdb_set_socketname failed - %s\n",
133                                                     ctdb_errstr(ctdb));
134                         exit(1);
135                 }
136         }
137
138         if (ctdb_cmdline.socketname != NULL) {
139                 ret = ctdb_set_socketname(ctdb, ctdb_cmdline.socketname);
140                 if (ret == -1) {
141                         fprintf(stderr, "ctdb_set_socketname failed - %s\n",
142                                         ctdb_errstr(ctdb));
143                         exit(1);
144                 }
145         }
146
147         /* Set the debug level */
148         if (!parse_debug(ctdb_cmdline.debuglevel, &DEBUGLEVEL)) {
149                 DEBUGLEVEL = DEBUG_NOTICE;
150         }
151
152         ret = ctdb_socket_connect(ctdb);
153         if (ret != 0) {
154                 fprintf(stderr, __location__ " Failed to connect to daemon\n");
155                 talloc_free(ctdb);
156                 return NULL;
157         }
158
159         /* get our pnn */
160         ctdb->pnn = ctdb_ctrl_getpnn(ctdb, req_timeout, CTDB_CURRENT_NODE);
161         if (ctdb->pnn == (uint32_t)-1) {
162                 DEBUG(DEBUG_CRIT,(__location__ " Failed to get ctdb pnn\n"));
163                 talloc_free(ctdb);
164                 return NULL;
165         }
166
167         return ctdb;
168 }