xref: /titanic_41/usr/src/lib/libast/common/preroot/setpreroot.c (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * AT&T Bell Laboratories
25  * force current command to run under dir preroot
26  */
27 
28 #include <ast.h>
29 #include <preroot.h>
30 
31 #if FS_PREROOT
32 
33 #include <option.h>
34 
35 void
36 setpreroot(register char** argv, const char* dir)
37 {
38 	register char*	s;
39 	register char**	ap;
40 	int		argc;
41 	char*		cmd;
42 	char**		av;
43 	char		buf[PATH_MAX];
44 
45 	if ((argv || (argv = opt_info.argv)) && (dir || (dir = getenv(PR_BASE)) && *dir) && !ispreroot(dir) && (*(cmd = *argv++) == '/' || (cmd = pathpath(buf, cmd, NiL, PATH_ABSOLUTE|PATH_REGULAR|PATH_EXECUTE))))
46 	{
47 		argc = 3;
48 		for (ap = argv; *ap++; argc++);
49 		if (av = newof(0, char*, argc, 0))
50 		{
51 			ap = av;
52 			*ap++ = PR_COMMAND;
53 			*ap++ = dir;
54 			*ap++ = cmd;
55 			while (*ap++ = *argv++);
56 			if (!(s = getenv(PR_SILENT)) || !*s)
57 			{
58 				sfprintf(sfstderr, "+");
59 				ap = av;
60 				while (s = *ap++)
61 					sfprintf(sfstderr, " %s", s);
62 				sfprintf(sfstderr, "\n");
63 				sfsync(sfstderr);
64 			}
65 			execv(*av, av);
66 			free(av);
67 		}
68 	}
69 }
70 
71 #else
72 
73 NoN(setpreroot)
74 
75 #endif
76