I noticed that when a regular user tried to kill a non-xcpu job process,
xp_proc_kill segfaulted. xp_proc_kill assumed that if the user
submitting the job is not "xcpu-admin" the process must be a xcpu
session job.
Any user belonging to "xcpu-admin" group can kill any process on any
node. This patch also makes xk print an error if it cannot kill a
process.
Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>
Index: libxcpu/proc.c
===================================================================
--- libxcpu/proc.c (revision 744)
+++ libxcpu/proc.c (working copy)
@@ -82,45 +82,51 @@
int
xp_proc_kill(Xpproc *xp, Spuser *user, Xkey *ukey, int signo)
{
- int n;
- char buf[64];
- Spcfsys *fs;
- Spcfid *fid;
- char ctl[16];
+ int i, n;
+ char buf[32];
+ Spcfsys *fs = NULL;
+ Spcfid *fid = NULL;
+ char *ctl = NULL;
fs = xp_node_mount(xp->node, user, ukey);
if (!fs)
- return -1;
+ goto error;
- if (!strcmp(user->uname, "xcpu-admin")) {
- strcpy(ctl, "ctl");
+ fid = spc_open(fs, "ctl", Owrite);
+ if (!fid) {
+ if (!xp->xcpusid)
+ goto error;
+
+ n = strlen(xp->xcpusid) + 5;
+ ctl = sp_malloc(n);
+ snprintf(ctl, n, "%s/ctl", xp->xcpusid);
+ fid = spc_open(fs, ctl, Owrite);
+ if (!fid)
+ goto error;
+ }
+
+ if (!ctl)
snprintf(buf, sizeof(buf), "kill %d %d\n", signo, xp->pid);
- }
- else {
- strcpy(ctl, xp->xcpusid);
- strcat(ctl, "/ctl");
+ else
snprintf(buf, sizeof(buf), "signal %d\n", signo);
- }
- fid = spc_open(fs, ctl, Owrite);
- if (!fid) {
- spc_umount(fs);
- return -1;
- }
-
n = spc_write(fid, (u8 *) buf, strlen(buf), 0);
- if (n != strlen(buf)) {
- if (n >= 0)
- sp_werror("error while writing", EIO);
-
- spc_close(fid);
- spc_umount(fs);
- return -1;
- }
+ if (n != strlen(buf) || n <= 0)
+ goto error;
spc_close(fid);
spc_umount(fs);
+ free(ctl);
return 0;
+error:
+ if (fid)
+ spc_close(fid);
+ if (fs)
+ spc_umount(fs);
+
+ sp_werror("%s: error killing process %d", EIO, xp->node->name, xp->pid);
+ free(ctl);
+ return -1;
}
static int
Index: utils/xk.c
===================================================================
--- utils/xk.c (revision 744)
+++ utils/xk.c (working copy)
@@ -155,21 +155,30 @@
xp = &procs[i];
if(isjid || match) {
if (xp->xcpujid) {
- if(!match && !strcmp(xp->xcpujid, id))
- xp_proc_kill(xp, auser, akey, signal);
- else if(match) {
+ if(!match && !strcmp(xp->xcpujid, id)) {
+ if (xp_proc_kill(xp, auser, akey, signal) < 0) {
+ sp_rerror(&ename, &ecode);
+ fprintf(stderr, "xp_proc_kill: %s.\n", ename);
+ }
+ } 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, auser, akey, signal);
+ if (xp_proc_kill(xp, auser, akey, signal) < 0) {
+ sp_rerror(&ename, &ecode);
+ fprintf(stderr, "xp_proc_kill: %s.\n", ename);
+ }
free(tmpjid);
}
}
} else {
if(xp->pid == pid)
- xp_proc_kill(xp, auser, akey, signal);
+ if (xp_proc_kill(xp, auser, akey, signal) < 0) {
+ sp_rerror(&ename, &ecode);
+ fprintf(stderr, "xp_proc_kill: %s.\n", ename);
+ }
}
}
}
@@ -178,7 +187,7 @@
error:
sp_rerror(&ename, &ecode);
- fprintf(stderr, "Error: %s: %d\n", ename, ecode);
+ fprintf(stderr, "Error %d: %s.\n", ecode, ename);
return 1;
}