| 1 |
/***************************************************************************** |
| 2 |
* Copyright (C) 2010-14 Lawrence Livermore National Security, LLC. |
| 3 |
* Written by Jim Garlick <[email protected]> LLNL-CODE-423279 |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 6 |
* This file is part of the Distributed I/O Daemon (diod). |
| 7 |
* For details, see http://code.google.com/p/diod. |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU General Public License as published by the Free |
| 11 |
* Software Foundation; either version 2 of the license, or (at your option) |
| 12 |
* any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 |
* for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License along |
| 20 |
* with this program; if not, write to the Free Software Foundation, |
| 21 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 |
* See also: http://www.gnu.org/licenses |
| 23 |
*****************************************************************************/ |
| 24 |
|
| 25 |
/* diodshowmount.c - cat ctl:connections */ |
| 26 |
|
| 27 |
#if HAVE_CONFIG_H |
| 28 |
#include "config.h" |
| 29 |
#endif |
| 30 |
#include <sys/types.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <fcntl.h> |
| 33 |
#include <unistd.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdint.h> |
| 36 |
#include <stdarg.h> |
| 37 |
#include <stdio.h> |
| 38 |
#if HAVE_GETOPT_H |
| 39 |
#include <getopt.h> |
| 40 |
#endif |
| 41 |
#include <string.h> |
| 42 |
#include <errno.h> |
| 43 |
#include <ctype.h> |
| 44 |
#include <libgen.h> |
| 45 |
#include <signal.h> |
| 46 |
|
| 47 |
#include "9p.h" |
| 48 |
#include "npfs.h" |
| 49 |
#include "npclient.h" |
| 50 |
|
| 51 |
#include "list.h" |
| 52 |
#include "hostlist.h" |
| 53 |
#include "diod_log.h" |
| 54 |
#include "diod_sock.h" |
| 55 |
#include "diod_auth.h" |
| 56 |
|
| 57 |
#define OPTIONS "s:m:u:t:l" |
| 58 |
#if HAVE_GETOPT_LONG |
| 59 |
#define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL) |
| 60 |
static const struct option longopts[] = { |
| 61 |
{"server", required_argument, 0, 's'}, |
| 62 |
{"msize", required_argument, 0, 'm'}, |
| 63 |
{"uid", required_argument, 0, 'u'}, |
| 64 |
{"timeout", required_argument, 0, 't'}, |
| 65 |
{"long", no_argument, 0, 'l'}, |
| 66 |
{0, 0, 0, 0}, |
| 67 |
}; |
| 68 |
#else |
| 69 |
#define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt) |
| 70 |
#endif |
| 71 |
|
| 72 |
static void sigalarm (int arg); |
| 73 |
|
| 74 |
static void |
| 75 |
usage (void) |
| 76 |
{ |
| 77 |
fprintf (stderr, |
| 78 |
"Usage: diodshowmount [OPTIONS] [-s HOST[:PORT]]\n" |
| 79 |
" -s,--server HOST:PORT server (default localhost:564)\n" |
| 80 |
" -m,--msize msize (default 65536)\n" |
| 81 |
" -u,--uid authenticate as uid (default is your euid)\n" |
| 82 |
" -t,--timeout SECS give up after specified seconds\n" |
| 83 |
" -l,--long list clients one per line and don't strip domain\n" |
| 84 |
); |
| 85 |
exit (1); |
| 86 |
} |
| 87 |
|
| 88 |
int |
| 89 |
main (int argc, char *argv[]) |
| 90 |
{ |
| 91 |
char *server = NULL; |
| 92 |
int msize = 65536; |
| 93 |
uid_t uid = geteuid (); |
| 94 |
int topt = 0; |
| 95 |
Npcfsys *fs = NULL; |
| 96 |
Npcfid *fid, *afid = NULL, *root; |
| 97 |
int c, fd; |
| 98 |
char buf[80], *host, *p; |
| 99 |
hostlist_t hl; |
| 100 |
hostlist_iterator_t itr; |
| 101 |
int lopt = 0; |
| 102 |
|
| 103 |
diod_log_init (argv[0]); |
| 104 |
|
| 105 |
opterr = 0; |
| 106 |
while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) { |
| 107 |
switch (c) { |
| 108 |
case 's': /* --server HOST[:PORT] or /path/to/socket */ |
| 109 |
server = optarg; |
| 110 |
break; |
| 111 |
case 'm': /* --msize SIZE */ |
| 112 |
msize = strtoul (optarg, NULL, 10); |
| 113 |
break; |
| 114 |
case 'u': /* --uid UID */ |
| 115 |
uid = strtoul (optarg, NULL, 10); |
| 116 |
break; |
| 117 |
case 't': /* --timeout SECS */ |
| 118 |
topt = strtoul (optarg, NULL, 10); |
| 119 |
break; |
| 120 |
case 'l': /* --long */ |
| 121 |
lopt = 1; |
| 122 |
break; |
| 123 |
default: |
| 124 |
usage (); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) |
| 129 |
err_exit ("signal"); |
| 130 |
if (signal (SIGALRM, sigalarm) == SIG_ERR) |
| 131 |
err_exit ("signal"); |
| 132 |
|
| 133 |
if (topt > 0) |
| 134 |
alarm (topt); |
| 135 |
|
| 136 |
if ((fd = diod_sock_connect (server, 0)) < 0) |
| 137 |
exit (1); |
| 138 |
|
| 139 |
if (!(fs = npc_start (fd, fd, msize, 0))) |
| 140 |
errn_exit (np_rerror (), "error negotiating protocol with server"); |
| 141 |
if (!(afid = npc_auth (fs, "ctl", uid, diod_auth)) && np_rerror () != 0) |
| 142 |
errn_exit (np_rerror (), "error authenticating to server"); |
| 143 |
if (!(root = npc_attach (fs, afid, "ctl", uid))) |
| 144 |
errn_exit (np_rerror (), "error attaching to aname=ctl"); |
| 145 |
if (!(fid = npc_open_bypath (root, "connections", O_RDONLY))) |
| 146 |
errn_exit (np_rerror (), "open connections"); |
| 147 |
|
| 148 |
if (!(hl = hostlist_create (NULL))) |
| 149 |
err_exit ("hostlist_create"); |
| 150 |
while (npc_gets (fid, buf, sizeof(buf))) { |
| 151 |
if ((p = strchr (buf, ' '))) |
| 152 |
*p = '\0'; |
| 153 |
if (!lopt && (p = strchr (buf, '.'))) |
| 154 |
*p = '\0'; |
| 155 |
if (!hostlist_push_host (hl, buf)) |
| 156 |
err_exit ("hostlist_push_host"); |
| 157 |
} |
| 158 |
hostlist_uniq (hl); |
| 159 |
if (lopt) { |
| 160 |
if (!(itr = hostlist_iterator_create (hl))) |
| 161 |
err_exit ("hostlist_iterator_create"); |
| 162 |
while ((host = hostlist_next (itr))) |
| 163 |
printf ("%s\n", host); |
| 164 |
hostlist_iterator_destroy (itr); |
| 165 |
} else { |
| 166 |
char s[1024]; |
| 167 |
|
| 168 |
if (hostlist_ranged_string (hl, sizeof (s), s) < 0) |
| 169 |
msg_exit ("hostlist output would be too long (use -l)"); |
| 170 |
printf ("%s\n", s); |
| 171 |
} |
| 172 |
hostlist_destroy (hl); |
| 173 |
|
| 174 |
if (npc_clunk (fid) < 0) |
| 175 |
errn_exit (np_rerror (), "clunk connections"); |
| 176 |
if (npc_clunk (root) < 0) |
| 177 |
errn_exit (np_rerror (), "error clunking ctl"); |
| 178 |
if (afid && npc_clunk (afid) < 0) |
| 179 |
errn_exit (np_rerror (), "error clunking afid"); |
| 180 |
npc_finish (fs); |
| 181 |
|
| 182 |
exit(0); |
| 183 |
} |
| 184 |
|
| 185 |
static void sigalarm (int arg) |
| 186 |
{ |
| 187 |
msg_exit ("timed out"); |
| 188 |
} |
| 189 |
|
| 190 |
/* |
| 191 |
* vi:tabstop=4 shiftwidth=4 expandtab |
| 192 |
*/ |