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)