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