Checkpoint commit: refactoring the request code in TIDS for better reuse
[trust_router.git] / mon / mons.c
1 /*
2  * Copyright (c) 2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <talloc.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <netdb.h>
39
40 #include <tr.h>
41 #include <tr_debug.h>
42 #include <mon_internal.h>
43 #include <tr_socket.h>
44 #include <sys/wait.h>
45
46 /**
47  * Allocate a new MONS_INSTANCE
48  *
49  * @param mem_ctx talloc context for allocation
50  * @return new MONS_INSTANCE or null on failure
51  */
52 MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx)
53 {
54   MONS_INSTANCE *mons = talloc(mem_ctx, MONS_INSTANCE);
55
56   if (mons) {
57     mons->port = 0;
58     mons->req_handler = NULL;
59     mons->auth_handler = NULL;
60     mons->cookie = NULL;
61     mons->authorized_gss_names = tr_gss_names_new(mons);
62     if (mons->authorized_gss_names == NULL) {
63       talloc_free(mons);
64       mons = NULL;
65     }
66   }
67   return mons;
68 }
69
70 /**
71  * Create a listener for monitoring requests
72  *
73  * Accept connections with mons_accept()
74  *
75  * @param mons monitoring server instance
76  * @param req_handler
77  * @param auth_handler
78  * @param hostname
79  * @param port
80  * @param cookie
81  * @param fd_out
82  * @param max_fd
83  * @return
84  */
85 int mons_get_listener(MONS_INSTANCE *mons,
86                       MONS_REQ_FUNC *req_handler,
87                       MONS_AUTH_FUNC *auth_handler,
88                       unsigned int port,
89                       void *cookie,
90                       int *fd_out,
91                       size_t max_fd)
92 {
93   size_t n_fd=0;
94   size_t ii=0;
95
96   mons->port = port;
97   n_fd = tr_sock_listen_all(port, fd_out, max_fd);
98   if (n_fd<=0)
99     tr_err("mons_get_listener: Error opening port %d");
100   else {
101     /* opening port succeeded */
102     tr_info("mons_get_listener: Opened port %d.", port);
103
104     /* make this socket non-blocking */
105     for (ii=0; ii<n_fd; ii++) {
106       if (0 != fcntl(fd_out[ii], F_SETFL, O_NONBLOCK)) {
107         tr_err("mons_get_listener: Error setting O_NONBLOCK.");
108         for (ii=0; ii<n_fd; ii++) {
109           close(fd_out[ii]);
110           fd_out[ii]=-1;
111         }
112         n_fd=0;
113         break;
114       }
115     }
116   }
117
118   if (n_fd>0) {
119     /* store the caller's request handler & cookie */
120     mons->req_handler = req_handler;
121     mons->auth_handler = auth_handler;
122     mons->cookie = cookie;
123   }
124
125   return (int) n_fd;
126 }
127
128 /**
129  * Accept and process a connection on a port opened with mons_get_listener()
130  *
131  * @param mons monitoring interface instance
132  * @param listen FD of the connection socket
133  * @return 0 on success
134  */
135 int mons_accept(MONS_INSTANCE *mons, int listen)
136 {
137   int conn=-1;
138   int pid=-1;
139
140   if (0 > (conn = accept(listen, NULL, NULL))) {
141     perror("Error from monitoring interface accept()");
142     return 1;
143   }
144
145   if (0 > (pid = fork())) {
146     perror("Error on fork()");
147     return 1;
148   }
149
150   if (pid == 0) {
151     close(listen);
152     mons_handle_connection(mons, conn);
153     close(conn);
154     exit(0); /* exit to kill forked child process */
155   } else {
156     close(conn);
157   }
158
159   /* clean up any processes that have completed */
160   while (waitpid(-1, 0, WNOHANG) > 0);
161
162   return 0;
163 }