| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <lha_internal.h> |
| 24 |
|
| 25 |
#include <sys/param.h> |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <errno.h> |
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
#include <ctype.h> |
| 34 |
#include <clplumbing/lsb_exitcodes.h> |
| 35 |
#include <clplumbing/Gmain_timeout.h> |
| 36 |
#include <clplumbing/GSource.h> |
| 37 |
#include <clplumbing/coredumps.h> |
| 38 |
#include <dopd.h> |
| 39 |
#include <memory.h> |
| 40 |
|
| 41 |
#define OPTARGS "hVt:p:r:" |
| 42 |
#define DEFAULT_TIMEOUT 60 |
| 43 |
|
| 44 |
typedef struct dop_client_s |
| 45 |
{ |
| 46 |
int timeout; |
| 47 |
GMainLoop *mainloop; |
| 48 |
int rc; |
| 49 |
} dop_client_t; |
| 50 |
|
| 51 |
static const char *crm_system_name = "drbd-peer-outdater"; |
| 52 |
|
| 53 |
static void usage(const char* cmd, int exit_status); |
| 54 |
|
| 55 |
static void |
| 56 |
dop_exit(dop_client_t *client) |
| 57 |
{ |
| 58 |
int rc; |
| 59 |
|
| 60 |
if (client == NULL) |
| 61 |
exit(5); |
| 62 |
rc = client->rc; |
| 63 |
|
| 64 |
free(client); |
| 65 |
exit(rc); |
| 66 |
} |
| 67 |
|
| 68 |
static gboolean |
| 69 |
outdate_callback(IPC_Channel * server, gpointer user_data) |
| 70 |
{ |
| 71 |
dop_client_t *client = (dop_client_t *)user_data; |
| 72 |
HA_Message *msg = NULL; |
| 73 |
const char *rc_string; |
| 74 |
char *ep; |
| 75 |
int rc; |
| 76 |
|
| 77 |
msg = msgfromIPC_noauth(server); |
| 78 |
if (!msg) { |
| 79 |
cl_log(LOG_WARNING, "no message from server or other " |
| 80 |
"instance is running\n"); |
| 81 |
if (client->mainloop != NULL && |
| 82 |
g_main_is_running(client->mainloop)) |
| 83 |
g_main_quit(client->mainloop); |
| 84 |
return FALSE; |
| 85 |
} |
| 86 |
cl_log(LOG_DEBUG, "message: %s, %s\n", |
| 87 |
ha_msg_value(msg, F_TYPE), |
| 88 |
ha_msg_value(msg, F_ORIG) |
| 89 |
); |
| 90 |
rc_string = ha_msg_value(msg, F_DOPD_VALUE); |
| 91 |
|
| 92 |
errno = 0; |
| 93 |
rc = strtol(rc_string, &ep, 10); |
| 94 |
if (errno != 0 || *ep != EOS) { |
| 95 |
cl_log(LOG_WARNING, "unknown message: %s from server", rc_string); |
| 96 |
client->rc = 20; |
| 97 |
ha_msg_del(msg); |
| 98 |
if (client->mainloop != NULL && |
| 99 |
g_main_is_running(client->mainloop)) |
| 100 |
g_main_quit(client->mainloop); |
| 101 |
return FALSE; |
| 102 |
} |
| 103 |
|
| 104 |
ha_msg_del(msg); |
| 105 |
|
| 106 |
|
| 107 |
client->rc = rc; |
| 108 |
|
| 109 |
if (client->mainloop != NULL && g_main_is_running(client->mainloop)) { |
| 110 |
g_main_quit(client->mainloop); |
| 111 |
} else |
| 112 |
dop_exit(client); |
| 113 |
|
| 114 |
return TRUE; |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
outdater_dispatch_destroy(gpointer user_data) |
| 119 |
{ |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
static gboolean |
| 124 |
outdater_timeout_dispatch(gpointer user_data) |
| 125 |
{ |
| 126 |
dop_client_t *client = (dop_client_t *)user_data; |
| 127 |
cl_log(LOG_WARNING, "error: could not connect to dopd after %i seconds" |
| 128 |
": timeout reached\n", client->timeout); |
| 129 |
if (client->mainloop != NULL && g_main_is_running(client->mainloop)) |
| 130 |
g_main_quit(client->mainloop); |
| 131 |
return FALSE; |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
dopd_connection_destroy(gpointer user_data) |
| 136 |
{ |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
static GCHSource* |
| 141 |
init_dopd_client_ipc_comms(const char *channel_name, |
| 142 |
gboolean (*dispatch)( |
| 143 |
IPC_Channel* source_data, gpointer user_data), |
| 144 |
void *client_data, IPC_Channel **out_ch) |
| 145 |
{ |
| 146 |
IPC_Channel *ch; |
| 147 |
GHashTable *attrs; |
| 148 |
GCHSource *the_source = NULL; |
| 149 |
void *callback_data = client_data; |
| 150 |
static char path[] = IPC_PATH_ATTR; |
| 151 |
|
| 152 |
attrs = g_hash_table_new(g_str_hash,g_str_equal); |
| 153 |
g_hash_table_insert(attrs, path, dopd_socket); |
| 154 |
|
| 155 |
ch = ipc_channel_constructor(IPC_ANYTYPE, attrs); |
| 156 |
g_hash_table_destroy(attrs); |
| 157 |
|
| 158 |
if (ch == NULL) { |
| 159 |
cl_log(LOG_ERR, "Could not access channel on: %s", dopd_socket); |
| 160 |
return NULL; |
| 161 |
} else if (ch->ops->initiate_connection(ch) != IPC_OK) { |
| 162 |
cl_log(LOG_DEBUG, "Could not init comms on: %s", dopd_socket); |
| 163 |
ch->ops->destroy(ch); |
| 164 |
return NULL; |
| 165 |
} |
| 166 |
*out_ch = ch; |
| 167 |
|
| 168 |
the_source = G_main_add_IPC_Channel( |
| 169 |
G_PRIORITY_HIGH, ch, FALSE, dispatch, callback_data, |
| 170 |
dopd_connection_destroy); |
| 171 |
|
| 172 |
return the_source; |
| 173 |
} |
| 174 |
|
| 175 |
int |
| 176 |
main(int argc, char ** argv) |
| 177 |
{ |
| 178 |
HA_Message *update = NULL; |
| 179 |
IPC_Channel *ipc_server = NULL; |
| 180 |
int argerr = 0; |
| 181 |
int flag; |
| 182 |
char *drbd_peer = NULL; |
| 183 |
char *drbd_resource = NULL; |
| 184 |
int timeout = DEFAULT_TIMEOUT; |
| 185 |
int i; |
| 186 |
|
| 187 |
dop_client_t *new_client = NULL; |
| 188 |
|
| 189 |
cl_log_set_entity(crm_system_name); |
| 190 |
cl_log_set_facility(HA_LOG_FACILITY); |
| 191 |
cl_log_set_logd_channel_source(NULL, NULL); |
| 192 |
cl_inherit_logging_environment(500); |
| 193 |
cl_set_corerootdir(HA_COREDIR); |
| 194 |
cl_cdtocoredir(); |
| 195 |
|
| 196 |
while ((flag = getopt(argc, argv, OPTARGS)) != EOF) { |
| 197 |
switch(flag) { |
| 198 |
case 'V': |
| 199 |
debug_level++; |
| 200 |
break; |
| 201 |
case 'h': |
| 202 |
usage(crm_system_name, LSB_EXIT_OK); |
| 203 |
break; |
| 204 |
case 'p': |
| 205 |
drbd_peer = strdup(optarg); |
| 206 |
break; |
| 207 |
case 'r': |
| 208 |
drbd_resource = strdup(optarg); |
| 209 |
break; |
| 210 |
case 't': |
| 211 |
timeout = atoi(optarg); |
| 212 |
break; |
| 213 |
default: |
| 214 |
++argerr; |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
if ((drbd_peer == NULL) && !(drbd_peer = getenv("DRBD_PEER"))) { |
| 222 |
++argerr; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
if ((drbd_resource == NULL) && !(drbd_resource = getenv("DRBD_RESOURCE"))) { |
| 228 |
++argerr; |
| 229 |
} |
| 230 |
|
| 231 |
if (optind > argc) { |
| 232 |
++argerr; |
| 233 |
} |
| 234 |
|
| 235 |
if (argerr) { |
| 236 |
usage(crm_system_name, LSB_EXIT_GENERIC); |
| 237 |
} |
| 238 |
for (i = 0; i < strlen(drbd_peer); i++) |
| 239 |
drbd_peer[i] = tolower(drbd_peer[i]); |
| 240 |
|
| 241 |
|
| 242 |
cl_log(LOG_DEBUG, "drbd peer: %s\n", drbd_peer); |
| 243 |
cl_log(LOG_DEBUG, "drbd resource: %s\n", drbd_resource); |
| 244 |
|
| 245 |
new_client = malloc(sizeof(dop_client_t)); |
| 246 |
memset(new_client, 0, sizeof(dop_client_t)); |
| 247 |
new_client->timeout = timeout; |
| 248 |
new_client->mainloop = g_main_new(FALSE); |
| 249 |
new_client->rc = 5; |
| 250 |
|
| 251 |
|
| 252 |
init_dopd_client_ipc_comms(T_OUTDATER, outdate_callback, |
| 253 |
(gpointer)new_client, &ipc_server); |
| 254 |
|
| 255 |
if (ipc_server == NULL) { |
| 256 |
cl_log(LOG_WARNING, "Could not connect to "T_OUTDATER" channel\n"); |
| 257 |
dop_exit(new_client); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
update = ha_msg_new(3); |
| 262 |
ha_msg_add(update, F_TYPE, T_OUTDATER); |
| 263 |
ha_msg_add(update, F_ORIG, crm_system_name); |
| 264 |
ha_msg_add(update, F_OUTDATER_PEER, drbd_peer); |
| 265 |
ha_msg_add(update, F_OUTDATER_RES, drbd_resource); |
| 266 |
|
| 267 |
if (msg2ipcchan(update, ipc_server) != HA_OK) { |
| 268 |
cl_log(LOG_WARNING, "Could not send message\n"); |
| 269 |
dop_exit(new_client); |
| 270 |
} |
| 271 |
|
| 272 |
Gmain_timeout_add_full(G_PRIORITY_DEFAULT, new_client->timeout * 1000, |
| 273 |
outdater_timeout_dispatch, (gpointer)new_client, |
| 274 |
outdater_dispatch_destroy); |
| 275 |
|
| 276 |
g_main_run(new_client->mainloop); |
| 277 |
dop_exit(new_client); |
| 278 |
return 20; |
| 279 |
} |
| 280 |
|
| 281 |
static void |
| 282 |
usage(const char* cmd, int exit_status) |
| 283 |
{ |
| 284 |
FILE* stream; |
| 285 |
|
| 286 |
stream = exit_status ? stderr : stdout; |
| 287 |
fprintf(stream, "usage: %s -r <string> -p <string> [-t <int>]\n", cmd); |
| 288 |
fprintf(stream, "\t-p <string>\tdrbd peer\n"); |
| 289 |
fprintf(stream, "\t-r <string>\tdrbd resource\n"); |
| 290 |
fprintf(stream, "\t-t <int>\ttimeout in seconds; default: %d\n\n", |
| 291 |
DEFAULT_TIMEOUT); |
| 292 |
fprintf(stream, "The drbd peer and drbd resource have to be specified either on the\n" |
| 293 |
"commandline using the -p and -r options, or using the $DRBD_PEER and\n" |
| 294 |
"$DRBD_RESOURCE environment variables. $DRBD_RESOURCE and $DRBD_PEER\n" |
| 295 |
"will be ignored, if the command line options are used.\n"); |
| 296 |
fflush(stream); |
| 297 |
|
| 298 |
exit(exit_status); |
| 299 |
} |
| 300 |
|