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