]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/canonicalize.c
2 * canonicalize.c -- canonicalize pathname by removing symlinks
4 * This file may be distributed under the terms of the
5 * GNU Lesser General Public License.
7 * Copyright (C) 2009-2013 Karel Zak <kzak@redhat.com>
15 #include <sys/types.h>
19 #include "canonicalize.h"
20 #include "pathnames.h"
24 * Converts private "dm-N" names to "/dev/mapper/<name>"
26 * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs
27 * provides the real DM device names in /sys/block/<ptname>/dm/name
29 char *__canonicalize_dm_name(const char *prefix
, const char *ptname
)
33 char path
[256], name
[sizeof(path
) - sizeof(_PATH_DEV_MAPPER
)], *res
= NULL
;
35 if (!ptname
|| !*ptname
)
41 snprintf(path
, sizeof(path
), "%s/sys/block/%s/dm/name", prefix
, ptname
);
42 if (!(f
= fopen(path
, "r" UL_CLOEXECSTR
)))
45 /* read "<name>\n" from sysfs */
46 if (fgets(name
, sizeof(name
), f
) && (sz
= strlen(name
)) > 1) {
48 snprintf(path
, sizeof(path
), _PATH_DEV_MAPPER
"/%s", name
);
50 if (prefix
|| access(path
, F_OK
) == 0)
57 char *canonicalize_dm_name(const char *ptname
)
59 return __canonicalize_dm_name(NULL
, ptname
);
62 static int is_dm_devname(char *canonical
, char **name
)
65 char *p
= strrchr(canonical
, '/');
70 || strncmp(p
, "/dm-", 4) != 0
72 || stat(canonical
, &sb
) != 0
73 || !S_ISBLK(sb
.st_mode
))
81 * This function does not canonicalize the path! It just prepends CWD before a
82 * relative path. If the path is no relative than returns NULL. The path does
85 char *absolute_path(const char *path
)
87 char cwd
[PATH_MAX
], *res
, *p
;
90 if (!is_relative_path(path
)) {
94 if (!getcwd(cwd
, sizeof(cwd
)))
98 if (startswith(path
, "./"))
100 else if (strcmp(path
, ".") == 0)
109 p
= res
= malloc(csz
+ 1 + psz
+ 1);
116 memcpy(p
, path
, psz
+ 1);
121 char *canonicalize_path(const char *path
)
123 char *canonical
, *dmname
;
128 canonical
= realpath(path
, NULL
);
132 if (is_dm_devname(canonical
, &dmname
)) {
133 char *dm
= canonicalize_dm_name(dmname
);
143 char *canonicalize_path_restricted(const char *path
)
145 char *canonical
= NULL
;
154 if (pipe(pipes
) != 0)
158 * To accurately assume identity of getuid() we must use setuid()
159 * but if we do that, we lose ability to reassume euid of 0, so
160 * we fork to do the check to keep euid intact.
167 return NULL
; /* fork error */
169 close(pipes
[0]); /* close unused end */
173 /* drop permissions */
174 if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
175 canonical
= NULL
; /* failed */
179 canonical
= realpath(path
, NULL
);
180 if (canonical
&& is_dm_devname(canonical
, &dmname
)) {
181 char *dm
= canonicalize_dm_name(dmname
);
189 len
= canonical
? strlen(canonical
) : errno
? -errno
: -EINVAL
;
191 /* send length or errno */
192 write_all(pipes
[1], (char *) &len
, sizeof(len
));
194 write_all(pipes
[1], canonical
, len
);
200 close(pipes
[1]); /* close unused end */
203 /* read size or -errno */
204 if (read_all(pipes
[0], (char *) &len
, sizeof(len
)) != sizeof(len
))
211 canonical
= malloc(len
+ 1);
217 if (read_all(pipes
[0], canonical
, len
) != len
) {
221 canonical
[len
] = '\0';
229 /* We make a best effort to reap child */
230 waitpid(pid
, NULL
, 0);
237 #ifdef TEST_PROGRAM_CANONICALIZE
238 int main(int argc
, char **argv
)
241 fprintf(stderr
, "usage: %s <device>\n", argv
[0]);
245 fprintf(stdout
, "orig: %s\n", argv
[1]);
246 fprintf(stdout
, "real: %s\n", canonicalize_path(argv
[1]));