Permalink
Browse files

Add diodshowmount utility (issue 111)

  • Loading branch information...
1 parent 40d78d8 commit a1e7f625f0c09333d4f89dc8b99546899c432384 @garlick garlick committed May 19, 2012
View
@@ -92,6 +92,7 @@ AC_CONFIG_FILES( \
utils/dtop.8 \
utils/diodload.8 \
utils/diodls.8 \
+ utils/diodshowmount.8 \
etc/diod.conf.5 \
scripts/Makefile \
scripts/diod.init \
@@ -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
* elements in 'path'. Multiple WALK requests will be sent depending on
- * the number of path elements. Returns a new fid representing path,
- * or NULL on error (retrieve with np_rerror ()).
+ * the number of path elements. If 'path' is NULL, call npc_clone().
+ * Returns a new fid representing path, or NULL on error (retrieve with
+ * np_rerror ()).
*/
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',
* with 'mode' bits as in open (2).
* Returns 0 on success or -1 on error (retrieve with np_rerror ()).
View
@@ -151,6 +151,7 @@ npc_aget(Npcfid *root, char *path)
/* FIXME: embed a buffer in Npcfid like stdio.
+ * Rewinding the file after each read is inefficient.
*/
char *
npc_gets(Npcfid *fid, char *buf, u32 count)
View
@@ -83,9 +83,16 @@ npc_closedir (Npcfid *fid)
Npcfid *
npc_opendir (Npcfid *root, char *path)
{
- Npcfid *fid;
+ Npcfid *fid = NULL;
+ struct stat sb;
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;
if (!(fid->dbuf = malloc (fid->dbuf_size))) {
(void)npc_closedir (fid);
@@ -97,6 +104,10 @@ npc_opendir (Npcfid *root, char *path)
fid->dbuf_used = 0;
}
return fid;
+error:
+ if (fid)
+ npc_clunk(fid);
+ return NULL;
}
/* Returns error code > 0 on error.
View
@@ -40,6 +40,35 @@
#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 *
npc_walk(Npcfid *nfid, char *path)
{
int n;
@@ -48,6 +77,9 @@ npc_walk(Npcfid *nfid, char *path)
Npfcall *tc = NULL, *rc = NULL;
Npcfid *fid = NULL;
+ if (path == NULL)
+ return npc_clone(nfid);
+
while (*path == '/')
path++;
View
@@ -3,7 +3,7 @@ AM_CFLAGS = @GCCWARN@
AM_CPPFLAGS = \
-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 = \
$(top_builddir)/libdiod/libdiod.a \
@@ -32,9 +32,13 @@ diodload_SOURCES = diodload.c $(common_sources)
diodls_LDADD = $(common_ldadd)
diodls_SOURCES = diodls.c $(common_sources)
+diodshowmount_LDADD = $(common_ldadd)
+diodshowmount_SOURCES = diodshowmount.c $(common_sources)
+
man8_MANS = \
diodmount.8 \
diodcat.8 \
dtop.8 \
diodload.8 \
- diodls.8
+ diodls.8 \
+ diodshowmount.8
View
@@ -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
.SH DESCRIPTION
.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 specified files on stdout.
.SH OPTIONS
View
@@ -5,7 +5,7 @@ diodload \- create artificial diod load
\fBdiodload\fR \fI[OPTIONS] [-s IP:PORT]\fR
.SH DESCRIPTION
.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
\fIctl:zero\fR to \fIctl:null\fR.
.SH OPTIONS
View
@@ -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
.SH DESCRIPTION
.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.
.SH OPTIONS
.TP
View
@@ -210,7 +210,6 @@ lsfile_l (Npcfid *dir, char *name)
if (!(gr = getgrgid (sb.st_gid)))
snprintf (gid, sizeof (gid), "%d", sb.st_gid);
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",
mode2str (sb.st_mode),
sb.st_nlink,
@@ -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)
View
@@ -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.