messaging: fix net command failure due to unhandled return code
[Samba.git] / ctdb / utils / smnotify / smnotify.c
blob43ca7f37c6ffc0577d72e4f6814aa1fbd8bb72dd
1 /*
2 simple smnotify tool
4 Copyright (C) Ronnie Sahlberg 2007
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.
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.
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/>.
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <stdlib.h>
27 #include "smnotify.h"
28 #include "popt.h"
30 static char *client = NULL;
31 static const char *ip = NULL;
32 static char *server = NULL;
33 static int stateval = 0;
34 static int clientport = 0;
35 static int sendport = 0;
37 static void useage(void)
39 exit(0);
42 static int create_socket(const char *addr, int port)
44 int s;
45 struct sockaddr_in sock_in;
47 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
48 if (s == -1) {
49 printf("Failed to open local socket\n");
50 exit(10);
53 bzero(&sock_in, sizeof(sock_in));
54 sock_in.sin_family = AF_INET;
55 sock_in.sin_port = htons(port);
56 inet_aton(addr, &sock_in.sin_addr);
57 if (bind(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) == -1) {
58 printf("Failed to bind to local socket\n");
59 exit(10);
62 return s;
65 int main(int argc, const char *argv[])
67 struct poptOption popt_options[] = {
68 POPT_AUTOHELP
69 { "client", 'c', POPT_ARG_STRING, &client, 0, "remote client to send the notify to", "hostname/ip" },
70 { "clientport", 0, POPT_ARG_INT, &clientport, 0, "clientport", "integer" },
71 { "ip", 'i', POPT_ARG_STRING, &ip, 0, "local ip address to send the notification from", "ip" },
72 { "sendport", 0, POPT_ARG_INT, &sendport, 0, "port to send the notify from", "integer" },
73 { "server", 's', POPT_ARG_STRING, &server, 0, "servername to use in the notification", "hostname/ip" },
74 { "stateval", 0, POPT_ARG_INT, &stateval, 0, "stateval", "integer" },
75 POPT_TABLEEND
77 int opt;
78 poptContext pc;
79 CLIENT *clnt;
80 int s;
81 struct sockaddr_in sock_cl;
82 struct timeval w;
83 struct status st;
85 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
87 while ((opt = poptGetNextOpt(pc)) != -1) {
88 switch (opt) {
89 default:
90 fprintf(stderr, "Invalid option %s: %s\n",
91 poptBadOption(pc, 0), poptStrerror(opt));
92 exit(1);
96 if (client == NULL) {
97 printf("ERROR: client not specified\n");
98 useage();
101 if (ip == NULL) {
102 printf("ERROR: ip not specified\n");
103 useage();
106 if (server == NULL) {
107 printf("ERROR: server not specified\n");
108 useage();
111 if (stateval == 0) {
112 printf("ERROR: stateval not specified\n");
113 useage();
117 /* Since we want to control from which address these packets are
118 sent we must create the socket ourself and use low-level rpc
119 calls.
121 s = create_socket(ip, sendport);
123 /* only wait for at most 3 seconds before giving up */
124 alarm(3);
126 /* Setup a sockaddr_in for the client we want to notify */
127 bzero(&sock_cl, sizeof(sock_cl));
128 sock_cl.sin_family = AF_INET;
129 sock_cl.sin_port = htons(clientport);
130 inet_aton(client, &sock_cl.sin_addr);
132 w.tv_sec = 1;
133 w.tv_usec= 0;
135 clnt = clntudp_create(&sock_cl, 100024, 1, w, &s);
136 if (clnt == NULL) {
137 printf("ERROR: failed to connect to client\n");
138 exit(10);
141 /* we don't want to wait for any reply */
142 w.tv_sec = 0;
143 w.tv_usec = 0;
144 clnt_control(clnt, CLSET_TIMEOUT, (char *)&w);
146 st.mon_name=server;
147 st.state=stateval;
148 sm_notify_1(&st, clnt);
150 return 0;