#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <libproc.h>
static int stop(char *);
static int lwpstop(int *, const lwpstatus_t *, const lwpsinfo_t *);
static char *command;
static const char *lwps;
static struct ps_prochandle *P;
int
main(int argc, char **argv)
{
int rc = 0;
if ((command = strrchr(argv[0], '/')) != NULL)
command++;
else
command = argv[0];
if (argc <= 1) {
(void) fprintf(stderr, "usage:\t%s pid[/lwps] ...\n", command);
(void) fprintf(stderr,
" (stop processes or lwps with /proc request)\n");
return (2);
}
while (--argc > 0)
rc += stop(*++argv);
return (rc);
}
static int
stop(char *arg)
{
int gcode;
int rc = 0;
if ((P = proc_arg_xgrab(arg, NULL, PR_ARG_PIDS, PGRAB_RETAIN |
PGRAB_NOSTOP | PGRAB_FORCE, &gcode, &lwps)) == NULL) {
(void) fprintf(stderr, "%s: cannot control %s: %s\n",
command, arg, Pgrab_error(gcode));
return (1);
} else if (lwps != NULL) {
int lwpcount = 0;
(void) Plwp_iter_all(P, (proc_lwp_all_f *)lwpstop, &lwpcount);
if (lwpcount == 0) {
(void) fprintf(stderr, "%s: cannot control %s:"
" no matching LWPs found\n", command, arg);
rc = 1;
} else if (lwpcount == -1)
rc = 1;
} else {
(void) Pdstop(P);
}
(void) Punsetflags(P, PR_RLC);
Pfree(P);
return (rc);
}
static int
lwpstop(int *lwpcount, const lwpstatus_t *status, const lwpsinfo_t *info)
{
struct ps_lwphandle *L;
int gcode;
if (proc_lwp_in_set(lwps, info->pr_lwpid)) {
if ((L = Lgrab(P, info->pr_lwpid, &gcode)) != NULL) {
(void) Ldstop(L);
Lfree(L);
if (*lwpcount >= 0)
(*lwpcount)++;
} else if (gcode != G_NOPROC) {
(void) fprintf(stderr, "%s: cannot control %d/%d: %s\n",
command, (int)Pstatus(P)->pr_pid,
(int)info->pr_lwpid, Lgrab_error(gcode));
*lwpcount = -1;
}
}
return (0);
}