~ubuntu-branches/ubuntu/vivid/ctdb/vivid-proposed

« back to all changes in this revision

Viewing changes to utils/smnotify/smnotify.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathieu Parent
  • Date: 2008-04-26 15:21:27 UTC
  • Revision ID: james.westby@ubuntu.com-20080426152127-58mv5ojv5q362ise
Tags: upstream-1.0.34+git200804242206
ImportĀ upstreamĀ versionĀ 1.0.34+git200804242206

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   simple smnotify tool
 
3
 
 
4
   Copyright (C) Ronnie Sahlberg 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
#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 "smnotify.h"
 
27
#include "../../lib/popt/popt.h"
 
28
 
 
29
static char *client       = NULL;
 
30
static const char *ip     = NULL;
 
31
static char *server = NULL;
 
32
static int stateval       = 0;
 
33
static int clientport     = 0;
 
34
static int sendport       = 0;
 
35
 
 
36
static void useage(void)
 
37
{
 
38
        exit(0);
 
39
}
 
40
 
 
41
static int create_socket(const char *addr, int port)
 
42
{
 
43
        int s;
 
44
        struct sockaddr_in sock_in;
 
45
 
 
46
        s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
 
47
        if (s == -1) {
 
48
                printf("Failed to open local socket\n");
 
49
                exit(10);
 
50
        }
 
51
 
 
52
        bzero(&sock_in, sizeof(sock_in));
 
53
        sock_in.sin_family = PF_INET;
 
54
        sock_in.sin_port   = htons(port);
 
55
        inet_aton(addr, &sock_in.sin_addr);
 
56
        if (bind(s, (struct sockaddr *)&sock_in, sizeof(sock_in)) == -1) {
 
57
                printf("Failed to bind to local socket\n");
 
58
                exit(10);
 
59
        }
 
60
 
 
61
        return s;
 
62
}
 
63
 
 
64
int main(int argc, const char *argv[])
 
65
{
 
66
        struct poptOption popt_options[] = {
 
67
                POPT_AUTOHELP
 
68
                { "client", 'c', POPT_ARG_STRING, &client, 0, "remote client to send the notify to", "hostname/ip" },
 
69
                { "clientport", 0, POPT_ARG_INT, &clientport, 0, "clientport", "integer" },
 
70
                { "ip", 'i', POPT_ARG_STRING, &ip, 0, "local ip address to send the notification from", "ip" },
 
71
                { "sendport", 0, POPT_ARG_INT, &sendport, 0, "port to send the notify from", "integer" },
 
72
                { "server", 's', POPT_ARG_STRING, &server, 0, "servername to use in the notification", "hostname/ip" },
 
73
                { "stateval", 0, POPT_ARG_INT, &stateval, 0, "stateval", "integer" },
 
74
                POPT_TABLEEND
 
75
        };
 
76
        int opt;
 
77
        poptContext pc;
 
78
        CLIENT *clnt;
 
79
        int s;
 
80
        struct sockaddr_in sock_cl;
 
81
        struct timeval w;
 
82
        struct status st;
 
83
 
 
84
        pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
 
85
 
 
86
        while ((opt = poptGetNextOpt(pc)) != -1) {
 
87
                switch (opt) {
 
88
                default:
 
89
                        fprintf(stderr, "Invalid option %s: %s\n", 
 
90
                                poptBadOption(pc, 0), poptStrerror(opt));
 
91
                        exit(1);
 
92
                }
 
93
        }
 
94
 
 
95
        if (client == NULL) {
 
96
                printf("ERROR: client not specified\n");
 
97
                useage();
 
98
        }
 
99
 
 
100
        if (ip == NULL) {
 
101
                printf("ERROR: ip not specified\n");
 
102
                useage();
 
103
        }
 
104
 
 
105
        if (server == NULL) {
 
106
                printf("ERROR: server not specified\n");
 
107
                useage();
 
108
        }
 
109
 
 
110
        if (stateval == 0) {
 
111
                printf("ERROR: stateval not specified\n");
 
112
                useage();
 
113
        }
 
114
 
 
115
 
 
116
        /* Since we want to control from which address these packets are
 
117
           sent we must create the socket ourself and use low-level rpc
 
118
           calls.
 
119
        */
 
120
        s = create_socket(ip, sendport);
 
121
 
 
122
        /* only wait for at most 3 seconds before giving up */
 
123
        alarm(3);
 
124
 
 
125
        /* Setup a sockaddr_in for the client we want to notify */
 
126
        bzero(&sock_cl, sizeof(sock_cl));
 
127
        sock_cl.sin_family = PF_INET;
 
128
        sock_cl.sin_port   = htons(clientport);
 
129
        inet_aton(client, &sock_cl.sin_addr);
 
130
 
 
131
        w.tv_sec = 1;
 
132
        w.tv_usec= 0;
 
133
 
 
134
        clnt = clntudp_create(&sock_cl, 100024, 1, w, &s);
 
135
        if (clnt == NULL) {
 
136
                printf("ERROR: failed to connect to client\n");
 
137
                exit(10);
 
138
        }
 
139
 
 
140
        /* we dont want to wait for any reply */
 
141
        w.tv_sec = 0;
 
142
        w.tv_usec = 0;
 
143
        clnt_control(clnt, CLSET_TIMEOUT, (char *)&w);
 
144
 
 
145
        st.mon_name=server;
 
146
        st.state=stateval;
 
147
        sm_notify_1(&st, clnt);
 
148
 
 
149
        return 0;
 
150
}