[hackers] [libixp] Add ixp_namespace, ixp_nsmount, ixp_smprint. Bump API version.

From: Kris Maglione <jg_AT_suckless.org>
Date: Sun Feb 03 23:11:06 2008

changeset: 89:85de01452e0a
tag: tip
user: Kris Maglione <jg_AT_suckless.org>
date: Sun Feb 03 17:10:10 2008 -0500
summary: Add ixp_namespace, ixp_nsmount, ixp_smprint. Bump API version.

diff -r 3e1f5e86e99b -r 85de01452e0a include/ixp.h
--- a/include/ixp.h Sun Jan 27 13:48:38 2008 -0500
+++ b/include/ixp.h Sun Feb 03 17:10:10 2008 -0500
@@ -7,7 +7,7 @@
 #include <sys/types.h>
 #include <sys/select.h>
 
-#define IXP_API 87
+#define IXP_API 89
 
 /* Gunk */
 #if defined(IXP_NEEDAPI) && IXP_API < IXP_NEEDAPI
@@ -498,7 +498,6 @@ int ixp_pthread_init(void);
 
 /* client.c */
 int ixp_close(IxpCFid*);
-Stat* ixp_fstat(IxpCFid*);
 long ixp_pread(IxpCFid*, void*, long, vlong);
 int ixp_print(IxpCFid*, const char*, ...);
 long ixp_pwrite(IxpCFid*, const void*, long, vlong);
@@ -508,8 +507,10 @@ int ixp_vprint(IxpCFid*, const char*, v
 int ixp_vprint(IxpCFid*, const char*, va_list);
 long ixp_write(IxpCFid*, const void*, long);
 IxpCFid* ixp_create(IxpClient*, const char*, uint perm, uchar mode);
-IxpClient* ixp_mount(char*);
+IxpStat* ixp_fstat(IxpCFid*);
+IxpClient* ixp_mount(const char*);
 IxpClient* ixp_mountfd(int);
+IxpClient* ixp_nsmount(const char*);
 IxpCFid* ixp_open(IxpClient*, const char*, uchar);
 IxpStat* ixp_stat(IxpClient*, const char*);
 
@@ -566,11 +567,14 @@ int ixp_unsettimer(IxpServer*, long);
 int ixp_unsettimer(IxpServer*, long);
 
 /* util.c */
+void ixp_cleanname(char*);
 void* ixp_emalloc(uint);
 void* ixp_emallocz(uint);
+void ixp_eprint(const char*, ...);
 void* ixp_erealloc(void*, uint);
 char* ixp_estrdup(const char*);
-void ixp_eprint(const char*, ...);
+char* ixp_namespace(void);
+char* ixp_smprint(const char*, ...);
+uint ixp_strlcat(char*, const char*, uint);
 uint ixp_tokenize(char**, uint len, char*, char);
-uint ixp_strlcat(char*, const char*, uint);
-
+
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/client.c
--- a/libixp/client.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/client.c Sun Feb 03 17:10:10 2008 -0500
@@ -77,7 +77,7 @@ dofcall(IxpClient *c, Fcall *fcall) {
                 werrstr("received mismatched fcall");
                 goto fail;
         }
- memcpy(fcall, ret, sizeof(*fcall));
+ memcpy(fcall, ret, sizeof *fcall);
         free(ret);
         return 1;
 fail:
@@ -121,15 +121,19 @@ allocmsg(IxpClient *c, int n) {
 /**
  * Function: ixp_mountfd
  * Function: ixp_mount
+ * Function: ixp_nsmount
  *
  * Params:
  * fd - A file descriptor which is already connected
  * to a 9P server.
  * address - An address (in Plan 9 resource fomat) on
  * which to connect to a 9P server.
- *
- * Initiate a 9P connection with the server at
- * P<address> or connected to on P<fd>.
+ * name - The name of the socket in the process's canonical
+ * namespace directory.
+ *
+ * Initiate a 9P connection with the server at P<address>,
+ * connected to on P<fd>, or under the process's namespace
+ * directory as P<name>.
  *
  * Returns:
  * A pointer to a new 9P client.
@@ -140,7 +144,7 @@ ixp_mountfd(int fd) {
         IxpClient *c;
         Fcall fcall;
 
- c = emallocz(sizeof(*c));
+ c = emallocz(sizeof *c);
         c->fd = fd;
 
         muxinit(c);
@@ -187,13 +191,28 @@ ixp_mountfd(int fd) {
 }
 
 IxpClient*
-ixp_mount(char *address) {
+ixp_mount(const char *address) {
         int fd;
 
         fd = ixp_dial(address);
         if(fd < 0)
                 return nil;
         return ixp_mountfd(fd);
+}
+
+IxpClient*
+ixp_nsmount(const char *name) {
+ char *address;
+ IxpClient *c;
+
+ address = ixp_namespace();
+ if(address)
+ address = ixp_smprint("unix!%s/%s", address, name);
+ if(address == nil)
+ return nil;
+ c = ixp_mount(address);
+ free(address);
+ return c;
 }
 
 static IxpCFid*
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/error.c
--- a/libixp/error.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/error.c Sun Feb 03 17:10:10 2008 -0500
@@ -78,7 +78,7 @@ errstr(char *buf, int n) {
 errstr(char *buf, int n) {
         char tmp[IXP_ERRMAX];
 
- strncpy(tmp, buf, sizeof(tmp));
+ strncpy(tmp, buf, sizeof tmp);
         rerrstr(buf, n);
         strncpy(thread->errbuf(), tmp, IXP_ERRMAX);
         errno = EPLAN9;
@@ -95,7 +95,7 @@ werrstr(const char *fmt, ...) {
         va_list ap;
 
         va_start(ap, fmt);
- ixp_vsnprint(tmp, sizeof(tmp), fmt, ap);
+ ixp_vsnprint(tmp, sizeof tmp, fmt, ap);
         va_end(ap);
         strncpy(thread->errbuf(), tmp, IXP_ERRMAX);
         errno = EPLAN9;
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/intmap.c
--- a/libixp/intmap.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/intmap.c Sun Feb 03 17:10:10 2008 -0500
@@ -100,7 +100,7 @@ insertkey(Intmap *map, ulong id, void *v
                 ov = f->aux;
                 f->aux = v;
         }else{
- f = emallocz(sizeof(*f));
+ f = emallocz(sizeof *f);
                 f->id = id;
                 f->aux = v;
                 h = hashid(map, id);
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/request.c
--- a/libixp/request.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/request.c Sun Feb 03 17:10:10 2008 -0500
@@ -73,7 +73,7 @@ createfid(Intmap *map, int fid, Ixp9Conn
 createfid(Intmap *map, int fid, Ixp9Conn *pc) {
         Fid *f;
 
- f = emallocz(sizeof(Fid));
+ f = emallocz(sizeof *f);
         pc->ref++;
         f->conn = pc;
         f->fid = fid;
@@ -116,7 +116,7 @@ handlefcall(IxpConn *c) {
                 goto Fail;
         thread->unlock(&pc->rlock);
 
- req = emallocz(sizeof(Ixp9Req));
+ req = emallocz(sizeof *req);
         pc->ref++;
         req->conn = pc;
         req->srv = pc->srv;
@@ -425,7 +425,7 @@ voidrequest(void *t) {
         pc = r->conn;
         pc->ref++;
 
- tr = emallocz(sizeof(Ixp9Req));
+ tr = emallocz(sizeof *tr);
         tr->ifcall.type = TFlush;
         tr->ifcall.tag = IXP_NOTAG;
         tr->ifcall.oldtag = r->ifcall.tag;
@@ -444,7 +444,7 @@ voidfid(void *t) {
         pc = f->conn;
         pc->ref++;
 
- tr = emallocz(sizeof(Ixp9Req));
+ tr = emallocz(sizeof *tr);
         tr->ifcall.type = TClunk;
         tr->ifcall.tag = IXP_NOTAG;
         tr->ifcall.fid = f->fid;
@@ -476,7 +476,7 @@ serve_9pcon(IxpConn *c) {
         if(fd < 0)
                 return;
 
- pc = emallocz(sizeof(Ixp9Conn));
+ pc = emallocz(sizeof *pc);
         pc->ref++;
         pc->srv = c->aux;
         pc->rmsg.size = 1024;
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/rpc.c
--- a/libixp/rpc.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/rpc.c Sun Feb 03 17:10:10 2008 -0500
@@ -88,7 +88,7 @@ muxrecv(IxpClient *mux)
         thread->lock(&mux->rlock);
         if(ixp_recvmsg(mux->fd, &mux->rmsg) == 0)
                 goto fail;
- f = emallocz(sizeof(Fcall));
+ f = emallocz(sizeof *f);
         if(ixp_msg2fcall(&mux->rmsg, f) == 0) {
                 free(f);
                 f = nil;
@@ -214,10 +214,10 @@ gettag(IxpClient *mux, IxpRpc *r)
                                         mw = 1;
                                 else
                                         mw <<= 1;
- w = realloc(mux->wait, mw*sizeof(w[0]));
+ w = realloc(mux->wait, mw * sizeof *w);
                                 if(w == nil)
                                         return -1;
- memset(w+mux->mwait, 0, (mw-mux->mwait)*sizeof(w[0]));
+ memset(w+mux->mwait, 0, (mw-mux->mwait) * sizeof *w);
                                 mux->wait = w;
                                 mux->freetag = mux->mwait;
                                 mux->mwait = mw;
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/server.c
--- a/libixp/server.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/server.c Sun Feb 03 17:10:10 2008 -0500
@@ -38,7 +38,7 @@ ixp_listen(IxpServer *s, int fd, void *a
                 ) {
         IxpConn *c;
 
- c = emallocz(sizeof(IxpConn));
+ c = emallocz(sizeof *c);
         c->fd = fd;
         c->aux = aux;
         c->srv = s;
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/socket.c
--- a/libixp/socket.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/socket.c Sun Feb 03 17:10:10 2008 -0500
@@ -52,10 +52,10 @@ sock_unix(char *address, sockaddr_un *sa
 sock_unix(char *address, sockaddr_un *sa, socklen_t *salen) {
         int fd;
 
- memset(sa, 0, sizeof(*sa));
+ memset(sa, 0, sizeof *sa);
 
         sa->sun_family = AF_UNIX;
- strncpy(sa->sun_path, address, sizeof(sa->sun_path));
+ strncpy(sa->sun_path, address, sizeof sa->sun_path);
         *salen = SUN_LEN(sa);
 
         fd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -94,7 +94,7 @@ announce_unix(char *file) {
         if(fd == -1)
                 return fd;
 
- if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0)
+ if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof yes) < 0)
                 goto fail;
 
         unlink(file);
@@ -123,7 +123,7 @@ alookup(char *host, int announce) {
         if(port == nil)
                 return nil;
 
- memset(&hints, 0, sizeof(hints));
+ memset(&hints, 0, sizeof hints);
         hints.ai_family = AF_INET;
         hints.ai_socktype = SOCK_STREAM;
 
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/transport.c
--- a/libixp/transport.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/transport.c Sun Feb 03 17:10:10 2008 -0500
@@ -24,7 +24,7 @@ mread(int fd, IxpMsg *msg, uint count) {
         if(n > count)
                 n = count;
 
- r = ixp_thread->read(fd, msg->pos, n);
+ r = thread->read(fd, msg->pos, n);
         if(r > 0)
                 msg->pos += r;
         return r;
@@ -41,7 +41,7 @@ readn(int fd, IxpMsg *msg, uint count) {
                 if(r == -1 && errno == EINTR)
                         continue;
                 if(r == 0) {
- werrstr("broken pipe");
+ werrstr("broken pipe: %r");
                         return count - num;
                 }
                 num -= r;
@@ -55,11 +55,11 @@ ixp_sendmsg(int fd, IxpMsg *msg) {
 
         msg->pos = msg->data;
         while(msg->pos < msg->end) {
- r = ixp_thread->write(fd, msg->pos, msg->end - msg->pos);
+ r = thread->write(fd, msg->pos, msg->end - msg->pos);
                 if(r < 1) {
                         if(errno == EINTR)
                                 continue;
- werrstr("broken pipe");
+ werrstr("broken pipe: %r");
                         return 0;
                 }
                 msg->pos += r;
@@ -94,3 +94,4 @@ ixp_recvmsg(int fd, IxpMsg *msg) {
         msg->end = msg->pos;
         return msize;
 }
+
diff -r 3e1f5e86e99b -r 85de01452e0a libixp/util.c
--- a/libixp/util.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp/util.c Sun Feb 03 17:10:10 2008 -0500
@@ -6,7 +6,121 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <pwd.h>
 #include "ixp_local.h"
+
+char*
+ixp_smprint(const char *fmt, ...) {
+ va_list ap;
+ char *s;
+
+ va_start(ap, fmt);
+ s = ixp_vsmprint(fmt, ap);
+ va_end(ap);
+ if(s == nil)
+ ixp_werrstr("no memory");
+ return s;
+}
+
+static char*
+_user(void) {
+ static char *user;
+ struct passwd *pw;
+
+ if(user == nil) {
+ pw = getpwuid(getuid());
+ if(pw)
+ user = strdup(pw->pw_name);
+ }
+ if(user == nil)
+ user = "none";
+ return user;
+}
+
+static int
+rmkdir(char *path, int mode) {
+ char *p;
+ int ret;
+ char c;
+
+ for(p = path+1; ; p++) {
+ c = *p;
+ if((c == '/') || (c == '\0')) {
+ *p = '\0';
+ ret = mkdir(path, mode);
+ if((ret == -1) && (errno != EEXIST)) {
+ ixp_werrstr("Can't create path '%s': %r", path);
+ return 0;
+ }
+ *p = c;
+ }
+ if(c == '\0')
+ break;
+ }
+ return 1;
+}
+
+static char*
+ns_display(void) {
+ char *path, *disp;
+ struct stat st;
+
+ disp = getenv("DISPLAY");
+ if(disp == nil || disp[0] == '\0') {
+ ixp_werrstr("$DISPLAY is unset");
+ return nil;
+ }
+
+ disp = estrdup(disp);
+ path = &disp[strlen(disp) - 2];
+ if(path > disp && !strcmp(path, ".0"))
+ *path = '\0';
+
+ path = ixp_smprint("/tmp/ns.%s.%s", _user(), disp);
+ free(disp);
+
+ if(!rmkdir(path, 0700))
+ ;
+ else if(stat(path, &st))
+ ixp_werrstr("Can't stat ns_path '%s': %r", path);
+ else if(getuid() != st.st_uid)
+ ixp_werrstr("ns_path '%s' exists but is not owned by you", path);
+ else if((st.st_mode & 077) && chmod(path, st.st_mode & ~077))
+ ixp_werrstr("Namespace path '%s' exists, but has wrong permissions: %r", path);
+ else
+ return path;
+ free(path);
+ return nil;
+}
+
+/**
+ * Function: ixp_namespace
+ *
+ * Returns the path of the canonical 9p namespace directory.
+ * Either the value of $NAMESPACE, if it's set, or, roughly,
+ * /tmp/ns.${USER}.${DISPLAY:%.0=%}. In the latter case, the
+ * directory is created if it doesn't exist, and it is
+ * ensured to be owned by the current user, with no group or
+ * other permissions.
+ *
+ * Returns:
+ * A statically allocated string which must not be freed
+ * or altered by the caller. The same value is returned
+ * upon successive calls.
+ */
+/* Not especially threadsafe. */
+char*
+ixp_namespace(void) {
+ static char *namespace;
+
+ if(namespace == nil)
+ namespace = getenv("NAMESPACE");
+ if(namespace == nil)
+ namespace = ns_display();
+ return namespace;
+}
 
 void
 eprint(const char *fmt, ...) {
@@ -38,7 +152,7 @@ mfatal(char *name, uint size) {
         char sizestr[8];
         int i;
         
- i = sizeof(sizestr);
+ i = sizeof sizestr;
         do {
                 sizestr[--i] = '0' + (size%10);
                 size /= 10;
@@ -115,12 +229,12 @@ strlcat(char *dst, const char *src, uint
                 d++;
         len = n;
 
- while(*s != '\0') {
- if(n-- > 0)
- *d++ = *s;
- s++;
- }
+ while(*s != '\0' && n-- > 0)
+ *d++ = *s++;
+ while(*s++ != '\0')
+ n--;
         if(len > 0)
                 *d = '\0';
         return size - n - 1;
 }
+
diff -r 3e1f5e86e99b -r 85de01452e0a libixp_rubythread/thread_ruby.c
--- a/libixp_rubythread/thread_ruby.c Sun Jan 27 13:48:38 2008 -0500
+++ b/libixp_rubythread/thread_ruby.c Sun Feb 03 17:10:10 2008 -0500
@@ -145,14 +145,26 @@ rwakeall(IxpRendez *r) {
 /* Yielding IO */
 static ssize_t
 _read(int fd, void *buf, size_t size) {
+ int n;
+
         rb_thread_wait_fd(fd);
- return read(fd, buf, size);
+ n = read(fd, buf, size);
+
+ if(n < 0 && errno == EINTR)
+ rb_thread_schedule();
+ return n;
 }
 
 static ssize_t
 _write(int fd, const void *buf, size_t size) {
+ int n;
+
         rb_thread_fd_writable(fd);
- return write(fd, buf, size);
+ n = write(fd, buf, size);
+
+ if(n < 0 && errno == EINTR)
+ rb_thread_schedule();
+ return n;
 }
 
 static IxpThread ixp_rthread = {
diff -r 3e1f5e86e99b -r 85de01452e0a mk/lib.mk
--- a/mk/lib.mk Sun Jan 27 13:48:38 2008 -0500
+++ b/mk/lib.mk Sun Feb 03 17:10:10 2008 -0500
@@ -11,6 +11,8 @@ depend: ${OBJ:=.depend}
 
 libclean:
         for i in $(LIB) $(OFILES); do \
+ [ -e $$i ] && \
+ echo CLEAN $$($(CLEANNAME) $(BASE)$$i); \
                 rm -f $$i; \
         done 2>/dev/null || true
 
diff -r 3e1f5e86e99b -r 85de01452e0a mk/many.mk
--- a/mk/many.mk Sun Jan 27 13:48:38 2008 -0500
+++ b/mk/many.mk Sun Feb 03 17:10:10 2008 -0500
@@ -13,6 +13,7 @@ printinstall:
 
 manyclean:
         for i in ${TARG:=.o} ${TARG:=.O} $(OFILES); do \
+ [ -e $$i ] && \
                 echo CLEAN $$($(CLEANNAME) $(BASE)$$i); \
                 rm -f $$i; \
         done 2>/dev/null || true
diff -r 3e1f5e86e99b -r 85de01452e0a mk/one.mk
--- a/mk/one.mk Sun Jan 27 13:48:38 2008 -0500
+++ b/mk/one.mk Sun Feb 03 17:10:10 2008 -0500
@@ -14,6 +14,7 @@ printinstall:
 
 oneclean:
         for i in $(PROG) $(OFILES); do \
+ [ -e $$i ] && \
                 echo CLEAN $$($(CLEANNAME) $(BASE)$$i); \
                 rm -f $$i; \
         done 2>/dev/null || true
diff -r 3e1f5e86e99b -r 85de01452e0a mk/so.mk
--- a/mk/so.mk Sun Jan 27 13:48:38 2008 -0500
+++ b/mk/so.mk Sun Feb 03 17:10:10 2008 -0500
@@ -12,6 +12,8 @@ depend: ${OBJ:=.depend}
 
 soclean:
         for i in $(SO) $(OFILES_PIC); do \
+ [ -e $$i ] && \
+ echo CLEAN $$($(CLEANNAME) $(BASE)$$i); \
                 rm -f $$i; \
         done 2>/dev/null || true
 
diff -r 3e1f5e86e99b -r 85de01452e0a util/compile
--- a/util/compile Sun Jan 27 13:48:38 2008 -0500
+++ b/util/compile Sun Feb 03 17:10:10 2008 -0500
@@ -9,7 +9,7 @@ xtmp=/tmp/cc.$$.$USER.out
 
 echo CC $($bin/cleanname ${BASE}$outfile)
 [ -n "$noisycc" ] && echo $CC -o $outfile $CFLAGS $@
-$CC -o $outfile $CFLAGS $@ 2>$xtmp
+$CC -o $outfile $CFLAGS $@ >$xtmp 2>&1
 status=$?
 
 base=$(echo $BASE | sed 's/,/\\,/g')
@@ -57,6 +57,8 @@ cat $xtmp | sed "s,^$re,$base&,g; s,\([[
 cat $xtmp | sed "s,^$re,$base&,g; s,\([[:space:]]\)$re,\1$base\2,g" |
         egrep -v ': error: .Each undeclared identifier|: error: for each function it appears|is dangerous, better use|is almost always misused|: In function |: At top level:|support .long long.|use of C99 long long|ISO C forbids conversion' |
         sed 's/ .first use in this function.$//; s/\"\([^\"][^\"]*\)\", line \([0-9][0-9]*\)/\1:\2/g' |
+ awk '$1 == "warning:"{t=$2" "$1; sub(/^[^ ]+ [^ ]+ /, ""); $0 = t" "$0}; //' |
+ awk '{sub(/\[/, ": [", $1); print}' |
         undup 1>&2
 
 rm -f $xtmp
Received on Sun Feb 03 2008 - 23:11:06 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:59:16 UTC