summaryrefslogtreecommitdiffstats
path: root/utils/mirror_vfd/mirror_server_stop.c
blob: bf306d9f76279f02c46283f11f32536e03fe8c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose:     Stop the mirror server
 *              Exists for cross-platform, optionally remote shutdown.
 */

#include "H5private.h" /* System compatability call-wrapper macros */

#ifdef H5_HAVE_MIRROR_VFD

#define MSHS_OPTS_MAGIC     0x613B1C15u /* sanity-checking constant */
#define MSHS_IP_STR_SIZE    20
#define MSHS_DEFAULT_IP     "127.0.0.1"
#define MSHS_DEFAULT_PORTNO 3000

/* ----------------------------------------------------------------------------
 * Structure:   struct mshs_opts
 *
 * Purpose:     Convenience structure to hold options as parsed from the
 *              command line.
 *
 * `magic` (uint32_t)
 *      Semi-unique constant to help verify pointer integrity.
 *
 * `help` (int)
 *      Flag that the help argument was present.
 *
 * `portno` (int)
 *      Port number, as received from arguments.
 *
 * `ip` (char *)
 *      IP address string as received from arguments.
 *
 * ----------------------------------------------------------------------------
 */
struct mshs_opts {
    uint32_t magic;
    int      help;
    int      portno;
    char     ip[MSHS_IP_STR_SIZE + 1];
};

/* ----------------------------------------------------------------------------
 * Function:    usage
 *
 * Purpose:     Print usage message to stdout.
 * ----------------------------------------------------------------------------
 */
static void
usage(void)
{
    HDprintf("mirror_server_halten_sie [options]\n"
             "System-independent Mirror Server shutdown program.\n"
             "Sends shutdown message to Mirror Server at given IP:port\n"
             "\n"
             "Options:\n"
             "    -h | --help Print this usage message and exit.\n"
             "    --ip=ADDR   IP Address of remote server (defaut %s)\n"
             "    --port=PORT Handshake port of remote server (default %d)\n",
             MSHS_DEFAULT_IP, MSHS_DEFAULT_PORTNO);
} /* end usage() */

/* ----------------------------------------------------------------------------
 * Function:    parse_args
 *
 * Purpose:     Parse command-line arguments, populating the options struct
 *              pointer as appropriate.
 *              Default values will be set for unspecified options.
 *
 * Return:      0 on success, negative (-1) if error.
 * ----------------------------------------------------------------------------
 */
static int
parse_args(int argc, char **argv, struct mshs_opts *opts)
{
    int i = 0;

    opts->magic  = MSHS_OPTS_MAGIC;
    opts->help   = 0;
    opts->portno = MSHS_DEFAULT_PORTNO;
    HDstrncpy(opts->ip, MSHS_DEFAULT_IP, MSHS_IP_STR_SIZE);

    for (i = 1; i < argc; i++) { /* start with first possible option argument */
        if (!HDstrncmp(argv[i], "-h", 3) || !HDstrncmp(argv[i], "--help", 7)) {
            opts->help = 1;
        }
        else if (!HDstrncmp(argv[i], "--ip=", 5)) {
            HDstrncpy(opts->ip, argv[i] + 5, MSHS_IP_STR_SIZE);
        }
        else if (!HDstrncmp(argv[i], "--port=", 7)) {
            opts->portno = HDatoi(argv[i] + 7);
        }
        else {
            HDprintf("Unrecognized option: '%s'\n", argv[i]);
            usage();
            opts->magic++; /* invalidate for sanity */
            return -1;
        }
    } /* end for each argument from command line */

    /* auto-replace 'localhost' with numeric IP */
    if (!HDstrncmp(opts->ip, "localhost", 10)) { /* include null terminator */
        HDstrncpy(opts->ip, "127.0.0.1", MSHS_IP_STR_SIZE);
    }

    return 0;
} /* end parse_args() */

/* ----------------------------------------------------------------------------
 * Function:    send_shutdown
 *
 * Purpose:     Create socket and send shutdown signal to remote server.
 *
 * Return:      0 on success, negative (-1) if error.
 * ----------------------------------------------------------------------------
 */
static int
send_shutdown(struct mshs_opts *opts)
{
    int                live_socket;
    struct sockaddr_in target_addr;

    if (opts->magic != MSHS_OPTS_MAGIC) {
        HDprintf("invalid options structure\n");
        return -1;
    }

    live_socket = HDsocket(AF_INET, SOCK_STREAM, 0);
    if (live_socket < 0) {
        HDprintf("ERROR socket()\n");
        return -1;
    }

    target_addr.sin_family      = AF_INET;
    target_addr.sin_port        = HDhtons((uint16_t)opts->portno);
    target_addr.sin_addr.s_addr = HDinet_addr(opts->ip);
    HDmemset(target_addr.sin_zero, '\0', sizeof(target_addr.sin_zero));

    if (HDconnect(live_socket, (struct sockaddr *)&target_addr, (socklen_t)sizeof(target_addr)) < 0) {
        HDprintf("ERROR connect() (%d)\n%s\n", errno, HDstrerror(errno));
        return -1;
    }

    if (HDwrite(live_socket, "SHUTDOWN", 9) == -1) {
        HDprintf("ERROR write() (%d)\n%s\n", errno, HDstrerror(errno));
        return -1;
    }

    if (HDclose(live_socket) < 0) {
        HDprintf("ERROR close() can't close socket\n");
        return -1;
    }

    return 0;
} /* end send_shutdown() */

/* ------------------------------------------------------------------------- */
int
main(int argc, char **argv)
{
    struct mshs_opts opts;

    if (parse_args(argc, argv, &opts) < 0) {
        HDprintf("Unable to parse arguments\n");
        HDexit(EXIT_FAILURE);
    }

    if (opts.help) {
        usage();
        HDexit(EXIT_FAILURE);
    }

    if (send_shutdown(&opts) < 0) {
        HDprintf("Unable to send shutdown command\n");
        HDexit(EXIT_FAILURE);
    }

    HDexit(EXIT_SUCCESS);
} /* end main() */

#else /* H5_HAVE_MIRROR_VFD */

/* ------------------------------------------------------------------------- */
int
main(void)
{
    HDprintf("Mirror VFD not built -- unable to perform shutdown.\n");
    HDexit(EXIT_FAILURE);
}

#endif /* H5_HAVE_MIRROR_VFD */