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