| 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 |
/* diodls.c - list 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 |
#include <dirent.h> |
| 47 |
#include <pwd.h> |
| 48 |
#include <grp.h> |
| 49 |
#include <time.h> |
| 50 |
|
| 51 |
#include "9p.h" |
| 52 |
#include "npfs.h" |
| 53 |
#include "npclient.h" |
| 54 |
|
| 55 |
#include "list.h" |
| 56 |
#include "diod_log.h" |
| 57 |
#include "diod_sock.h" |
| 58 |
#include "diod_auth.h" |
| 59 |
|
| 60 |
#define OPTIONS "a:s:m:u:t:lp" |
| 61 |
#if HAVE_GETOPT_LONG |
| 62 |
#define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL) |
| 63 |
static const struct option longopts[] = { |
| 64 |
{"aname", required_argument, 0, 'a'}, |
| 65 |
{"server", required_argument, 0, 's'}, |
| 66 |
{"msize", required_argument, 0, 'm'}, |
| 67 |
{"uid", required_argument, 0, 'u'}, |
| 68 |
{"timeout", required_argument, 0, 't'}, |
| 69 |
{"privport",no_argument, 0, 'p'}, |
| 70 |
{0, 0, 0, 0}, |
| 71 |
}; |
| 72 |
#else |
| 73 |
#define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt) |
| 74 |
#endif |
| 75 |
|
| 76 |
static int lsfiles (int fd, uid_t uid, int msize, char *aname, int lopt, |
| 77 |
char **av, int ac); |
| 78 |
static void sigalarm (int arg); |
| 79 |
|
| 80 |
static void |
| 81 |
usage (void) |
| 82 |
{ |
| 83 |
fprintf (stderr, |
| 84 |
"Usage: diodcat [OPTIONS] [-s HOST[:PORT]] [-a aname] [file [file...]]\n" |
| 85 |
" -a,--aname NAME file system (default ctl)\n" |
| 86 |
" -s,--server HOST:PORT server (default localhost:564)\n" |
| 87 |
" -l,--long show stat information too\n" |
| 88 |
" -m,--msize msize (default 65536)\n" |
| 89 |
" -u,--uid authenticate as uid (default is your euid)\n" |
| 90 |
" -t,--timeout SECS give up after specified seconds\n" |
| 91 |
" -p,--privport connect from a privileged port (root user only)\n" |
| 92 |
); |
| 93 |
exit (1); |
| 94 |
} |
| 95 |
|
| 96 |
int |
| 97 |
main (int argc, char *argv[]) |
| 98 |
{ |
| 99 |
char *aname = NULL; |
| 100 |
char *server = NULL; |
| 101 |
int msize = 65536; |
| 102 |
uid_t uid = geteuid (); |
| 103 |
int topt = 0; |
| 104 |
int lopt = 0; |
| 105 |
int fd, c; |
| 106 |
int flags = 0; |
| 107 |
|
| 108 |
diod_log_init (argv[0]); |
| 109 |
|
| 110 |
opterr = 0; |
| 111 |
while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) { |
| 112 |
switch (c) { |
| 113 |
case 'a': /* --aname NAME */ |
| 114 |
aname = optarg; |
| 115 |
break; |
| 116 |
case 's': /* --server HOST[:PORT] or /path/to/socket */ |
| 117 |
server = optarg; |
| 118 |
break; |
| 119 |
case 'm': /* --msize SIZE */ |
| 120 |
msize = strtoul (optarg, NULL, 10); |
| 121 |
break; |
| 122 |
case 'u': /* --uid UID */ |
| 123 |
uid = strtoul (optarg, NULL, 10); |
| 124 |
break; |
| 125 |
case 't': /* --timeout SECS */ |
| 126 |
topt = strtoul (optarg, NULL, 10); |
| 127 |
break; |
| 128 |
case 'l': /* --long */ |
| 129 |
lopt = 1; |
| 130 |
break; |
| 131 |
case 'p': /* --privport */ |
| 132 |
flags |= DIOD_SOCK_PRIVPORT; |
| 133 |
break; |
| 134 |
default: |
| 135 |
usage (); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if (signal (SIGPIPE, SIG_IGN) == SIG_ERR) |
| 140 |
err_exit ("signal"); |
| 141 |
if (signal (SIGALRM, sigalarm) == SIG_ERR) |
| 142 |
err_exit ("signal"); |
| 143 |
|
| 144 |
if (topt > 0) |
| 145 |
alarm (topt); |
| 146 |
|
| 147 |
if ((fd = diod_sock_connect (server, flags)) < 0) |
| 148 |
exit (1); |
| 149 |
|
| 150 |
if (!aname) |
| 151 |
aname = "ctl"; |
| 152 |
if (lsfiles (fd, uid, msize, aname, lopt, argv + optind, argc - optind) < 0) |
| 153 |
exit (1); |
| 154 |
|
| 155 |
close (fd); |
| 156 |
|
| 157 |
diod_log_fini (); |
| 158 |
|
| 159 |
exit (0); |
| 160 |
} |
| 161 |
|
| 162 |
static void sigalarm (int arg) |
| 163 |
{ |
| 164 |
msg_exit ("timed out"); |
| 165 |
} |
| 166 |
|
| 167 |
static char * |
| 168 |
mode2str (mode_t mode) |
| 169 |
{ |
| 170 |
static char s[16]; |
| 171 |
|
| 172 |
/* FIXME: represent other file types, sticky bits */ |
| 173 |
s[0] = S_ISDIR(mode) ? 'd' : '-'; |
| 174 |
|
| 175 |
s[1] = mode & S_IRUSR ? 'r' : '-'; |
| 176 |
s[2] = mode & S_IWUSR ? 'w' : '-'; |
| 177 |
s[3] = mode & S_IXUSR ? 'x' : '-'; |
| 178 |
|
| 179 |
s[4] = mode & S_IRGRP ? 'r' : '-'; |
| 180 |
s[5] = mode & S_IWGRP ? 'w' : '-'; |
| 181 |
s[6] = mode & S_IXGRP ? 'x' : '-'; |
| 182 |
|
| 183 |
s[7] = mode & S_IROTH ? 'r' : '-'; |
| 184 |
s[8] = mode & S_IWOTH ? 'w' : '-'; |
| 185 |
s[9] = mode & S_IXOTH ? 'x' : '-'; |
| 186 |
|
| 187 |
s[10] = '.'; |
| 188 |
s[11] = '\0'; |
| 189 |
|
| 190 |
return s; |
| 191 |
} |
| 192 |
|
| 193 |
static void |
| 194 |
lsfile_l (Npcfid *dir, char *name) |
| 195 |
{ |
| 196 |
Npcfid *fid = NULL; |
| 197 |
struct stat sb; |
| 198 |
struct passwd *pw; |
| 199 |
struct group *gr; |
| 200 |
char uid[16], gid[16]; |
| 201 |
char *mtime; |
| 202 |
|
| 203 |
if (!(fid = npc_walk (dir, name))) { |
| 204 |
errn (np_rerror (), "npc_walk %s\n", name); |
| 205 |
goto error; |
| 206 |
} |
| 207 |
if (npc_fstat (fid, &sb) < 0) { |
| 208 |
errn (np_rerror (), "npc_fstat %s\n", name); |
| 209 |
goto error; |
| 210 |
} |
| 211 |
if (npc_clunk (fid) < 0) { |
| 212 |
errn (np_rerror (), "npc_clunk %s\n", name); |
| 213 |
goto error; |
| 214 |
} |
| 215 |
if (!(pw = getpwuid (sb.st_uid))) |
| 216 |
snprintf (uid, sizeof (uid), "%d", sb.st_uid); |
| 217 |
if (!(gr = getgrgid (sb.st_gid))) |
| 218 |
snprintf (gid, sizeof (gid), "%d", sb.st_gid); |
| 219 |
mtime = ctime( &sb.st_mtime); |
| 220 |
printf ("%10s %4lu %s %s %12lu %.*s %s\n", |
| 221 |
mode2str (sb.st_mode), |
| 222 |
sb.st_nlink, |
| 223 |
pw ? pw->pw_name : uid, |
| 224 |
gr ? gr->gr_name : gid, |
| 225 |
sb.st_size, |
| 226 |
(int)strlen (mtime) - 13, mtime + 4, |
| 227 |
name); |
| 228 |
return; |
| 229 |
error: |
| 230 |
if (fid) |
| 231 |
(void)npc_clunk (fid); |
| 232 |
} |
| 233 |
|
| 234 |
static int |
| 235 |
lsdir (int i, int count, Npcfid *root, int lopt, char *path) |
| 236 |
{ |
| 237 |
Npcfid *dir = NULL; |
| 238 |
struct dirent d, *dp; |
| 239 |
|
| 240 |
if (!(dir = npc_opendir (root, path))) { |
| 241 |
if (np_rerror () == ENOTDIR) { |
| 242 |
if (lopt) |
| 243 |
lsfile_l (root, path); |
| 244 |
else |
| 245 |
printf ("%s\n", path); |
| 246 |
return 0; |
| 247 |
} |
| 248 |
errn (np_rerror (), "%s", path); |
| 249 |
goto error; |
| 250 |
} |
| 251 |
if (count > 1) |
| 252 |
printf ("%s:\n", path); |
| 253 |
do { |
| 254 |
if ((npc_readdir_r (dir, &d, &dp)) > 0) { |
| 255 |
errn (np_rerror (), "npc_readdir: %s", path); |
| 256 |
goto error; |
| 257 |
} |
| 258 |
if (dp) { |
| 259 |
if (lopt) |
| 260 |
lsfile_l (dir, dp->d_name); |
| 261 |
else if (strcmp (dp->d_name, ".") && strcmp (dp->d_name, "..")) |
| 262 |
printf ("%s\n", dp->d_name); |
| 263 |
} |
| 264 |
} while (dp != NULL); |
| 265 |
if (npc_clunk (dir) < 0) { |
| 266 |
errn (np_rerror (), "npc_clunk: %s", path); |
| 267 |
goto error; |
| 268 |
} |
| 269 |
if (count > 1 && i < count - 1) |
| 270 |
printf ("\n"); |
| 271 |
return 0; |
| 272 |
error: |
| 273 |
if (dir) |
| 274 |
(void)npc_clunk (dir); |
| 275 |
return -1; |
| 276 |
} |
| 277 |
|
| 278 |
static int |
| 279 |
lsfiles (int fd, uid_t uid, int msize, char *aname, int lopt, char **av, int ac) |
| 280 |
{ |
| 281 |
Npcfsys *fs = NULL; |
| 282 |
Npcfid *afid = NULL, *root = NULL; |
| 283 |
int i, ret = -1; |
| 284 |
|
| 285 |
if (!(fs = npc_start (fd, fd, msize, 0))) { |
| 286 |
errn (np_rerror (), "error negotiating protocol with server"); |
| 287 |
goto done; |
| 288 |
} |
| 289 |
if (!(afid = npc_auth (fs, aname, uid, diod_auth)) && np_rerror () != 0) { |
| 290 |
errn (np_rerror (), "error authenticating to server"); |
| 291 |
goto done; |
| 292 |
} |
| 293 |
if (!(root = npc_attach (fs, afid, aname, uid))) { |
| 294 |
errn (np_rerror (), "error attaching to aname='%s'", aname ? aname : ""); |
| 295 |
goto done; |
| 296 |
} |
| 297 |
if (ac == 0) { |
| 298 |
if (lsdir(1, 1, root, lopt, NULL) < 0) |
| 299 |
goto done; |
| 300 |
} else { |
| 301 |
for (i = 0; i < ac; i++) { |
| 302 |
if (lsdir (i, ac, root, lopt, av[i]) < 0) |
| 303 |
goto done; |
| 304 |
} |
| 305 |
} |
| 306 |
ret = 0; |
| 307 |
done: |
| 308 |
if (root && npc_clunk (root) < 0) { |
| 309 |
errn (np_rerror (), "error clunking %s", aname); |
| 310 |
ret = -1; |
| 311 |
} |
| 312 |
if (afid && npc_clunk (afid) < 0) { |
| 313 |
errn (np_rerror (), "error clunking afid"); |
| 314 |
ret = -1; |
| 315 |
} |
| 316 |
if (fs) |
| 317 |
npc_finish (fs); |
| 318 |
return ret; |
| 319 |
} |
| 320 |
|
| 321 |
/* |
| 322 |
* vi:tabstop=4 shiftwidth=4 expandtab |
| 323 |
*/ |