1 /* $NetBSD: am_glue.c,v 1.3 2010/02/26 22:44:17 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 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 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __RCSID("$NetBSD: am_glue.c,v 1.3 2010/02/26 22:44:17 christos Exp $"); 32 #endif /* not lint */ 33 34 #ifdef HAVE_CONFIG_H 35 # include <config.h> 36 #endif /* HAVE_CONFIG_H */ 37 #include <am_defs.h> 38 #include <amu.h> 39 #include <rpc/pmap_prot.h> 40 #include <rpc/pmap_clnt.h> 41 42 #include "am_glue.h" 43 44 static CLIENT *clnt; 45 46 static struct timeval tv = { 5, 0 }; 47 /* 48 * Appease lint: Properly typecast some numbers defined in 49 * src/extern/bsd/am-utils/dist/include/amq_defs.h. 50 */ 51 #define xAMQ_PROGRAM (rpcprog_t)AMQ_PROGRAM 52 #define xAMQ_VERSION (rpcvers_t)AMQ_VERSION 53 #define xAMQPROC_SYNC_UMNT (rpcproc_t)AMQPROC_SYNC_UMNT 54 55 static int 56 ping_pmap(void) 57 { 58 u_short port = 0; 59 CLIENT *cl; 60 struct pmap parms; 61 struct sockaddr_in si; 62 int s = -1, rv; 63 static struct timeval pingtv = { 1, 0 }; 64 65 (void)memset(&si, 0, sizeof(si)); 66 si.sin_family = AF_INET; 67 si.sin_len = sizeof(si); 68 si.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 69 si.sin_port = htons(PMAPPORT); 70 71 if ((cl = clntudp_bufcreate(&si, PMAPPROG, PMAPVERS, pingtv, 72 &s, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE)) == NULL) 73 return -1; 74 75 parms.pm_prog = PMAPPROG; 76 parms.pm_vers = PMAPVERS; 77 parms.pm_prot = IPPROTO_UDP; 78 parms.pm_port = 0; /* not needed or used */ 79 80 rv = CLNT_CALL(cl, (rpcproc_t)PMAPPROC_GETPORT, 81 (xdrproc_t)xdr_pmap, &parms, 82 (xdrproc_t)xdr_u_short, &port, pingtv) == RPC_SUCCESS ? 0 : -1; 83 84 CLNT_DESTROY(cl); 85 return rv; 86 } 87 88 void 89 am_init(void) 90 { 91 static const char *server = "localhost"; 92 93 if (ping_pmap() == -1) 94 return; 95 /* 96 * Create RPC endpoint 97 */ 98 /* try tcp first */ 99 clnt = clnt_create(server, xAMQ_PROGRAM, xAMQ_VERSION, "tcp"); 100 if (clnt != NULL) 101 return; 102 103 /* try udp next */ 104 clnt = clnt_create(server, xAMQ_PROGRAM, xAMQ_VERSION, "udp"); 105 if (clnt != NULL) /* set udp timeout */ 106 (void)clnt_control(clnt, CLSET_RETRY_TIMEOUT, (void *)&tv); 107 } 108 109 int 110 am_unmount(const char *dirname) 111 { 112 static struct timeval timeout = { ALLOWED_MOUNT_TIME, 0 }; 113 amq_sync_umnt result; 114 115 if (clnt == NULL) 116 return AMQ_UMNT_FAILED; 117 118 if (clnt_call(clnt, xAMQPROC_SYNC_UMNT, xdr_amq_string, &dirname, 119 xdr_amq_sync_umnt, (void *)&result, timeout) != RPC_SUCCESS) 120 return AMQ_UMNT_SERVER; 121 122 return result.au_etype; 123 } 124