-/* Example: sandbox_root = /vservers/bnc, relpath = /proc/1 */
-static int sandbox_file_exists(char *sandbox_root, char *relpath)
-{
- struct stat stat_buf;
- char *file;
- int len, exists = 0;
-
- len = strlen(sandbox_root) + strlen(relpath) + NULLBYTE_SIZE;
- if ((file = (char *)malloc(len)) == NULL) {
- PERROR("malloc(%d)", len);
- exit(1);
- }
- sprintf(file, "%s%s", sandbox_root, relpath);
- file[len - 1] = '\0';
- if (stat(file, &stat_buf) == 0) {
- exists = 1;
- }
-
-
- free(file);
- return exists;
-}
-
-static int proc_mounted(char *sandbox_root)
-{
- return sandbox_file_exists(sandbox_root, "/proc/1");
-}
-
-static int devpts_mounted(char *sandbox_root)
-{
- return sandbox_file_exists(sandbox_root, "/dev/pts/0");
-}
-
-static void mount_proc(char *sandbox_root)
-{
- char *source = "/proc";
- char *target;
- int len;
-
- len = strlen(sandbox_root) + strlen("/") + strlen("proc") + NULLBYTE_SIZE;
- if ((target = (char *)malloc(len)) == NULL) {
- PERROR("malloc(%d)", len);
- exit(1);
- }
-
- sprintf(target, "%s/proc", sandbox_root);
- target[len - 1] = '\0';
- if (!proc_mounted(sandbox_root))
- mount(source, target, "proc", MS_BIND | MS_RDONLY, NULL);
-
- free(target);
-}
-
-static void mount_devpts(char *sandbox_root)
-{
- char *source = "/dev/pts";
- char *target;
- int len;
-
- len = strlen(sandbox_root) + strlen("/") + strlen("dev/pts") + NULLBYTE_SIZE;
- if ((target = (char *)malloc(len)) == NULL) {
- PERROR("malloc(%d)", len);
- exit(1);
- }
-
- sprintf(target, "%s/dev/pts", sandbox_root);
- target[len - 1] = '\0';
- if (!devpts_mounted(sandbox_root))
- mount(source, target, "devpts", 0, NULL);
-
- free(target);
-}
-
-static int sandbox_chroot(uid_t uid)