Debian Codegraph @

src/chroot.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2013, 2015 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #define _GNU_SOURCE
24 #include <errno.h>
25 #include <stddef.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include "setenv.h"
30 #include "libfakechroot.h"
31
32 #include "strlcpy.h"
33 #include "dedotdot.h"
34
35 #ifdef HAVE___XSTAT64
36 # include "__xstat64.h"
37 # define STAT_T stat64
38 # define STAT(path, sb) nextcall(__xstat64)(_STAT_VER, path, sb)
39 #else
40 # include "stat.h"
41 # define STAT_T stat
42 # define STAT(path, sb) nextcall(stat)(path, sb)
43 #endif
44
45 #include "getcwd_real.h"
46
47 wrapper(chroot, int, (const char * path))
48 {
49 char fakechroot_abspath[FAKECHROOT_PATH_MAX];
50 char fakechroot_buf[FAKECHROOT_PATH_MAX];
51
52 char *ld_library_path, *separator, *new_ld_library_path;
53 int status;
54 size_t len;
55 char cwd[FAKECHROOT_PATH_MAX - 1];
56 char tmp[FAKECHROOT_PATH_MAX], *tmpptr = tmp;
57 struct STAT_T sb;
58
59 const char *fakechroot_base = getenv("FAKECHROOT_BASE");
60
61 debug("chroot(\"%s\")", path);
62
63 if (!path) {
64 __set_errno(EFAULT);
65 return -1;
66 }
67
68 if (!*path) {
69 __set_errno(ENOENT);
70 return -1;
71 }
72
73 if (getcwd_real(cwd, FAKECHROOT_PATH_MAX - 1) == NULL) {
74 __set_errno(EIO);
75 return -1;
76 }
77
78 if (fakechroot_base != NULL && strstr(cwd, fakechroot_base) == cwd) {
79 expand_chroot_path(path);
80 strlcpy(tmp, path, FAKECHROOT_PATH_MAX);
81 dedotdot(tmpptr);
82 path = tmpptr;
83 }
84 else {
85 size_t tmplen;
86 if (*path == '/') {
87 expand_chroot_rel_path(path);
88 strlcpy(tmp, path, FAKECHROOT_PATH_MAX);
89 dedotdot(tmpptr);
90 path = tmpptr;
91 }
92 else {
93 snprintf(tmp, FAKECHROOT_PATH_MAX, "%s/%s", cwd, path);
94 dedotdot(tmpptr);
95 path = tmpptr;
96 }
97 tmplen = strlen(tmpptr);
98 while(tmplen > 1 && tmpptr[tmplen - 1] == '/') {
99 tmpptr[--tmplen] = '\0';
100 }
101 }
102
103 /* Suppress a trailing slash */
104 if ((strlen(tmpptr) > 1) && path[strlen(tmpptr) - 1] == '/') {
105 tmpptr[strlen(tmpptr) - 1] = '\0';
106 }
107
108 if ((status = STAT(path, &sb)) != 0) {
109 return status;
110 }
111
112 if ((sb.st_mode & S_IFMT) != S_IFDIR) {
113 __set_errno(ENOTDIR);
114 return -1;
115 }
116
117 if (setenv("FAKECHROOT_BASE", path, 1) == -1) {
118 return -1;
119 }
120
121 ld_library_path = getenv("LD_LIBRARY_PATH");
122
123 if (ld_library_path != NULL && strlen(ld_library_path) > 0) {
124 separator = ":";
125 }
126 else {
127 ld_library_path = "";
128 separator = "";
129 }
130
131 len = strlen(ld_library_path)+strlen(separator)+strlen(path)*2+sizeof("/usr/lib:/lib");
132
133 if ((new_ld_library_path = malloc(len)) == NULL) {
134 __set_errno(ENOMEM);
135 return -1;
136 }
137
138 snprintf(new_ld_library_path, len, "%s%s%s/usr/lib:%s/lib", ld_library_path, separator, path, path);
139 setenv("LD_LIBRARY_PATH", new_ld_library_path, 1);
140 free(new_ld_library_path);
141
142 return 0;
143 }