One of the entry points to release an ip reset the pnn field before invoking the...
[sahlberg/ctdb.git] / 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 "lib/tevent/tevent.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "../include/ctdb_client.h"
25 #include "../include/ctdb_private.h"
26 #include "../common/rb_tree.h"
27 #include <ctype.h>
28
29 /* Handle common command line options for ctdb test progs
30  */
31
32 static struct {
33         const char *socketname;
34         const char *debuglevel;
35         int torture;
36         const char *events;
37 } ctdb_cmdline = {
38         .torture = 0,
39         .debuglevel = "ERR",
40 };
41
42 enum {OPT_EVENTSYSTEM=1};
43
44 static void ctdb_cmdline_callback(poptContext con, 
45                                   enum poptCallbackReason reason,
46                                   const struct poptOption *opt,
47                                   const char *arg, const void *data)
48 {
49         switch (opt->val) {
50         case OPT_EVENTSYSTEM:
51                 event_set_default_backend(arg);
52                 break;
53         }
54 }
55
56
57 struct poptOption popt_ctdb_cmdline[] = {
58         { NULL, 0, POPT_ARG_CALLBACK, (void *)ctdb_cmdline_callback },  
59         { "socket", 0, POPT_ARG_STRING, &ctdb_cmdline.socketname, 0, "local socket name", "filename" },
60         { "debug", 'd', POPT_ARG_STRING, &ctdb_cmdline.debuglevel, 0, "debug level"},
61         { "torture", 0, POPT_ARG_NONE, &ctdb_cmdline.torture, 0, "enable nastiness in library", NULL },
62         { "events", 0, POPT_ARG_STRING, NULL, OPT_EVENTSYSTEM, "event system", NULL },
63         { NULL }
64 };
65
66
67 /*
68   startup daemon side of ctdb according to command line options
69  */
70 struct ctdb_context *ctdb_cmdline_init(struct event_context *ev)
71 {
72         struct ctdb_context *ctdb;
73         int ret;
74
75         /* initialise ctdb */
76         ctdb = ctdb_init(ev);
77         if (ctdb == NULL) {
78                 printf("Failed to init ctdb\n");
79                 exit(1);
80         }
81
82         if (ctdb_cmdline.torture) {
83                 ctdb_set_flags(ctdb, CTDB_FLAG_TORTURE);
84         }
85
86         /* command line specified a socket name */
87         if (ctdb_cmdline.socketname != NULL) {
88                 setenv("CTDB_SOCKET", ctdb_cmdline.socketname, 1);
89                 ret = ctdb_set_socketname(ctdb, ctdb_cmdline.socketname);
90                 if (ret == -1) {
91                         printf("ctdb_set_socketname failed - %s\n",
92                                                     ctdb_errstr(ctdb));
93                         exit(1);
94                 }
95         }
96
97         /* Set the debug level */
98         if (isalpha(ctdb_cmdline.debuglevel[0]) || ctdb_cmdline.debuglevel[0] == '-') { 
99                 LogLevel = get_debug_by_desc(ctdb_cmdline.debuglevel);
100         } else {
101                 LogLevel = strtol(ctdb_cmdline.debuglevel, NULL, 0);
102         }
103
104         /* set up the tree to store server ids */
105         ctdb->server_ids = trbt_create(ctdb, 0);
106
107         return ctdb;
108 }
109
110
111 /*
112   startup a client only ctdb context
113  */
114 struct ctdb_context *ctdb_cmdline_client(struct tevent_context *ev,
115                                          struct timeval req_timeout)
116 {
117         struct ctdb_context *ctdb;
118         char *socket_name;
119         int ret;
120
121         /* initialise ctdb */
122         ctdb = ctdb_init(ev);
123         if (ctdb == NULL) {
124                 fprintf(stderr, "Failed to init ctdb\n");
125                 exit(1);
126         }
127
128         /* tell ctdb the socket address */
129         socket_name = getenv("CTDB_SOCKET");
130         if (socket_name != NULL) {
131                 ret = ctdb_set_socketname(ctdb, socket_name);
132                 if (ret == -1) {
133                         printf("ctdb_set_socketname failed - %s\n",
134                                                     ctdb_errstr(ctdb));
135                         exit(1);
136                 }
137         }
138
139         if (ctdb_cmdline.socketname != NULL) {
140                 ret = ctdb_set_socketname(ctdb, ctdb_cmdline.socketname);
141                 if (ret == -1) {
142                         fprintf(stderr, "ctdb_set_socketname failed - %s\n",
143                                         ctdb_errstr(ctdb));
144                         exit(1);
145                 }
146         }
147
148         ret = ctdb_socket_connect(ctdb);
149         if (ret != 0) {
150                 fprintf(stderr, __location__ " Failed to connect to daemon\n");
151                 talloc_free(ctdb);
152                 return NULL;
153         }
154
155         /* get our pnn */
156         ctdb->pnn = ctdb_ctrl_getpnn(ctdb, req_timeout, CTDB_CURRENT_NODE);
157         if (ctdb->pnn == (uint32_t)-1) {
158                 DEBUG(DEBUG_CRIT,(__location__ " Failed to get ctdb pnn\n"));
159                 talloc_free(ctdb);
160                 return NULL;
161         }
162
163         return ctdb;
164 }