1 1.9 sevan /* $NetBSD: stat_proc.c,v 1.9 2018/01/23 21:06:26 sevan Exp $ */ 2 1.1 scottr 3 1.1 scottr /* 4 1.1 scottr * Copyright (c) 1995 5 1.1 scottr * A.R. Gordon (andrew.gordon (at) net-tel.co.uk). All rights reserved. 6 1.1 scottr * 7 1.1 scottr * Redistribution and use in source and binary forms, with or without 8 1.1 scottr * modification, are permitted provided that the following conditions 9 1.1 scottr * are met: 10 1.1 scottr * 1. Redistributions of source code must retain the above copyright 11 1.1 scottr * notice, this list of conditions and the following disclaimer. 12 1.1 scottr * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 scottr * notice, this list of conditions and the following disclaimer in the 14 1.1 scottr * documentation and/or other materials provided with the distribution. 15 1.1 scottr * 3. All advertising materials mentioning features or use of this software 16 1.1 scottr * must display the following acknowledgement: 17 1.1 scottr * This product includes software developed for the FreeBSD project 18 1.1 scottr * 4. Neither the name of the author nor the names of any co-contributors 19 1.1 scottr * may be used to endorse or promote products derived from this software 20 1.1 scottr * without specific prior written permission. 21 1.1 scottr * 22 1.1 scottr * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND 23 1.1 scottr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 scottr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 scottr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 1.1 scottr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 scottr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 scottr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 scottr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 scottr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 scottr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 scottr * SUCH DAMAGE. 33 1.1 scottr * 34 1.1 scottr */ 35 1.2 lukem 36 1.2 lukem #include <sys/cdefs.h> 37 1.2 lukem #ifndef lint 38 1.9 sevan __RCSID("$NetBSD: stat_proc.c,v 1.9 2018/01/23 21:06:26 sevan Exp $"); 39 1.2 lukem #endif 40 1.1 scottr 41 1.3 lukem #include <errno.h> 42 1.3 lukem #include <netdb.h> 43 1.1 scottr #include <stdio.h> 44 1.1 scottr #include <stdlib.h> 45 1.1 scottr #include <string.h> 46 1.3 lukem #include <syslog.h> 47 1.4 christos #include <signal.h> 48 1.3 lukem #include <unistd.h> 49 1.3 lukem 50 1.1 scottr #include <rpc/rpc.h> 51 1.1 scottr 52 1.1 scottr #include "statd.h" 53 1.1 scottr 54 1.1 scottr /* sm_stat_1 --------------------------------------------------------------- */ 55 1.1 scottr /* 56 1.1 scottr * Purpose: RPC call to enquire if a host can be monitored 57 1.1 scottr * Returns: TRUE for any hostname that can be looked up to give 58 1.1 scottr * an address. 59 1.1 scottr */ 60 1.1 scottr struct sm_stat_res * 61 1.9 sevan sm_stat_1_svc(sm_name *arg, struct svc_req *req) 62 1.1 scottr { 63 1.8 lukem static sm_stat_res smres; 64 1.6 fvdl struct addrinfo *ai; 65 1.1 scottr 66 1.4 christos NO_ALARM; 67 1.1 scottr if (debug) 68 1.1 scottr syslog(LOG_DEBUG, "stat called for host %s", arg->mon_name); 69 1.1 scottr 70 1.6 fvdl if (getaddrinfo(arg->mon_name, NULL, NULL, &ai) == 0) { 71 1.8 lukem smres.res_stat = stat_succ; 72 1.6 fvdl freeaddrinfo(ai); 73 1.6 fvdl } else { 74 1.1 scottr syslog(LOG_ERR, "invalid hostname to sm_stat: %s", 75 1.1 scottr arg->mon_name); 76 1.8 lukem smres.res_stat = stat_fail; 77 1.1 scottr } 78 1.1 scottr 79 1.8 lukem smres.state = status_info.ourState; 80 1.4 christos ALARM; 81 1.8 lukem return (&smres); 82 1.1 scottr } 83 1.1 scottr 84 1.1 scottr /* sm_mon_1 ---------------------------------------------------------------- */ 85 1.1 scottr /* 86 1.1 scottr * Purpose: RPC procedure to establish a monitor request 87 1.1 scottr * Returns: Success, unless lack of resources prevents 88 1.1 scottr * the necessary structures from being set up 89 1.1 scottr * to record the request, or if the hostname is not 90 1.1 scottr * valid (as judged by gethostbyname()) 91 1.1 scottr */ 92 1.1 scottr struct sm_stat_res * 93 1.9 sevan sm_mon_1_svc(mon *arg, struct svc_req *req) 94 1.1 scottr { 95 1.8 lukem static sm_stat_res smres; 96 1.6 fvdl struct addrinfo *ai; 97 1.4 christos HostInfo *hp, h; 98 1.1 scottr MonList *lp; 99 1.1 scottr 100 1.4 christos NO_ALARM; 101 1.1 scottr if (debug) { 102 1.1 scottr syslog(LOG_DEBUG, "monitor request for host %s", 103 1.1 scottr arg->mon_id.mon_name); 104 1.1 scottr syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d", 105 1.3 lukem arg->mon_id.my_id.my_name, arg->mon_id.my_id.my_prog, 106 1.3 lukem arg->mon_id.my_id.my_vers, arg->mon_id.my_id.my_proc); 107 1.1 scottr } 108 1.8 lukem smres.res_stat = stat_fail; /* Assume fail until set otherwise */ 109 1.8 lukem smres.state = status_info.ourState; 110 1.1 scottr 111 1.1 scottr /* 112 1.1 scottr * Find existing host entry, or create one if not found. If 113 1.1 scottr * find_host() fails, it will have logged the error already. 114 1.1 scottr */ 115 1.6 fvdl if (getaddrinfo(arg->mon_id.mon_name, NULL, NULL, &ai) != 0) { 116 1.1 scottr syslog(LOG_ERR, "Invalid hostname to sm_mon: %s", 117 1.1 scottr arg->mon_id.mon_name); 118 1.8 lukem return &smres; 119 1.4 christos } 120 1.6 fvdl 121 1.6 fvdl freeaddrinfo(ai); 122 1.1 scottr 123 1.4 christos if ((hp = find_host(arg->mon_id.mon_name, &h)) == NULL) 124 1.4 christos memset(hp = &h, 0, sizeof(h)); 125 1.1 scottr 126 1.4 christos lp = (MonList *)malloc(sizeof(MonList)); 127 1.4 christos if (!lp) 128 1.4 christos syslog(LOG_ERR, "Out of memory"); 129 1.4 christos else { 130 1.4 christos strncpy(lp->notifyHost, arg->mon_id.my_id.my_name, 131 1.4 christos SM_MAXSTRLEN); 132 1.4 christos lp->notifyProg = arg->mon_id.my_id.my_prog; 133 1.4 christos lp->notifyVers = arg->mon_id.my_id.my_vers; 134 1.4 christos lp->notifyProc = arg->mon_id.my_id.my_proc; 135 1.4 christos memcpy(lp->notifyData, arg->priv, 136 1.4 christos sizeof(lp->notifyData)); 137 1.4 christos 138 1.4 christos lp->next = hp->monList; 139 1.4 christos hp->monList = lp; 140 1.4 christos change_host(arg->mon_id.mon_name, hp); 141 1.4 christos sync_file(); 142 1.8 lukem smres.res_stat = stat_succ; /* Report success */ 143 1.1 scottr } 144 1.4 christos ALARM; 145 1.8 lukem return (&smres); 146 1.1 scottr } 147 1.1 scottr 148 1.1 scottr /* do_unmon ---------------------------------------------------------------- */ 149 1.1 scottr /* 150 1.1 scottr * Purpose: Remove a monitor request from a host 151 1.1 scottr * Returns: TRUE if found, FALSE if not found. 152 1.1 scottr * Notes: Common code from sm_unmon_1_svc and sm_unmon_all_1_svc 153 1.1 scottr * In the unlikely event of more than one identical monitor 154 1.1 scottr * request, all are removed. 155 1.1 scottr */ 156 1.4 christos int 157 1.9 sevan do_unmon(char *name, HostInfo *hp, void *ptr) 158 1.1 scottr { 159 1.4 christos my_id *idp = ptr; 160 1.1 scottr MonList *lp, *next; 161 1.1 scottr MonList *last = NULL; 162 1.1 scottr int result = FALSE; 163 1.1 scottr 164 1.1 scottr lp = hp->monList; 165 1.1 scottr while (lp) { 166 1.1 scottr if (!strncasecmp(idp->my_name, lp->notifyHost, SM_MAXSTRLEN) 167 1.1 scottr && (idp->my_prog == lp->notifyProg) 168 1.1 scottr && (idp->my_proc == lp->notifyProc) 169 1.1 scottr && (idp->my_vers == lp->notifyVers)) { 170 1.1 scottr /* found one. Unhook from chain and free. */ 171 1.1 scottr next = lp->next; 172 1.1 scottr if (last) 173 1.1 scottr last->next = next; 174 1.1 scottr else 175 1.1 scottr hp->monList = next; 176 1.1 scottr free(lp); 177 1.1 scottr lp = next; 178 1.1 scottr result = TRUE; 179 1.1 scottr } else { 180 1.1 scottr last = lp; 181 1.1 scottr lp = lp->next; 182 1.1 scottr } 183 1.1 scottr } 184 1.1 scottr return (result); 185 1.1 scottr } 186 1.1 scottr 187 1.1 scottr /* sm_unmon_1 -------------------------------------------------------------- */ 188 1.1 scottr /* 189 1.1 scottr * Purpose: RPC procedure to release a monitor request. 190 1.1 scottr * Returns: Local machine's status number 191 1.1 scottr * Notes: The supplied mon_id should match the value passed in an 192 1.1 scottr * earlier call to sm_mon_1 193 1.1 scottr */ 194 1.1 scottr struct sm_stat * 195 1.9 sevan sm_unmon_1_svc(mon_id *arg, struct svc_req *req) 196 1.1 scottr { 197 1.8 lukem static sm_stat smres; 198 1.4 christos HostInfo *hp, h; 199 1.1 scottr 200 1.4 christos NO_ALARM; 201 1.1 scottr if (debug) { 202 1.1 scottr syslog(LOG_DEBUG, "un-monitor request for host %s", 203 1.1 scottr arg->mon_name); 204 1.1 scottr syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d", 205 1.3 lukem arg->my_id.my_name, arg->my_id.my_prog, 206 1.1 scottr arg->my_id.my_vers, arg->my_id.my_proc); 207 1.1 scottr } 208 1.4 christos if ((hp = find_host(arg->mon_name, &h)) != NULL) { 209 1.4 christos if (do_unmon(arg->mon_name, hp, &arg->my_id)) { 210 1.4 christos change_host(arg->mon_name, hp); 211 1.1 scottr sync_file(); 212 1.4 christos } 213 1.1 scottr else 214 1.1 scottr syslog(LOG_ERR, 215 1.1 scottr "unmon request from %s, no matching monitor", 216 1.1 scottr arg->my_id.my_name); 217 1.1 scottr } else 218 1.1 scottr syslog(LOG_ERR, "unmon request from %s for unknown host %s", 219 1.1 scottr arg->my_id.my_name, arg->mon_name); 220 1.1 scottr 221 1.8 lukem smres.state = status_info.ourState; 222 1.4 christos ALARM; 223 1.1 scottr 224 1.8 lukem return (&smres); 225 1.1 scottr } 226 1.1 scottr 227 1.1 scottr /* sm_unmon_all_1 ---------------------------------------------------------- */ 228 1.1 scottr /* 229 1.1 scottr * Purpose: RPC procedure to release monitor requests. 230 1.1 scottr * Returns: Local machine's status number 231 1.1 scottr * Notes: Releases all monitor requests (if any) from the specified 232 1.1 scottr * host and program number. 233 1.1 scottr */ 234 1.1 scottr struct sm_stat * 235 1.9 sevan sm_unmon_all_1_svc(my_id *arg, struct svc_req *req) 236 1.1 scottr { 237 1.8 lukem static sm_stat smres; 238 1.1 scottr 239 1.4 christos NO_ALARM; 240 1.1 scottr if (debug) { 241 1.1 scottr syslog(LOG_DEBUG, 242 1.1 scottr "unmon_all for host: %s prog: %d ver: %d proc: %d", 243 1.1 scottr arg->my_name, arg->my_prog, arg->my_vers, arg->my_proc); 244 1.1 scottr } 245 1.1 scottr 246 1.4 christos unmon_hosts(); 247 1.1 scottr sync_file(); 248 1.1 scottr 249 1.8 lukem smres.state = status_info.ourState; 250 1.4 christos ALARM; 251 1.1 scottr 252 1.8 lukem return (&smres); 253 1.1 scottr } 254 1.1 scottr 255 1.1 scottr /* sm_simu_crash_1 --------------------------------------------------------- */ 256 1.1 scottr /* 257 1.1 scottr * Purpose: RPC procedure to simulate a crash 258 1.1 scottr * Returns: Nothing 259 1.1 scottr * Notes: Standardised mechanism for debug purposes 260 1.1 scottr * The specification says that we should drop all of our 261 1.1 scottr * status information (apart from the list of monitored hosts 262 1.1 scottr * on disc). However, this would confuse the rpc.lockd 263 1.1 scottr * which would be unaware that all of its monitor requests 264 1.1 scottr * had been silently junked. Hence we in fact retain all 265 1.1 scottr * current requests and simply increment the status counter 266 1.1 scottr * and inform all hosts on the monitor list. 267 1.1 scottr */ 268 1.1 scottr void * 269 1.9 sevan sm_simu_crash_1_svc(void *v, struct svc_req *req) 270 1.1 scottr { 271 1.1 scottr static char dummy; 272 1.1 scottr 273 1.4 christos NO_ALARM; 274 1.1 scottr if (debug) 275 1.1 scottr syslog(LOG_DEBUG, "simu_crash called!!"); 276 1.1 scottr 277 1.4 christos reset_database(); 278 1.4 christos ALARM; 279 1.4 christos notify_handler(0); 280 1.1 scottr 281 1.1 scottr return (&dummy); 282 1.1 scottr } 283 1.1 scottr 284 1.1 scottr /* sm_notify_1 ------------------------------------------------------------- */ 285 1.1 scottr /* 286 1.1 scottr * Purpose: RPC procedure notifying local statd of the crash of another 287 1.1 scottr * Returns: Nothing 288 1.1 scottr * Notes: There is danger of deadlock, since it is quite likely that 289 1.1 scottr * the client procedure that we call will in turn call us 290 1.1 scottr * to remove or adjust the monitor request. 291 1.1 scottr * We therefore fork() a process to do the notifications. 292 1.1 scottr * Note that the main HostInfo structure is in a mmap() 293 1.1 scottr * region and so will be shared with the child, but the 294 1.1 scottr * monList pointed to by the HostInfo is in normal memory. 295 1.1 scottr * Hence if we read the monList before forking, we are 296 1.1 scottr * protected from the parent servicing other requests 297 1.1 scottr * that modify the list. 298 1.1 scottr */ 299 1.1 scottr void * 300 1.9 sevan sm_notify_1_svc(stat_chge *arg, struct svc_req *req) 301 1.1 scottr { 302 1.1 scottr struct timeval timeout = {20, 0}; /* 20 secs timeout */ 303 1.1 scottr CLIENT *cli; 304 1.1 scottr static char dummy; 305 1.1 scottr status tx_arg; /* arg sent to callback procedure */ 306 1.1 scottr MonList *lp; 307 1.4 christos HostInfo *hp, h; 308 1.1 scottr pid_t pid; 309 1.1 scottr 310 1.1 scottr if (debug) 311 1.1 scottr syslog(LOG_DEBUG, "notify from host %s, new state %d", 312 1.1 scottr arg->mon_name, arg->state); 313 1.1 scottr 314 1.4 christos hp = find_host(arg->mon_name, &h); 315 1.1 scottr if (!hp) { 316 1.1 scottr /* Never heard of this host - why is it notifying us? */ 317 1.5 bouyer syslog(LOG_DEBUG, "Unsolicited notification from host %s", 318 1.1 scottr arg->mon_name); 319 1.5 bouyer return (&dummy); 320 1.1 scottr } 321 1.1 scottr lp = hp->monList; 322 1.1 scottr if (!lp) /* We know this host, but have no outstanding requests. */ 323 1.5 bouyer return (&dummy); 324 1.1 scottr 325 1.7 bouyer sync_file(); 326 1.1 scottr pid = fork(); 327 1.1 scottr if (pid == -1) { 328 1.1 scottr syslog(LOG_ERR, "Unable to fork notify process - %s", 329 1.1 scottr strerror(errno)); 330 1.3 lukem return (FALSE); 331 1.1 scottr } 332 1.1 scottr if (pid) 333 1.1 scottr return (&dummy); /* Parent returns */ 334 1.1 scottr 335 1.1 scottr while (lp) { 336 1.1 scottr tx_arg.mon_name = arg->mon_name; 337 1.1 scottr tx_arg.state = arg->state; 338 1.1 scottr memcpy(tx_arg.priv, lp->notifyData, sizeof(tx_arg.priv)); 339 1.1 scottr cli = clnt_create(lp->notifyHost, lp->notifyProg, 340 1.1 scottr lp->notifyVers, "udp"); 341 1.1 scottr if (!cli) 342 1.1 scottr syslog(LOG_ERR, "Failed to contact host %s%s", 343 1.1 scottr lp->notifyHost, clnt_spcreateerror("")); 344 1.1 scottr else { 345 1.1 scottr if (clnt_call(cli, lp->notifyProc, xdr_status, &tx_arg, 346 1.1 scottr xdr_void, &dummy, timeout) != RPC_SUCCESS) 347 1.1 scottr syslog(LOG_ERR, 348 1.1 scottr "Failed to call rpc.statd client at host %s", 349 1.1 scottr lp->notifyHost); 350 1.1 scottr clnt_destroy(cli); 351 1.1 scottr } 352 1.1 scottr lp = lp->next; 353 1.1 scottr } 354 1.1 scottr 355 1.1 scottr exit(0); /* Child quits */ 356 1.1 scottr } 357