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