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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #pragma init(init)
27 
28 #include <s10_brand.h>
29 #include <stdlib.h>
30 #include <sys/syscall.h>
31 #include <dlfcn.h>
32 #include <link.h>
33 #include <limits.h>
34 #include <sys/mman.h>
35 #include <strings.h>
36 
37 /* MAXCOMLEN is only defined in user.h in the kernel. */
38 #define	MAXCOMLEN	16
39 
40 /*
41  * This is a library that is LD_PRELOADed into native processes.
42  * Its primary function is to perform one brand operation, B_S10_NATIVE,
43  * which checks that this is actually a native process.  If it is, then
44  * the operation changes the executable name so that it is no longer
45  * ld.so.1.  Instead it changes it to be the name of the real native
46  * executable that we're runnning.  This allows things like pgrep to work
47  * as expected.  Note that this brand operation only changes the process
48  * name wrt the kernel.  From the process' perspective, AT_SUN_EXECNAME is
49  * still ld.so.1. ld.so.1 removes itself and its arguments from the argv list.
50  */
51 void
52 init(void)
53 {
54 	int i;
55 	Dl_argsinfo_t argsinfo;
56 	sysret_t rval;
57 	char	*pcomm;
58 	char	cmd_buf[MAXCOMLEN + 1];
59 	char	arg_buf[PSARGSZ];
60 
61 	if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &argsinfo) == -1)
62 		return;
63 
64 	/* get the base cmd name */
65 	if ((pcomm = strrchr(argsinfo.dla_argv[0], '/')) != NULL)
66 		pcomm = pcomm + 1;
67 	else
68 		pcomm = argsinfo.dla_argv[0];
69 	(void) strlcpy(cmd_buf, pcomm, sizeof (cmd_buf));
70 
71 	(void) strlcpy(arg_buf, argsinfo.dla_argv[0], sizeof (arg_buf));
72 
73 	for (i = 1; i < argsinfo.dla_argc; i++) {
74 		(void) strlcat(arg_buf, " ", sizeof (arg_buf));
75 		if (strlcat(arg_buf, argsinfo.dla_argv[i], sizeof (arg_buf))
76 		    >= sizeof (arg_buf))
77 			break;
78 	}
79 
80 	(void) __systemcall(&rval, SYS_brand, B_S10_NATIVE, cmd_buf, arg_buf);
81 }
82