xk and xps use the xp_defaultuser() function of libxcpu to get the admin
user and key.

for xk, it first tries as xcpu-admin and if that fails as the user
itself. xps works for all.

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

p.s. longer lines are truncated when the patch is pasted inline, so i am
attaching the diff too.

On Wed, 2008-11-26 at 18:46 -0700, Abhishek Kulkarni wrote:
> xk and xps use the xp_defaultuser() function of libxcpu to get the admin
> user and key.
> 
> for xk, it first tries as xcpu-admin and if that fails as the user
> itself. xps works for all.
> 
> Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>
> 
> Index: utils/xk.c
> ===================================================================
> --- utils/xk.c        (revision 739)
> +++ utils/xk.c        (working copy)
> @@ -50,54 +50,9 @@
>  
>  extern int spc_chatty;
>  
> -static Spuser *user;
> -static Xkey *ukey;
> -static char *afname;
> -
> -static int
> -init_user(void)
> -{
> -     char *homepath, keypath[128];
> -     int xcpu_uid;
> -     struct passwd *xcpu_admin;
> -     Spuserpool *upool;
> -     
> -     
> -     if (!afname) 
> -             ukey = xauth_privkey_create("/etc/xcpu/admin_key");
> -     else 
> -             ukey = xauth_privkey_create(afname);
> -     
> -     if (ukey) {
> -             upool = sp_priv_userpool_create();
> -             xcpu_admin = getpwnam("xcpu-admin");
> -             if (xcpu_admin) 
> -                     xcpu_uid = xcpu_admin->pw_uid;
> -             else 
> -                     xcpu_uid = 65530; 
> -             
> -             user = sp_priv_user_add(upool, "xcpu-admin", xcpu_uid, ukey);
> -             if (!user) 
> -                     return -1;
> -     }
> -     
> -     else {
> -             user = sp_unix_users->uid2user(sp_unix_users, geteuid());
> -             if (!user)
> -                     return -1;
> -             homepath = getenv("HOME");
> -             snprintf(keypath, sizeof(keypath), "%s/.ssh/id_rsa", homepath);
> -             ukey = xauth_privkey_create(keypath);
> -             if (!ukey)
> -                     return -1;
> -     }
> -     
> -     return 0;
> -}
> -
>  void 
>  usage(char *argv) {
> -     fprintf(stderr, "usage: %s [-dhjm] [-a adminkey] sig host:<pid|jid>
> [host:<pid|jid> ...]\n", argv);
> +     fprintf(stderr, "usage: %s [-dhjm] [-A adminkey] sig host:<pid|jid>
> [host:<pid|jid> ...]\n", argv);
>       exit(1);
>  }
>  
> @@ -112,9 +67,11 @@
>       Xpproc *procs, *xp;
>       char *s;
>       char statserver[32];
> +     static Spuser *auser;
> +     static Xkey *akey;
> +     static char *adminkey = NULL;
>  
> -     afname = 0;
> -     while ((c = getopt(argc, argv, "+dhja:m")) != -1) {
> +     while ((c = getopt(argc, argv, "+dhjA:m")) != -1) {
>               switch (c) {
>               case 'd':
>                       spc_chatty = 1;
> @@ -122,8 +79,8 @@
>               case 'j':
>                       killjob++;
>                       break;
> -             case 'a':
> -                     afname = optarg;
> +             case 'A':
> +                     adminkey = strdup(optarg);
>                       break;
>               case 'm':
>                       match = 1;
> @@ -142,11 +99,19 @@
>               fprintf(stderr, "bad signal argument %s: expected int",
> argv[optind-1]);
>               usage(argv[0]);
>       }
> -     
> -     
> -     if (init_user() < 0)
> -             goto error;
>  
> +     if (adminkey) {
> +             akey = xauth_privkey_create(adminkey);
> +             if (!akey)
> +                     goto error;
> +     }
> +
> +     if (xp_defaultuser(&auser, &akey) < 0) {
> +             if (akey)
> +                     xauth_destroy(akey);
> +             goto error;
> +     }
> +             
>       for(;optind < argc; optind++) {
>               int isjid = 0;
>               int pid = -1;
> @@ -178,7 +143,7 @@
>                       }
>               }
>               
> -             n = xp_proc_list(ns, user, ukey, &procs);
> +             n = xp_proc_list(ns, auser, akey, &procs);
>               if (n < 0) {
>                       fprintf(stderr, "can not obtain process list for 
> nodeset %s;
> skipping\n", argv[optind]);
>                       continue;
> @@ -191,20 +156,20 @@
>                       if(isjid || match) {
>                               if (xp->xcpujid) {
>                                       if(!match && !strcmp(xp->xcpujid, id))
> -                                             xp_proc_kill(xp, user, ukey, 
> signal);
> +                                             xp_proc_kill(xp, auser, akey, 
> signal);
>                                       else if(match) {
>                                               tmpjid = 
> malloc(strlen(xp->xcpujid) + 1);
>                                               strcpy(tmpjid, xp->xcpujid);
>                                               slash = strchr(tmpjid, '/');
>                                               *slash = '\0';
>                                               if (!strcmp(tmpjid, id))
> -                                                     xp_proc_kill(xp, user, 
> ukey, signal);
> +                                                     xp_proc_kill(xp, auser, 
> akey, signal);
>                                               free(tmpjid);
>                                       }
>                               }
>                       } else {
>                               if(xp->pid == pid)
> -                                     xp_proc_kill(xp, user, ukey, signal);
> +                                     xp_proc_kill(xp, auser, akey, signal);
>                       }
>               }
>       }
> Index: utils/xps.c
> ===================================================================
> --- utils/xps.c       (revision 739)
> +++ utils/xps.c       (working copy)
> @@ -48,29 +48,9 @@
>  static void printpinfo(Xpproc *xp, char *stat);
>  
>  extern int spc_chatty;
> -static Spuser *user;
> -static Xkey *ukey;
>  
> -static int
> -init_user(void)
> -{
> -     char *homepath, keypath[128];
> -
> -     user = sp_unix_users->uid2user(sp_unix_users, geteuid());
> -     if (!user)
> -             return -1;
> -
> -     homepath = getenv("HOME");
> -     snprintf(keypath, sizeof(keypath), "%s/.ssh/id_rsa", homepath);
> -     ukey = xauth_privkey_create(keypath);
> -     if (!ukey)
> -             return -1;
> -
> -     return 0;
> -}
> -
>  void usage() {
> -     fprintf(stderr, "usage: xps [-dax] [-J JobId] host,...\n");
> +     fprintf(stderr, "usage: xps [-dax] [-p port] [-J JobId] host,...\n");
>       exit(1);
>  }
>  
> @@ -80,16 +60,18 @@
>       int i, n, c, jlen, ecode;
>       int min, sec, allflag = 0;
>       long long int t;
> -     int xonly = 0;
> -     char *ename, buf[256], *jobid;
> +     int xonly = 0, port = STAT_PORT;
> +     char *ename, buf[256], *jobid, *end;
>       Xpnodeset *nds, *nds2;
>       Xpproc *procs, *xp;
>       char stat[32];
>       char tim[64];
> +     static Spuser *auser;
> +     static Xkey *akey;
>  
>       jlen = 0;
>       jobid = NULL;
> -     while ((c = getopt(argc, argv, "+daxJ:")) != -1) {
> +     while ((c = getopt(argc, argv, "+daxp:J:h")) != -1) {
>               switch (c) {
>               case 'd':
>                       spc_chatty = 1;
> @@ -113,6 +95,12 @@
>                       xonly = 1;
>                       break;
>  
> +             case 'p':
> +                     port = strtol(optarg, &end, 10);
> +                     if (*end != '\0')
> +                             usage(argv[0]);
> +                     break;
> +             case 'h':
>               default:
>                       usage();
>               }
> @@ -120,7 +108,7 @@
>  
>       if (allflag) {
>               char statserver[32];
> -             sprintf(statserver, "localhost!%d", STAT_PORT);
> +             sprintf(statserver, "localhost!%d", port);
>               nds = xp_nodeset_list(NULL);
>               if(nds == NULL)
>                       nds = xp_nodeset_list(statserver);
> @@ -145,13 +133,12 @@
>       if (!nds)
>               goto error;
>  
> -     if (init_user() < 0)
> +     if (xp_defaultuser(&auser, &akey) < 0)
>               goto error;
>  
> -     n = xp_proc_list(nds, user, ukey, &procs);
> +     n = xp_proc_list(nds, auser, akey, &procs);
>       if (n < 0)
>               goto error;
> -
>       
>       printf("NODE\tPID\tTTY\tSTAT\tTIME\tJOBID\tCOMMAND\n");
>       for(i = 0; i < n; i++) {
> 
> 
Index: utils/xk.c
===================================================================
--- utils/xk.c	(revision 741)
+++ utils/xk.c	(working copy)
@@ -50,54 +50,9 @@
 
 extern int spc_chatty;
 
-static Spuser *user;
-static Xkey *ukey;
-static char *afname;
-
-static int
-init_user(void)
-{
-	char *homepath, keypath[128];
-	int xcpu_uid;
-	struct passwd *xcpu_admin;
-	Spuserpool *upool;
-	
-	
-	if (!afname) 
-		ukey = xauth_privkey_create("/etc/xcpu/admin_key");
-	else 
-		ukey = xauth_privkey_create(afname);
-	
-	if (ukey) {
-		upool = sp_priv_userpool_create();
-		xcpu_admin = getpwnam("xcpu-admin");
-		if (xcpu_admin) 
-			xcpu_uid = xcpu_admin->pw_uid;
-		else 
-			xcpu_uid = 65530; 
-		
-		user = sp_priv_user_add(upool, "xcpu-admin", xcpu_uid, ukey);
-		if (!user) 
-			return -1;
-	}
-	
-	else {
-		user = sp_unix_users->uid2user(sp_unix_users, geteuid());
-		if (!user)
-			return -1;
-		homepath = getenv("HOME");
-		snprintf(keypath, sizeof(keypath), "%s/.ssh/id_rsa", homepath);
-		ukey = xauth_privkey_create(keypath);
-		if (!ukey)
-			return -1;
-	}
-	
-	return 0;
-}
-
 void 
 usage(char *argv) {
-	fprintf(stderr, "usage: %s [-dhjm] [-a adminkey] sig host:<pid|jid> [host:<pid|jid> ...]\n", argv);
+	fprintf(stderr, "usage: %s [-dhjm] [-A adminkey] sig host:<pid|jid> [host:<pid|jid> ...]\n", argv);
 	exit(1);
 }
 
@@ -112,9 +67,11 @@
 	Xpproc *procs, *xp;
 	char *s;
 	char statserver[32];
+	static Spuser *auser;
+	static Xkey *akey;
+	static char *adminkey = NULL;
 
-	afname = 0;
-	while ((c = getopt(argc, argv, "+dhja:m")) != -1) {
+	while ((c = getopt(argc, argv, "+dhjA:m")) != -1) {
 		switch (c) {
 		case 'd':
 			spc_chatty = 1;
@@ -122,8 +79,8 @@
 		case 'j':
 			killjob++;
 			break;
-		case 'a':
-		        afname = optarg;
+		case 'A':
+		        adminkey = strdup(optarg);
 			break;
 		case 'm':
 		        match = 1;
@@ -142,11 +99,19 @@
 		fprintf(stderr, "bad signal argument %s: expected int", argv[optind-1]);
 		usage(argv[0]);
 	}
-	
-	
-	if (init_user() < 0)
-		goto error;
 
+	if (adminkey) {
+		akey = xauth_privkey_create(adminkey);
+		if (!akey)
+			goto error;
+	}
+
+	if (xp_defaultuser(&auser, &akey) < 0) {
+		if (akey)
+			xauth_destroy(akey);
+		goto error;
+	}
+		
 	for(;optind < argc; optind++) {
 		int isjid = 0;
 		int pid = -1;
@@ -178,7 +143,7 @@
 			}
 		}
 		
-		n = xp_proc_list(ns, user, ukey, &procs);
+		n = xp_proc_list(ns, auser, akey, &procs);
 		if (n < 0) {
 			fprintf(stderr, "can not obtain process list for nodeset %s; skipping\n", argv[optind]);
 			continue;
@@ -191,20 +156,20 @@
 			if(isjid || match) {
 				if (xp->xcpujid) {
 					if(!match && !strcmp(xp->xcpujid, id))
-						xp_proc_kill(xp, user, ukey, signal);
+						xp_proc_kill(xp, auser, akey, signal);
 					else if(match) {
 						tmpjid = malloc(strlen(xp->xcpujid) + 1);
 						strcpy(tmpjid, xp->xcpujid);
 						slash = strchr(tmpjid, '/');
 						*slash = '\0';
 						if (!strcmp(tmpjid, id))
-							xp_proc_kill(xp, user, ukey, signal);
+							xp_proc_kill(xp, auser, akey, signal);
 						free(tmpjid);
 					}
 				}
 			} else {
 				if(xp->pid == pid)
-					xp_proc_kill(xp, user, ukey, signal);
+					xp_proc_kill(xp, auser, akey, signal);
 			}
 		}
 	}
Index: utils/xps.c
===================================================================
--- utils/xps.c	(revision 741)
+++ utils/xps.c	(working copy)
@@ -48,29 +48,9 @@
 static void printpinfo(Xpproc *xp, char *stat);
 
 extern int spc_chatty;
-static Spuser *user;
-static Xkey *ukey;
 
-static int
-init_user(void)
-{
-	char *homepath, keypath[128];
-
-	user = sp_unix_users->uid2user(sp_unix_users, geteuid());
-	if (!user)
-		return -1;
-
-	homepath = getenv("HOME");
-	snprintf(keypath, sizeof(keypath), "%s/.ssh/id_rsa", homepath);
-	ukey = xauth_privkey_create(keypath);
-	if (!ukey)
-		return -1;
-
-	return 0;
-}
-
 void usage() {
-	fprintf(stderr, "usage: xps [-dax] [-J JobId] host,...\n");
+	fprintf(stderr, "usage: xps [-dax] [-p port] [-J JobId] host,...\n");
 	exit(1);
 }
 
@@ -80,16 +60,18 @@
 	int i, n, c, jlen, ecode;
 	int min, sec, allflag = 0;
 	long long int t;
-	int xonly = 0;
-	char *ename, buf[256], *jobid;
+	int xonly = 0, port = STAT_PORT;
+	char *ename, buf[256], *jobid, *end;
 	Xpnodeset *nds, *nds2;
 	Xpproc *procs, *xp;
 	char stat[32];
 	char tim[64];
+	static Spuser *auser;
+	static Xkey *akey;
 
 	jlen = 0;
 	jobid = NULL;
-	while ((c = getopt(argc, argv, "+daxJ:")) != -1) {
+	while ((c = getopt(argc, argv, "+daxp:J:h")) != -1) {
 		switch (c) {
 		case 'd':
 			spc_chatty = 1;
@@ -113,6 +95,12 @@
 		        xonly = 1;
 	                break;
 
+		case 'p':
+			port = strtol(optarg, &end, 10);
+			if (*end != '\0')
+				usage(argv[0]);
+			break;
+		case 'h':
 		default:
 			usage();
 		}
@@ -120,7 +108,7 @@
 
 	if (allflag) {
 		char statserver[32];
-		sprintf(statserver, "localhost!%d", STAT_PORT);
+		sprintf(statserver, "localhost!%d", port);
 		nds = xp_nodeset_list(NULL);
 		if(nds == NULL)
 			nds = xp_nodeset_list(statserver);
@@ -145,13 +133,12 @@
 	if (!nds)
 		goto error;
 
-	if (init_user() < 0)
+	if (xp_defaultuser(&auser, &akey) < 0)
 		goto error;
 
-	n = xp_proc_list(nds, user, ukey, &procs);
+	n = xp_proc_list(nds, auser, akey, &procs);
 	if (n < 0)
 		goto error;
-
 	
 	printf("NODE\tPID\tTTY\tSTAT\tTIME\tJOBID\tCOMMAND\n");
 	for(i = 0; i < n; i++) {

Reply via email to