xref: /titanic_50/usr/src/cmd/ptools/pstop/pstop.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1994-1999 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <sys/types.h>
36 #include <signal.h>
37 #include <libproc.h>
38 
39 static	int	stop(char *);
40 static	int	perr(char *);
41 
42 static	char	*command;
43 static	char	*procname;
44 
45 int
46 main(int argc, char **argv)
47 {
48 	int rc = 0;
49 
50 	if ((command = strrchr(argv[0], '/')) != NULL)
51 		command++;
52 	else
53 		command = argv[0];
54 
55 	if (argc <= 1) {
56 		(void) fprintf(stderr, "usage:\t%s pid ...\n", command);
57 		(void) fprintf(stderr,
58 			"  (stop processes with /proc request)\n");
59 		return (2);
60 	}
61 
62 	while (--argc > 0)
63 		rc += stop(*++argv);
64 
65 	return (rc);
66 }
67 
68 static int
69 stop(char *arg)
70 {
71 	char ctlfile[100];
72 	long ctl[1];
73 	int ctlfd, gcode;
74 	pid_t pid;
75 
76 	procname = arg;		/* for perr() */
77 	if ((pid = proc_arg_psinfo(arg, PR_ARG_PIDS, NULL, &gcode)) == -1) {
78 		(void) fprintf(stderr, "%s: cannot control %s: %s\n",
79 			command, arg, Pgrab_error(gcode));
80 		return (1);
81 	}
82 
83 	(void) sprintf(ctlfile, "/proc/%d/ctl", (int)pid);
84 	errno = 0;
85 	if ((ctlfd = open(ctlfile, O_WRONLY)) >= 0) {
86 		ctl[0] = PCDSTOP;
87 		(void) write(ctlfd, ctl, sizeof (long));
88 		(void) close(ctlfd);
89 	}
90 
91 	return (perr(NULL));
92 }
93 
94 static int
95 perr(char *s)
96 {
97 	if (errno == 0)
98 		return (0);
99 	if (s)
100 		(void) fprintf(stderr, "%s: ", procname);
101 	else
102 		s = procname;
103 	perror(s);
104 	return (1);
105 }
106