1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
/*****************************************************************************
* Copyright (C) 2010-2011 Lawrence Livermore National Security, LLC.
* Written by Jim Garlick <garlick@llnl.gov> LLNL-CODE-423279
* All Rights Reserved.
*
* This file is part of the Distributed I/O Daemon (diod).
* For details, see <http://code.google.com/p/diod/>.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (as published by the
* Free Software Foundation) version 2, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or see
* <http://www.gnu.org/licenses/>.
*****************************************************************************/
/* diodcat.c - cat remote files */
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <libgen.h>
#include <signal.h>
#include "9p.h"
#include "npfs.h"
#include "npclient.h"
#include "list.h"
#include "diod_log.h"
#include "diod_sock.h"
#include "diod_auth.h"
#define OPTIONS "a:s:m:u:t:"
#if HAVE_GETOPT_LONG
#define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL)
static const struct option longopts[] = {
{"aname", required_argument, 0, 'a'},
{"server", required_argument, 0, 's'},
{"msize", required_argument, 0, 'm'},
{"uid", required_argument, 0, 'u'},
{"timeout", required_argument, 0, 't'},
{0, 0, 0, 0},
};
#else
#define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt)
#endif
static int catfiles (int fd, uid_t uid, int msize, char *aname,
char **av, int ac);
static void sigalarm (int arg);
static void
usage (void)
{
fprintf (stderr,
"Usage: diodcat [OPTIONS] [-s HOST[:PORT]] [-a aname] [file [file...]]\n"
" -a,--aname NAME file system (default ctl)\n"
" -s,--server HOST:PORT server (default localhost:564)\n"
" -m,--msize msize (default 65536)\n"
" -u,--uid authenticate as uid (default is your euid)\n"
" -t,--timeout SECS give up after specified seconds\n"
);
exit (1);
}
int
main (int argc, char *argv[])
{
char *aname = NULL;
char *server = NULL;
int msize = 65536;
uid_t uid = geteuid ();
int topt = 0;
int fd, c;
diod_log_init (argv[0]);
opterr = 0;
while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) {
switch (c) {
case 'a': /* --aname NAME */
aname = optarg;
break;
case 's': /* --server HOST[:PORT] or /path/to/socket */
server = optarg;
break;
case 'm': /* --msize SIZE */
msize = strtoul (optarg, NULL, 10);
break;
case 'u': /* --uid UID */
uid = strtoul (optarg, NULL, 10);
break;
case 't': /* --timeout SECS */
topt = strtoul (optarg, NULL, 10);
break;
default:
usage ();
}
}
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR)
err_exit ("signal");
if (signal (SIGALRM, sigalarm) == SIG_ERR)
err_exit ("signal");
if (topt > 0)
alarm (topt);
if ((fd = diod_sock_connect (server, 0)) < 0)
exit (1);
if (!aname)
aname = "ctl";
if (catfiles (fd, uid, msize, aname, argv + optind, argc - optind) < 0)
exit (1);
close (fd);
diod_log_fini ();
exit (0);
}
static void sigalarm (int arg)
{
msg_exit ("timed out");
}
static int
cat9 (Npcfid *root, char *path)
{
Npcfid *fid = NULL;
char *buf = NULL;
int n, m, done;
int ret = -1;
if (!(fid = npc_open_bypath (root, path, O_RDONLY))) {
errn (np_rerror (), "open %s", path);
goto done;
}
NP_ASSERT (fid->iounit > 0); /* libnpclient invariant */
if (!(buf = malloc (fid->iounit))) {
msg ("out of memory");
goto done;
}
while ((n = npc_read (fid, buf, fid->iounit)) > 0) {
done = 0;
do {
if ((m = write (1, buf + done, n - done)) < 0) {
if (errno != EPIPE)
err ("stdout");
goto done;
}
done += m;
} while (done < n);
}
if (n < 0) {
errn (np_rerror (), "read %s", path);
goto done;
}
ret = 0;
done:
if (buf)
free (buf);
if (fid && npc_clunk (fid) < 0)
errn (np_rerror (), "clunk %s", path);
return ret;
}
static int
catfiles (int fd, uid_t uid, int msize, char *aname, char **av, int ac)
{
Npcfsys *fs = NULL;
Npcfid *afid = NULL, *root = NULL;
int i, ret = -1;
if (!(fs = npc_start (fd, fd, msize, 0))) {
errn (np_rerror (), "error negotiating protocol with server");
goto done;
}
if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) {
errn (np_rerror (), "error authenticating to server");
goto done;
}
if (!(root = npc_attach (fs, afid, aname, uid))) {
errn (np_rerror (), "error attaching to aname='%s'", aname ? aname : "");
goto done;
}
if (ac == 0) {
if (cat9 (root, "version") < 0)
goto done;
} else {
for (i = 0; i < ac; i++) {
if (cat9 (root, av[i]) < 0)
goto done;
}
}
ret = 0;
done:
if (root && npc_clunk (root) < 0) {
errn (np_rerror (), "error clunking %s", aname);
ret = -1;
}
if (afid && npc_clunk (afid) < 0) {
errn (np_rerror (), "error clunking afid");
ret = -1;
}
if (fs)
npc_finish (fs);
return ret;
}
/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/
|