forgot to svn add group.c, here is the updated patch.

Moved the user and group functions to libxcpu.
libxcpu now provides an API to add/delete/flush users.

for xuserset and xgroupset, it's just a matter of calling these
functions now.

the main motivation for this patch is that schedulers need to call these
functions to add/delete users from the group pool when they submit a
job.

Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>



Index: user.c
===================================================================
--- user.c	(revision 715)
+++ user.c	(working copy)
@@ -24,8 +24,19 @@
 #include "libxauth.h"
 #include "libxcpu.h"
 
+typedef struct Ubuf Ubuf;
+struct Ubuf {
+	int size;
+	char *buf;
+	char *uname;
+	u32 uid;
+	char *gname;
+	Xkey *adminkey;
+	Spuser *adminuser;
+};
+
 int
-defaultuser(Spuser **puser, Spgroup **pgroup)
+xp_defaultuser(Spuser **puser, Spgroup **pgroup)
 {
 	static Spuserpool *upool = NULL;
 	static struct passwd *xcpu_admin = NULL;
@@ -81,3 +92,182 @@
 
 }
 
+static int
+user_cmd(Xpnode *nd, void *cba)
+{
+	Ubuf *cmdbuf = cba;
+	Spcfsys *fs;
+	Spcfid *fid;
+	Spwstat wst;
+
+	fs = xp_node_mount(nd, cmdbuf->adminuser, cmdbuf->adminkey);
+	if (!fs)
+		goto error;
+
+	fid = spc_open(fs, "ctl", Owrite);
+	if (!fid) 
+		goto error;
+
+	if (spc_write(fid, (u8 *) cmdbuf->buf, cmdbuf->size, 0) < 0)
+		goto error;
+
+	spc_close(fid);
+	wst.type = ~0;
+	wst.dev = ~0;
+	wst.qid.type = ~0;
+	wst.qid.version = ~0;
+	wst.qid.path = ~0;
+	wst.mode = ~0;
+	wst.atime = ~0;
+	wst.mtime = ~0;
+	wst.name = NULL;
+	wst.length = ~0;
+	wst.uid = (cmdbuf->uname) ? cmdbuf->uname : cmdbuf->adminuser->uname;
+	wst.gid = (cmdbuf->gname) ? cmdbuf->gname : cmdbuf->adminuser->uname;
+	wst.muid = NULL;
+	wst.extension = NULL;
+	wst.n_uid = (cmdbuf->uid) ? cmdbuf->uid : cmdbuf->adminuser->uid;
+	wst.n_gid = ~0;
+	wst.n_muid = ~0;
+
+	if (spc_wstat(fs, "clone", &wst) < 0)
+		goto error;
+	spc_umount(fs);
+	return 0;
+error:
+	if (fs)
+		spc_umount(fs);
+	return -1;
+}
+
+static inline Ubuf *
+ubuf_init(char *buf, int size, char *uname, u32 uid, char *gname, Xkey *adminkey)
+{
+	struct passwd *xcpu_admin;
+	Ubuf *upbuf = sp_malloc(sizeof(*upbuf));
+	if (!upbuf)
+		return NULL;
+
+	upbuf->buf = buf;
+	upbuf->size = size;
+	upbuf->uname = uname;
+	upbuf->uid = uid;
+	upbuf->gname = gname;
+	upbuf->adminkey = adminkey;
+
+	upbuf->adminuser = sp_malloc(sizeof(*upbuf->adminuser));
+	upbuf->adminuser->uname = strdup("xcpu-admin");
+	upbuf->adminuser->uid = 65530;
+	xcpu_admin = getpwnam(upbuf->adminuser->uname);
+	if (xcpu_admin)
+		upbuf->adminuser->uid = xcpu_admin->pw_uid;
+
+	return upbuf;
+}
+
+static inline void
+ubuf_destroy(Ubuf *buf)
+{
+	if (buf->adminuser)
+		free(buf->adminuser);
+
+	free(buf);
+}
+
+int
+xp_user_add(Xpnodeset *nds, Xkey *adminkey, char *uname, uid_t uid, char *gname, char *ukey)
+{
+	int bufsize = 4096, buflen;
+	char *buf, *qkey;
+	Ubuf *addbuf;
+
+	if (!nds)
+		return -1;
+	
+	buf = sp_malloc(sizeof(*buf) * bufsize);
+	qkey = quotestrdup(ukey);
+	snprintf(buf, bufsize, "user-add %s %d %s %s\n", uname, uid, gname, qkey);
+	free(qkey);
+	buflen = strlen(buf);
+	addbuf = ubuf_init(buf, buflen, uname, uid, gname, adminkey);
+	if (!addbuf)
+		goto error;
+	
+	if (xp_nodeset_iterate(nds, user_cmd, addbuf) > 0)
+		goto error;
+
+	free(buf);
+	ubuf_destroy(addbuf);
+
+	return 0;
+error:
+	free(buf);
+	if (addbuf)
+		ubuf_destroy(addbuf);
+
+	xp_nodeerror_print("xp_user_add");
+	return -1;
+}
+
+int
+xp_user_del(Xpnodeset *nds, Xkey *adminkey, char *uname)
+{
+	int bufsize = 1024, buflen;
+	char *buf;
+	Ubuf *delbuf;
+
+	if (!nds)
+		return -1;
+	
+	buf = sp_malloc(sizeof(*buf) * bufsize);
+	snprintf(buf, bufsize, "user-del %s\n", uname);
+	buflen = strlen(buf);
+	delbuf = ubuf_init(buf, buflen, NULL, 0, NULL, adminkey);
+	if (!delbuf)
+		goto error;
+	
+	if (xp_nodeset_iterate(nds, user_cmd, delbuf) > 0)
+		goto error;
+
+	free(buf);
+	ubuf_destroy(delbuf);
+	return 0;
+error:
+	free(buf);
+	if (delbuf)
+		ubuf_destroy(delbuf);
+
+	xp_nodeerror_print("xp_user_del");
+	return -1;
+}
+
+int
+xp_user_flush(Xpnodeset *nds, Xkey *adminkey)
+{
+	int buflen;
+	char *buf;
+	Ubuf *flbuf;
+
+	if (!nds)
+		return -1;
+	
+	buf = strdup("user-flush\n");
+	buflen = strlen(buf);
+	flbuf = ubuf_init(buf, buflen, NULL, 0, NULL, adminkey);
+	if (!flbuf)
+		goto error;
+
+	if (xp_nodeset_iterate(nds, user_cmd, flbuf) > 0)
+		goto error;
+
+	free(buf);
+	ubuf_destroy(flbuf);
+	return 0;
+error:
+	free(buf);
+	if (flbuf)
+		ubuf_destroy(flbuf);
+
+	xp_nodeerror_print("xp_user_flush");
+	return -1;
+}
Index: node.c
===================================================================
--- node.c	(revision 715)
+++ node.c	(working copy)
@@ -820,38 +820,3 @@
 	return -1;
 }
 */
-
-/*
-int
-xp_nodeset_add_user(Xpnodeset *nds, Xkey *adminkey, char *uname, u32 uid, 
-	char *gname, char *ukey)
-{
-	int i, buflen;
-	char buf[4096], *qkey;
-	Spcfsys *fs;
-	Spcfid *fid;
-
-	qkey = quotestrdup(ukey);
-	snprintf(buf, sizeof(buf), "user-add %s %d %s %s\n", uname, uid, gname, qkey);
-	free(qkey);
-	buflen = strlen(buf);
-
-	for(i = 0; i < nds->len; i++) {
-		fs = xp_node_mount(&nds->nodes[i], &adminuser, adminkey);
-		if (!fs)
-			return -1;
-
-		fid = spc_open(fs, "ctl", Owrite);
-		if (!fid) {
-			spc_umount(fs);
-			return -1;
-		}
-
-		spc_write(fid, (u8 *) buf, buflen, 0);
-		spc_close(fid);
-		spc_umount(fs);
-	}
-
-	return 0;
-}
-*/
Index: Makefile
===================================================================
--- Makefile	(revision 715)
+++ Makefile	(working copy)
@@ -16,6 +16,7 @@
 	proc.o\
 	session.o\
 	user.o\
+	group.o\
 	util.o\
 
 libxcpu.a: $(LIBFILES)
Index: group.c
===================================================================
--- group.c	(revision 0)
+++ group.c	(revision 0)
@@ -0,0 +1,187 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <limits.h>
+#include <sys/stat.h>
+#include <sys/utsname.h>
+#include <sys/wait.h>
+#include <sys/param.h>
+#include <signal.h>
+#include <dirent.h>
+#include <signal.h>
+#include <regex.h>
+#include <math.h>
+#include <sys/types.h>
+#include <pwd.h>
+
+#include "spfs.h"
+#include "spclient.h"
+#include "strutil.h"
+#include "libxauth.h"
+#include "libxcpu.h"
+
+typedef struct Gbuf Gbuf;
+struct Gbuf {
+	int size;
+	char *buf;
+	Xkey *adminkey;
+	Spuser *adminuser;
+};
+
+static int
+group_cmd(Xpnode *nd, void *cba)
+{
+	Gbuf *cmdbuf = cba;
+	Spcfsys *fs;
+	Spcfid *fid;
+
+	fs = xp_node_mount(nd, cmdbuf->adminuser, cmdbuf->adminkey);
+	if (!fs)
+		goto error;
+
+	fid = spc_open(fs, "ctl", Owrite);
+	if (!fid) 
+		goto error;
+
+	if (spc_write(fid, (u8 *) cmdbuf->buf, cmdbuf->size, 0) < 0)
+		goto error;
+
+	spc_close(fid);
+	spc_umount(fs);
+	return 0;
+
+error:
+	if (fs)
+		spc_umount(fs);
+	return -1;
+}
+
+static inline Gbuf *
+gbuf_init(char *buf, int size, Xkey *adminkey)
+{
+	struct passwd *xcpu_admin;
+	Gbuf *gpbuf = sp_malloc(sizeof(*gpbuf));
+	if (!gpbuf)
+		return NULL;
+
+	gpbuf->buf = buf;
+	gpbuf->size = size;
+	gpbuf->adminkey = adminkey;
+
+	gpbuf->adminuser = sp_malloc(sizeof(*gpbuf->adminuser));
+	gpbuf->adminuser->uname = strdup("xcpu-admin");
+	gpbuf->adminuser->uid = 65530;
+	xcpu_admin = getpwnam(gpbuf->adminuser->uname);
+	if (xcpu_admin)
+		gpbuf->adminuser->uid = xcpu_admin->pw_uid;
+
+	return gpbuf;
+}
+
+static inline void
+gbuf_destroy(Gbuf *buf)
+{
+	if(buf->adminuser)
+		free(buf->adminuser);
+
+	free(buf);
+}
+
+int
+xp_group_add(Xpnodeset *nds, Xkey *adminkey, char *gname, gid_t gid)
+{
+	int bufsize = 4096, buflen;
+	char *buf;
+	Gbuf *addbuf;
+
+	if (!nds)
+		return -1;
+	
+	buf = sp_malloc(sizeof(*buf) * bufsize);
+	snprintf(buf, bufsize, "group-add %s %d\n", gname, gid);
+	buflen = strlen(buf);
+	addbuf = gbuf_init(buf, buflen, adminkey);
+	if (!addbuf)
+		goto error;
+
+	if (xp_nodeset_iterate(nds, group_cmd, addbuf) > 0)
+		goto error;
+
+	free(buf);
+	gbuf_destroy(addbuf);
+	return 0;
+error:
+	free(buf);
+	if (addbuf)
+		gbuf_destroy(addbuf);
+
+	xp_nodeerror_print("xp_group_add");
+	return -1;
+}
+
+int
+xp_group_del(Xpnodeset *nds, Xkey *adminkey, char *gname)
+{
+	int bufsize = 1024, buflen;
+	char *buf;
+	Gbuf *delbuf;
+
+	if (!nds)
+		return -1;
+	
+	buf = sp_malloc(sizeof(*buf) * bufsize);
+	snprintf(buf, bufsize, "group-del %s\n", gname);
+	buflen = strlen(buf);
+	delbuf = gbuf_init(buf, buflen, adminkey);
+	if (!delbuf)
+		goto error;
+
+	if (xp_nodeset_iterate(nds, group_cmd, delbuf) > 0)
+		goto error;
+
+	free(buf);
+	gbuf_destroy(delbuf);
+	return 0;
+error:
+	free(buf);
+	if (delbuf)
+		gbuf_destroy(delbuf);
+
+	xp_nodeerror_print("xp_group_del");
+	return -1;
+}
+
+int
+xp_group_flush(Xpnodeset *nds, Xkey *adminkey)
+{
+	int buflen;
+	char *buf;
+	Gbuf *flbuf;
+
+	if (!nds)
+		return -1;
+	
+	buf = strdup("group-flush\n");
+	buflen = strlen(buf);
+	flbuf = gbuf_init(buf, buflen, adminkey);
+	if (!flbuf)
+		goto error;
+
+	if (xp_nodeset_iterate(nds, group_cmd, flbuf) > 0)
+		goto error;
+
+	free(buf);
+	gbuf_destroy(flbuf);	       
+	return 0;
+error:
+	free(buf);
+	if (flbuf)
+		gbuf_destroy(flbuf);
+
+	xp_nodeerror_print("xp_group_flush");
+	return -1;
+}

Reply via email to