Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add diodshowmount utility (issue 111)
  • Loading branch information
garlick committed May 19, 2012
1 parent 40d78d8 commit a1e7f62
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 9 deletions.
1 change: 1 addition & 0 deletions configure.ac
Expand Up @@ -92,6 +92,7 @@ AC_CONFIG_FILES( \
utils/dtop.8 \ utils/dtop.8 \
utils/diodload.8 \ utils/diodload.8 \
utils/diodls.8 \ utils/diodls.8 \
utils/diodshowmount.8 \
etc/diod.conf.5 \ etc/diod.conf.5 \
scripts/Makefile \ scripts/Makefile \
scripts/diod.init \ scripts/diod.init \
Expand Down
10 changes: 8 additions & 2 deletions libnpclient/npclient.h
Expand Up @@ -109,11 +109,17 @@ int npc_pwrite (Npcfid *fid, void *buf, u32 count, u64 offset);


/* Descend a directory represnted by 'fid' by walking successive path /* Descend a directory represnted by 'fid' by walking successive path
* elements in 'path'. Multiple WALK requests will be sent depending on * elements in 'path'. Multiple WALK requests will be sent depending on
* the number of path elements. Returns a new fid representing path, * the number of path elements. If 'path' is NULL, call npc_clone().
* or NULL on error (retrieve with np_rerror ()). * Returns a new fid representing path, or NULL on error (retrieve with
* np_rerror ()).
*/ */
Npcfid *npc_walk (Npcfid *fid, char *path); Npcfid *npc_walk (Npcfid *fid, char *path);


/* Clone a fid. Returns a new fid representing the same path as 'fid',
* or NULL on error (retrieve with np_rerror ()).
*/
Npcfid *npc_clone(Npcfid *fid);

/* Send a MKDIR request to create 'name' in parent directory 'fid', /* Send a MKDIR request to create 'name' in parent directory 'fid',
* with 'mode' bits as in open (2). * with 'mode' bits as in open (2).
* Returns 0 on success or -1 on error (retrieve with np_rerror ()). * Returns 0 on success or -1 on error (retrieve with np_rerror ()).
Expand Down
1 change: 1 addition & 0 deletions libnpclient/read.c
Expand Up @@ -151,6 +151,7 @@ npc_aget(Npcfid *root, char *path)




/* FIXME: embed a buffer in Npcfid like stdio. /* FIXME: embed a buffer in Npcfid like stdio.
* Rewinding the file after each read is inefficient.
*/ */
char * char *
npc_gets(Npcfid *fid, char *buf, u32 count) npc_gets(Npcfid *fid, char *buf, u32 count)
Expand Down
13 changes: 12 additions & 1 deletion libnpclient/readdir.c
Expand Up @@ -83,9 +83,16 @@ npc_closedir (Npcfid *fid)
Npcfid * Npcfid *
npc_opendir (Npcfid *root, char *path) npc_opendir (Npcfid *root, char *path)
{ {
Npcfid *fid; Npcfid *fid = NULL;
struct stat sb;


if ((fid = npc_open_bypath (root, path, O_RDONLY))) { if ((fid = npc_open_bypath (root, path, O_RDONLY))) {
if (npc_fstat (fid, &sb) < 0)
goto error;
if (!S_ISDIR (sb.st_mode)) {
np_uerror (ENOTDIR);
goto error;
}
fid->dbuf_size = root->fsys->msize - P9_IOHDRSZ; fid->dbuf_size = root->fsys->msize - P9_IOHDRSZ;
if (!(fid->dbuf = malloc (fid->dbuf_size))) { if (!(fid->dbuf = malloc (fid->dbuf_size))) {
(void)npc_closedir (fid); (void)npc_closedir (fid);
Expand All @@ -97,6 +104,10 @@ npc_opendir (Npcfid *root, char *path)
fid->dbuf_used = 0; fid->dbuf_used = 0;
} }
return fid; return fid;
error:
if (fid)
npc_clunk(fid);
return NULL;
} }


/* Returns error code > 0 on error. /* Returns error code > 0 on error.
Expand Down
32 changes: 32 additions & 0 deletions libnpclient/walk.c
Expand Up @@ -39,6 +39,35 @@
#include "npclient.h" #include "npclient.h"
#include "npcimpl.h" #include "npcimpl.h"


Npcfid *
npc_clone(Npcfid *fid)
{
Npcfsys *fsys = fid->fsys;
Npcfid *nfid = NULL;
Npfcall *tc = NULL, *rc = NULL;

if (!(nfid = npc_fid_alloc(fsys)))
goto error;
if (!(tc = np_create_twalk(fid->fid, nfid->fid, 0, NULL))) {
np_uerror(ENOMEM);
goto error;
}
if (fsys->rpc(fsys, tc, &rc) < 0)
goto error;
free(tc);
free(rc);

return nfid;
error:
if (rc)
free(rc);
if (tc)
free(tc);
if (nfid)
npc_fid_free (nfid);
return NULL;
}

Npcfid * Npcfid *
npc_walk(Npcfid *nfid, char *path) npc_walk(Npcfid *nfid, char *path)
{ {
Expand All @@ -48,6 +77,9 @@ npc_walk(Npcfid *nfid, char *path)
Npfcall *tc = NULL, *rc = NULL; Npfcall *tc = NULL, *rc = NULL;
Npcfid *fid = NULL; Npcfid *fid = NULL;


if (path == NULL)
return npc_clone(nfid);

while (*path == '/') while (*path == '/')
path++; path++;


Expand Down
8 changes: 6 additions & 2 deletions utils/Makefile.am
Expand Up @@ -3,7 +3,7 @@ AM_CFLAGS = @GCCWARN@
AM_CPPFLAGS = \ AM_CPPFLAGS = \
-I../libnpfs -I../liblsd -I../libdiod -I../libnpclient -I../libnpfs -I../liblsd -I../libdiod -I../libnpclient


sbin_PROGRAMS = diodmount diodcat dtop diodload diodls sbin_PROGRAMS = diodmount diodcat dtop diodload diodls diodshowmount


common_ldadd = \ common_ldadd = \
$(top_builddir)/libdiod/libdiod.a \ $(top_builddir)/libdiod/libdiod.a \
Expand Down Expand Up @@ -32,9 +32,13 @@ diodload_SOURCES = diodload.c $(common_sources)
diodls_LDADD = $(common_ldadd) diodls_LDADD = $(common_ldadd)
diodls_SOURCES = diodls.c $(common_sources) diodls_SOURCES = diodls.c $(common_sources)


diodshowmount_LDADD = $(common_ldadd)
diodshowmount_SOURCES = diodshowmount.c $(common_sources)

man8_MANS = \ man8_MANS = \
diodmount.8 \ diodmount.8 \
diodcat.8 \ diodcat.8 \
dtop.8 \ dtop.8 \
diodload.8 \ diodload.8 \
diodls.8 diodls.8 \
diodshowmount.8
2 changes: 1 addition & 1 deletion utils/diodcat.8.in
Expand Up @@ -5,7 +5,7 @@ diodcat \- cat files by attaching directly to diod server
\fBdiodcat\fR \fI[OPTIONS] [-s IP:PORT] [-a aname] [file [file...]]\fR \fBdiodcat\fR \fI[OPTIONS] [-s IP:PORT] [-a aname] [file [file...]]\fR
.SH DESCRIPTION .SH DESCRIPTION
.B diodcat .B diodcat
connects to a \fBdiod\fR server on \fIhost\fR, attaches to connects to a \fBdiod\fR server on \fIIP:PORT\fR, attaches to
the mount point \fIaname\fR, and concatenates the contents of the mount point \fIaname\fR, and concatenates the contents of
the specified files on stdout. the specified files on stdout.
.SH OPTIONS .SH OPTIONS
Expand Down
2 changes: 1 addition & 1 deletion utils/diodload.8.in
Expand Up @@ -5,7 +5,7 @@ diodload \- create artificial diod load
\fBdiodload\fR \fI[OPTIONS] [-s IP:PORT]\fR \fBdiodload\fR \fI[OPTIONS] [-s IP:PORT]\fR
.SH DESCRIPTION .SH DESCRIPTION
.B diodload .B diodload
connects to a \fBdiod\fR server and issues requests. connects to a \fBdiod\fR server on \fIIP:PORT\fR and issues requests.
Unless changed with \fI-g\fR, each thread copies from Unless changed with \fI-g\fR, each thread copies from
\fIctl:zero\fR to \fIctl:null\fR. \fIctl:zero\fR to \fIctl:null\fR.
.SH OPTIONS .SH OPTIONS
Expand Down
2 changes: 1 addition & 1 deletion utils/diodls.8.in
Expand Up @@ -5,7 +5,7 @@ diodls \- list files by attaching directly to diod server
\fBdiodls\fR \fI[OPTIONS] [-s IP:PORT] [-a aname] [file [file...]]\fR \fBdiodls\fR \fI[OPTIONS] [-s IP:PORT] [-a aname] [file [file...]]\fR
.SH DESCRIPTION .SH DESCRIPTION
.B diodls .B diodls
connects to a \fBdiod\fR server on \fIhost\fR, attaches to connects to a \fBdiod\fR server on \fIIP:PORT\fR, attaches to
the mount point \fIaname\fR, and lists the specified files or directories. the mount point \fIaname\fR, and lists the specified files or directories.
.SH OPTIONS .SH OPTIONS
.TP .TP
Expand Down
1 change: 0 additions & 1 deletion utils/diodls.c
Expand Up @@ -210,7 +210,6 @@ lsfile_l (Npcfid *dir, char *name)
if (!(gr = getgrgid (sb.st_gid))) if (!(gr = getgrgid (sb.st_gid)))
snprintf (gid, sizeof (gid), "%d", sb.st_gid); snprintf (gid, sizeof (gid), "%d", sb.st_gid);
mtime = ctime( &sb.st_mtime); mtime = ctime( &sb.st_mtime);
/* -rwxr-xr-x. 1 garlick garlick 774551 May 18 14:56 diodcat */
printf ("%10s %4lu %s %s %12lu %.*s %s\n", printf ("%10s %4lu %s %s %12lu %.*s %s\n",
mode2str (sb.st_mode), mode2str (sb.st_mode),
sb.st_nlink, sb.st_nlink,
Expand Down
25 changes: 25 additions & 0 deletions utils/diodshowmount.8.in
@@ -0,0 +1,25 @@
.TH diodcat 8 "@META_DATE@" "@META_ALIAS@" "@META_NAME@"
.SH NAME
diodshowmount \- list diod server connections
.SH SYNOPSIS
\fBdiodshowmount\fR \fI[OPTIONS] [-s IP:PORT]\fR
.SH DESCRIPTION
.B diodshowmount
connects to a \fBdiod\fR server on \fIIP:PORT\fR and displays a list of
clients connected to that server, one per line.
.SH OPTIONS
.TP
.I "-s, --server NAME"
The server in IP:PORT, HOST:PORT, or /path/to/unix_domain_socket form
(default localhost:564).
.TP
.I "-m, --msize SIZE"
The maximum request size including 9P headers (default 65536).
.TP
.I "-u, --uid UID"
Try to attach to the server as the specified user (default your effective uid).
.TP
.I "-t, --timeout SECS"
Force timeout after specified number of seconds (default no timeout).
.SH "SEE ALSO"
diod (8)
175 changes: 175 additions & 0 deletions utils/diodshowmount.c
@@ -0,0 +1,175 @@
/*****************************************************************************
* 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/>.
*****************************************************************************/

/* diodshowmount.c - cat ctl:connections */

#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 "hostlist.h"
#include "diod_log.h"
#include "diod_sock.h"
#include "diod_auth.h"

#define OPTIONS "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[] = {
{"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 void sigalarm (int arg);

static void
usage (void)
{
fprintf (stderr,
"Usage: diodshowmount [OPTIONS] [-s HOST:PORT]\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 *server = "localhost:564";
int msize = 65536;
uid_t uid = geteuid ();
int topt = 0;
Npcfsys *fs = NULL;
Npcfid *fid, *afid, *root;
int c, fd;
char buf[80], *host, *p;
hostlist_t hl;
hostlist_iterator_t itr;

diod_log_init (argv[0]);

opterr = 0;
while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) {
switch (c) {
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 (!(fs = npc_start (fd, fd, msize, 0)))
errn_exit (np_rerror (), "error negotiating protocol with server");
if (!(afid = npc_auth (fs, "ctl", uid, diod_auth)) && np_rerror () != 0)
errn_exit (np_rerror (), "error authenticating to server");
if (!(root = npc_attach (fs, afid, "ctl", uid)))
errn_exit (np_rerror (), "error attaching to aname=ctl");
if (!(fid = npc_open_bypath (root, "connections", O_RDONLY)))
errn_exit (np_rerror (), "open connections");

if (!(hl = hostlist_create (NULL)))
err_exit ("hostlist_create");
while (npc_gets (fid, buf, sizeof(buf))) { // inefficient (fix libnpclient)
if ((p = strchr (buf, ' ')))
*p = '\0';
if (!hostlist_push_host (hl, buf))
err_exit ("hostlist_push_host");
}
hostlist_uniq (hl);
if (!(itr = hostlist_iterator_create (hl)))
err_exit ("hostlist_iterator_create");
while ((host = hostlist_next (itr)))
printf ("%s\n", host);
hostlist_iterator_destroy (itr);
hostlist_destroy (hl);

if (npc_clunk (fid) < 0)
errn_exit (np_rerror (), "clunk connections");
if (npc_clunk (root) < 0)
errn_exit (np_rerror (), "error clunking ctl");
if (npc_clunk (afid) < 0)
errn_exit (np_rerror (), "error clunking afid");
npc_finish (fs);

exit(0);
}

static void sigalarm (int arg)
{
msg_exit ("timed out");
}

/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/

0 comments on commit a1e7f62

Please sign in to comment.