📄 spawni.c
字号:
__mach_port_deallocate (__mach_task_self (), dtable[fd]); else { _hurd_port_free (dtable_cells[fd], &ulink_dtable[fd], dtable[fd]); } dtable_cells[fd] = NULL; dtable[fd] = MACH_PORT_NULL; return 0; } return EBADF; } /* Make sure the dtable can hold NEWFD. */#define EXPAND_DTABLE(newfd) \ ({ \ if ((unsigned int)newfd >= dtablesize \ && newfd < _hurd_rlimits[RLIMIT_OFILE].rlim_cur) \ { \ /* We need to expand the dtable for the child. */ \ NEW_TABLE (dtable, newfd); \ NEW_TABLE (ulink_dtable, newfd); \ NEW_TABLE (dtable_cells, newfd); \ } \ ((unsigned int)newfd < dtablesize ? 0 : EMFILE); \ })#define NEW_TABLE(x, newfd) \ do { __typeof (x) new_##x = __alloca ((newfd + 1) * sizeof (x[0])); \ memcpy (new_##x, x, dtablesize * sizeof (x[0])); \ memset (&new_##x[dtablesize], 0, (newfd + 1 - dtablesize) * sizeof (x[0])); \ x = new_##x; } while (0) struct __spawn_action *action = &file_actions->__actions[i]; switch (action->tag) { case spawn_do_close: err = do_close (action->action.close_action.fd); break; case spawn_do_dup2: if ((unsigned int)action->action.dup2_action.fd < dtablesize && dtable[action->action.dup2_action.fd] != MACH_PORT_NULL) { const int fd = action->action.dup2_action.fd; const int newfd = action->action.dup2_action.newfd; // dup2 always clears any old FD_CLOEXEC flag on the new fd. if (newfd < orig_dtablesize) dtable_cloexec[newfd] = 0; if (fd == newfd) // Same is same as same was. break; err = EXPAND_DTABLE (newfd); if (!err) { /* Close the old NEWFD and replace it with FD's contents, which can be either an original descriptor (DTABLE_CELLS[FD] != 0) or a new right that we acquired in this function. */ do_close (newfd); dtable_cells[newfd] = dtable_cells[fd]; if (dtable_cells[newfd] != NULL) dtable[newfd] = _hurd_port_get (dtable_cells[newfd], &ulink_dtable[newfd]); else { dtable[newfd] = dtable[fd]; err = __mach_port_mod_refs (__mach_task_self (), dtable[fd], MACH_PORT_RIGHT_SEND, +1); } } } else // The old FD specified was bogus. err = EBADF; break; case spawn_do_open: /* Open a file on behalf of the child. XXX note that this can subject the parent to arbitrary delays waiting for the files to open. I don't know what the spec says about this. If it's not permissible, then this whole forkless implementation is probably untenable. */ { const int fd = action->action.open_action.fd; do_close (fd); if (fd < orig_dtablesize) dtable_cloexec[fd] = 0; err = EXPAND_DTABLE (fd); if (err) break; err = child_lookup (action->action.open_action.path, action->action.open_action.oflag, action->action.open_action.mode, &dtable[fd]); dtable_cells[fd] = NULL; break; } } if (err) goto out; } /* Only now can we perform FD_CLOEXEC. We had to leave the descriptors unmolested for the file actions to use. Note that the DTABLE_CLOEXEC array is never expanded by file actions, so it might now have fewer than DTABLESIZE elements. */ for (i = 0; i < orig_dtablesize; ++i) if (dtable[i] != MACH_PORT_NULL && dtable_cloexec[i]) { assert (dtable_cells[i] != NULL); _hurd_port_free (dtable_cells[i], &ulink_dtable[i], dtable[i]); dtable[i] = MACH_PORT_NULL; } /* Prune trailing null ports from the descriptor table. */ while (dtablesize > 0 && dtable[dtablesize - 1] == MACH_PORT_NULL) --dtablesize; if (flags & POSIX_SPAWN_RESETIDS) { /* Reauthenticate all the child's ports with its new auth handle. */ mach_port_t ref; process_t newproc; /* Reauthenticate with the proc server. */ ref = __mach_reply_port (); err = __proc_reauthenticate (proc, ref, MACH_MSG_TYPE_MAKE_SEND); if (!err) err = __auth_user_authenticate (auth, ref, MACH_MSG_TYPE_MAKE_SEND, &newproc); __mach_port_destroy (__mach_task_self (), ref); if (!err) { __mach_port_deallocate (__mach_task_self (), proc); proc = newproc; } if (!err) err = reauthenticate (INIT_PORT_CRDIR, &rcrdir); if (!err) err = reauthenticate (INIT_PORT_CWDIR, &rcwdir); /* We must reauthenticate all the fds except those that came from `spawn_do_open' file actions, which were opened using the child's auth port to begin with. */ for (i = 0; !err && i < dtablesize; ++i) err = reauthenticate_fd (i); } if (err) goto out; /* Now we are ready to open the executable file using the child's ports. We do this after performing all the file actions so the order of events is the same as for a fork, exec sequence. This affects things like the meaning of a /dev/fd file name, as well as which error conditions are diagnosed first and what side effects (file creation, etc) can be observed before what errors. */ if (! use_path || strchr (file, '/') != NULL) /* The FILE parameter is actually a path. */ err = child_lookup (file, O_EXEC, 0, &execfile); else { /* We have to search for FILE on the path. */ path = getenv ("PATH"); if (path == NULL) { /* There is no `PATH' in the environment. The default search path is the current directory followed by the path `confstr' returns for `_CS_PATH'. */ len = confstr (_CS_PATH, (char *) NULL, 0); path = (char *) __alloca (1 + len); path[0] = ':'; (void) confstr (_CS_PATH, path + 1, len); } len = strlen (file) + 1; pathlen = strlen (path); name = __alloca (pathlen + len + 1); /* Copy the file name at the top. */ name = (char *) memcpy (name + pathlen + 1, file, len); /* And add the slash. */ *--name = '/'; p = path; do { char *startp; path = p; p = __strchrnul (path, ':'); if (p == path) /* Two adjacent colons, or a colon at the beginning or the end of `PATH' means to search the current directory. */ startp = name + 1; else startp = (char *) memcpy (name - (p - path), path, p - path); /* Try to open this file name. */ err = child_lookup (startp, O_EXEC, 0, &execfile); switch (err) { case EACCES: case ENOENT: case ESTALE: case ENOTDIR: /* Those errors indicate the file is missing or not executablev by us, in which case we want to just try the next path directory. */ continue; case 0: /* Success! */ default: /* Some other error means we found an executable file, but something went wrong executing it; return the error to our caller. */ break; } // We only get here when we are done looking for the file. break; } while (*p++ != '\0'); } if (err) goto out; /* Almost there! */ { mach_port_t ports[_hurd_nports]; struct hurd_userlink ulink_ports[_hurd_nports]; char *args = NULL, *env = NULL; size_t argslen = 0, envlen = 0; inline error_t exec (file_t file) { return __file_exec (file, task, (__sigismember (&_hurdsig_traced, SIGKILL) ? EXEC_SIGTRAP : 0), args, argslen, env, envlen, dtable, MACH_MSG_TYPE_COPY_SEND, dtablesize, ports, MACH_MSG_TYPE_COPY_SEND, _hurd_nports, ints, INIT_INT_MAX, NULL, 0, NULL, 0); } /* Now we are out of things that can fail before the file_exec RPC, for which everything else must be prepared. The only thing left to do is packing up the argument and environment strings, and the array of init ports. */ if (argv != NULL) err = __argz_create (argv, &args, &argslen); if (!err && envp != NULL) err = __argz_create (envp, &env, &envlen); /* Load up the ports to give to the new program. Note the loop/switch below must parallel exactly to release refs. */ for (i = 0; i < _hurd_nports; ++i) { switch (i) { case INIT_PORT_AUTH: ports[i] = auth; continue; case INIT_PORT_PROC: ports[i] = proc; continue; case INIT_PORT_CRDIR: if (flags & POSIX_SPAWN_RESETIDS) { ports[i] = rcrdir; continue; } break; case INIT_PORT_CWDIR: if (flags & POSIX_SPAWN_RESETIDS) { ports[i] = rcwdir; continue; } break; } ports[i] = _hurd_port_get (&_hurd_ports[i], &ulink_ports[i]); } /* Finally, try executing the file we opened. */ if (!err) err = exec (execfile); __mach_port_deallocate (__mach_task_self (), execfile); if (err == ENOEXEC) { /* The file is accessible but it is not an executable file. Invoke the shell to interpret it as a script. */ err = __argz_insert (&args, &argslen, args, _PATH_BSHELL); if (!err) err = child_lookup (_PATH_BSHELL, O_EXEC, 0, &execfile); if (!err) { err = exec (execfile); __mach_port_deallocate (__mach_task_self (), execfile); } } /* Release the references just packed up in PORTS. This switch must always parallel the one above that fills PORTS. */ for (i = 0; i < _hurd_nports; ++i) { switch (i) { case INIT_PORT_AUTH: case INIT_PORT_PROC: continue; case INIT_PORT_CRDIR: if (flags & POSIX_SPAWN_RESETIDS) continue; break; case INIT_PORT_CWDIR: if (flags & POSIX_SPAWN_RESETIDS) continue; break; } _hurd_port_free (&_hurd_ports[i], &ulink_ports[i], ports[i]); } free (args); free (env); } /* We did it! We have a child! */ if (pid != NULL) *pid = new_pid; out: /* Clean up all the references we are now holding. */ if (task != MACH_PORT_NULL) { if (err) /* We failed after creating the task, so kill it. */ __task_terminate (task); __mach_port_deallocate (__mach_task_self (), task); } __mach_port_deallocate (__mach_task_self (), auth); __mach_port_deallocate (__mach_task_self (), proc); if (rcrdir != MACH_PORT_NULL) __mach_port_deallocate (__mach_task_self (), rcrdir); if (rcwdir != MACH_PORT_NULL) __mach_port_deallocate (__mach_task_self (), rcwdir); if (ulink_dtable) /* Release references to the file descriptor ports. */ for (i = 0; i < dtablesize; ++i) if (dtable[i] != MACH_PORT_NULL) { if (dtable_cells[i] == NULL) __mach_port_deallocate (__mach_task_self (), dtable[i]); else _hurd_port_free (dtable_cells[i], &ulink_dtable[i], dtable[i]); } if (err) /* This hack canonicalizes the error code that we return. */ err = (__hurd_fail (err), errno); return err;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -