xref: /titanic_50/usr/src/cmd/rexd/where_main.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * Copyright 1992 Sun Microsystems, Inc.  All rights reserved.
23*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include <stdio.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate static char **Argv;		/* saved argument vector (for ps) */
35*7c478bd9Sstevel@tonic-gate static char *LastArgv;		/* saved end-of-argument vector */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate int child = 0;			/* pid of the executed process */
38*7c478bd9Sstevel@tonic-gate int ChildDied = 0;		/* true when above is valid */
39*7c478bd9Sstevel@tonic-gate int HasHelper = 0;		/* must kill helpers (interactive mode) */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate int	Debug = 0;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate void
main(argc,argv)46*7c478bd9Sstevel@tonic-gate main(argc, argv)
47*7c478bd9Sstevel@tonic-gate 	char **argv;
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	char host[MAXPATHLEN];
50*7c478bd9Sstevel@tonic-gate 	char fsname[MAXPATHLEN];
51*7c478bd9Sstevel@tonic-gate 	char within[MAXPATHLEN];
52*7c478bd9Sstevel@tonic-gate 	char *pn;
53*7c478bd9Sstevel@tonic-gate 	int many;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	/*
56*7c478bd9Sstevel@tonic-gate 	 * argv start and extent for setproctitle()
57*7c478bd9Sstevel@tonic-gate 	 */
58*7c478bd9Sstevel@tonic-gate 	Argv = argv;
59*7c478bd9Sstevel@tonic-gate 	if (argc > 0)
60*7c478bd9Sstevel@tonic-gate 		LastArgv = argv[argc-1] + strlen(argv[argc-1]);
61*7c478bd9Sstevel@tonic-gate 	else
62*7c478bd9Sstevel@tonic-gate 		LastArgv = NULL;
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	many = argc > 2;
65*7c478bd9Sstevel@tonic-gate 	while (--argc > 0) {
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 		if ( strcmp( "-d", *argv ) == 0 )
68*7c478bd9Sstevel@tonic-gate 		{
69*7c478bd9Sstevel@tonic-gate 			Debug = 1;
70*7c478bd9Sstevel@tonic-gate 			argv++;
71*7c478bd9Sstevel@tonic-gate 			continue;
72*7c478bd9Sstevel@tonic-gate 		}
73*7c478bd9Sstevel@tonic-gate 		pn = *++argv;
74*7c478bd9Sstevel@tonic-gate 		where(pn, host, fsname, within);
75*7c478bd9Sstevel@tonic-gate 		if (many)
76*7c478bd9Sstevel@tonic-gate 			printf("%s:\t", pn);
77*7c478bd9Sstevel@tonic-gate 		printf("%s:%s%s\n", host, fsname, within);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	exit(0);
80*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate  *  SETPROCTITLE -- set the title of this process for "ps"
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  *	Does nothing if there were not enough arguments on the command
87*7c478bd9Sstevel@tonic-gate  * 	line for the information.
88*7c478bd9Sstevel@tonic-gate  *
89*7c478bd9Sstevel@tonic-gate  *	Side Effects:
90*7c478bd9Sstevel@tonic-gate  *		Clobbers argv[] of our main procedure.
91*7c478bd9Sstevel@tonic-gate  */
92*7c478bd9Sstevel@tonic-gate void
setproctitle(user,host)93*7c478bd9Sstevel@tonic-gate setproctitle(user, host)
94*7c478bd9Sstevel@tonic-gate 	char *user, *host;
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	register char *tohere;
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	tohere = Argv[0];
99*7c478bd9Sstevel@tonic-gate 	if (LastArgv == NULL ||
100*7c478bd9Sstevel@tonic-gate 			strlen(user)+strlen(host)+3 > (LastArgv - tohere))
101*7c478bd9Sstevel@tonic-gate 		return;
102*7c478bd9Sstevel@tonic-gate 	*tohere++ = '-';		/* So ps prints (rpc.rexd)	*/
103*7c478bd9Sstevel@tonic-gate 	sprintf(tohere, "%s@%s", user, host);
104*7c478bd9Sstevel@tonic-gate 	while (*tohere++)		/* Skip to end of printf output	*/
105*7c478bd9Sstevel@tonic-gate 		;
106*7c478bd9Sstevel@tonic-gate 	while (tohere < LastArgv)	/* Avoid confusing ps		*/
107*7c478bd9Sstevel@tonic-gate 		*tohere++ = ' ';
108*7c478bd9Sstevel@tonic-gate }
109