| 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 |
/* diodcat.c - cat remote files */ |
| 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 "diod_log.h" |
| 53 |
#include "diod_sock.h" |
| 54 |
#include "diod_auth.h" |
| 55 |
|
| 56 |
#define OPTIONS "a:s:m:u:t:p" |
| 57 |
#if HAVE_GETOPT_LONG |
| 58 |
#define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL) |
| 59 |
static const struct option longopts[] = { |
| 60 |
{"aname", required_argument, 0, 'a'}, |
| 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 |
{"privport",no_argument, 0, 'p'}, |
| 66 |
{0, 0, 0, 0}, |
| 67 |
}; |
| 68 |
#else |
| 69 |
#define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt) |
| 70 |
#endif |
| 71 |
|
| 72 |
static int catfiles (int fd, uid_t uid, int msize, char *aname, |
| 73 |
char **av, int ac); |
| 74 |
static void sigalarm (int arg); |
| 75 |
|
| 76 |
static void |
| 77 |
usage (void) |
| 78 |
{ |
| 79 |
fprintf (stderr, |
| 80 |
"Usage: diodcat [OPTIONS] [-s HOST[:PORT]] [-a aname] [file [file...]]\n" |
| 81 |
" -a,--aname NAME file system (default ctl)\n" |
| 82 |
" -s,--server HOST:PORT server (default localhost:564)\n" |
| 83 |
" -m,--msize msize (default 65536)\n" |
| 84 |
" -u,--uid authenticate as uid (default is your euid)\n" |
| 85 |
" -t,--timeout SECS give up after specified seconds\n" |
| 86 |
" -p,--privport connect from a privileged port (root user only)\n" |
| 87 |
); |
| 88 |
exit (1); |
| 89 |
} |
| 90 |
|
| 91 |
int |
| 92 |
main (int argc, char *argv[]) |
| 93 |
{ |
| 94 |
char *aname = NULL; |
| 95 |
char *server = NULL; |
| 96 |
int msize = 65536; |
| 97 |
uid_t uid = geteuid (); |
| 98 |
int topt = 0; |
| 99 |
int flags = 0; |
| 100 |
int fd, c; |
| 101 |
|
| 102 |
diod_log_init (argv[0]); |
| 103 |
|
| 104 |
opterr = 0; |
| 105 |
while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) { |
| 106 |
switch (c) { |
| 107 |
case 'a': /* --aname NAME */ |
| 108 |
aname = optarg; |
| 109 |
break; |
| 110 |
case 's': /* --server HOST[:PORT] or /path/to/socket */ |
| 111 |
server = optarg; |
| 112 |
break; |
| 113 |
case 'm': /* --msize SIZE */ |
| 114 |
msize = strtoul (optarg, NULL, 10); |
| 115 |
break; |
| 116 |
case 'u': /* --uid UID */ |
| 117 |
uid = strtoul (optarg, NULL, 10); |
| 118 |
break; |
| 119 |
case 't': /* --timeout SECS */ |
| 120 |
topt = strtoul (optarg, NULL, 10); |
| 121 |
break; |
| 122 |
case 'p': /* --privport */ |
| 123 |
flags |= DIOD_SOCK_PRIVPORT; |
| 124 |
break; |
| 125 |
default: |
| 126 |
usage (); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) |
| 131 |
err_exit ("signal"); |
| 132 |
if (signal (SIGALRM, sigalarm) == SIG_ERR) |
| 133 |
err_exit ("signal"); |
| 134 |
|
| 135 |
if (topt > 0) |
| 136 |
alarm (topt); |
| 137 |
|
| 138 |
if ((fd = diod_sock_connect (server, flags)) < 0) |
| 139 |
exit (1); |
| 140 |
|
| 141 |
if (!aname) |
| 142 |
aname = "ctl"; |
| 143 |
if (catfiles (fd, uid, msize, aname, argv + optind, argc - optind) < 0) |
| 144 |
exit (1); |
| 145 |
|
| 146 |
close (fd); |
| 147 |
|
| 148 |
diod_log_fini (); |
| 149 |
|
| 150 |
exit (0); |
| 151 |
} |
| 152 |
|
| 153 |
static void sigalarm (int arg) |
| 154 |
{ |
| 155 |
msg_exit ("timed out"); |
| 156 |
} |
| 157 |
|
| 158 |
static int |
| 159 |
cat9 (Npcfid *root, char *path) |
| 160 |
{ |
| 161 |
Npcfid *fid = NULL; |
| 162 |
char *buf = NULL; |
| 163 |
int n, m, done; |
| 164 |
int ret = -1; |
| 165 |
|
| 166 |
if (!(fid = npc_open_bypath (root, path, O_RDONLY))) { |
| 167 |
errn (np_rerror (), "open %s", path); |
| 168 |
goto done; |
| 169 |
} |
| 170 |
NP_ASSERT (fid->iounit > 0); /* libnpclient invariant */ |
| 171 |
if (!(buf = malloc (fid->iounit))) { |
| 172 |
msg ("out of memory"); |
| 173 |
goto done; |
| 174 |
} |
| 175 |
while ((n = npc_read (fid, buf, fid->iounit)) > 0) { |
| 176 |
done = 0; |
| 177 |
do { |
| 178 |
if ((m = write (1, buf + done, n - done)) < 0) { |
| 179 |
if (errno != EPIPE) |
| 180 |
err ("stdout"); |
| 181 |
goto done; |
| 182 |
} |
| 183 |
done += m; |
| 184 |
} while (done < n); |
| 185 |
} |
| 186 |
if (n < 0) { |
| 187 |
errn (np_rerror (), "read %s", path); |
| 188 |
goto done; |
| 189 |
} |
| 190 |
ret = 0; |
| 191 |
done: |
| 192 |
if (buf) |
| 193 |
free (buf); |
| 194 |
if (fid && npc_clunk (fid) < 0) |
| 195 |
errn (np_rerror (), "clunk %s", path); |
| 196 |
return ret; |
| 197 |
} |
| 198 |
|
| 199 |
static int |
| 200 |
catfiles (int fd, uid_t uid, int msize, char *aname, char **av, int ac) |
| 201 |
{ |
| 202 |
Npcfsys *fs = NULL; |
| 203 |
Npcfid *afid = NULL, *root = NULL; |
| 204 |
int i, ret = -1; |
| 205 |
|
| 206 |
if (!(fs = npc_start (fd, fd, msize, 0))) { |
| 207 |
errn (np_rerror (), "error negotiating protocol with server"); |
| 208 |
goto done; |
| 209 |
} |
| 210 |
if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) { |
| 211 |
errn (np_rerror (), "error authenticating to server"); |
| 212 |
goto done; |
| 213 |
} |
| 214 |
if (!(root = npc_attach (fs, afid, aname, uid))) { |
| 215 |
errn (np_rerror (), "error attaching to aname='%s'", aname ? aname : ""); |
| 216 |
goto done; |
| 217 |
} |
| 218 |
if (ac == 0) { |
| 219 |
if (cat9 (root, "version") < 0) |
| 220 |
goto done; |
| 221 |
} else { |
| 222 |
for (i = 0; i < ac; i++) { |
| 223 |
if (cat9 (root, av[i]) < 0) |
| 224 |
goto done; |
| 225 |
} |
| 226 |
} |
| 227 |
ret = 0; |
| 228 |
done: |
| 229 |
if (root && npc_clunk (root) < 0) { |
| 230 |
errn (np_rerror (), "error clunking %s", aname); |
| 231 |
ret = -1; |
| 232 |
} |
| 233 |
if (afid && npc_clunk (afid) < 0) { |
| 234 |
errn (np_rerror (), "error clunking afid"); |
| 235 |
ret = -1; |
| 236 |
} |
| 237 |
if (fs) |
| 238 |
npc_finish (fs); |
| 239 |
return ret; |
| 240 |
} |
| 241 |
|
| 242 |
/* |
| 243 |
* vi:tabstop=4 shiftwidth=4 expandtab |
| 244 |
*/ |