mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 00:14:32 +09:00
Several fixes and cleanups around sd_listen_fds() (#36788)
This commit is contained in:
@@ -318,8 +318,7 @@ static int run(int argc, char *argv[]) {
|
||||
|
||||
n = sd_listen_fds(true);
|
||||
if (n < 0)
|
||||
return log_error_errno(errno,
|
||||
"Failed to read listening file descriptors from environment: %m");
|
||||
return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
|
||||
|
||||
if (n <= 0 || n > SERVER_FD_MAX)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
|
||||
@@ -3753,7 +3753,7 @@ _public_ int sd_varlink_server_add_connection_stdio(sd_varlink_server *s, sd_var
|
||||
|
||||
_public_ int sd_varlink_server_listen_name(sd_varlink_server *s, const char *name) {
|
||||
_cleanup_strv_free_ char **names = NULL;
|
||||
int r, n = 0;
|
||||
int r, m, n = 0;
|
||||
|
||||
assert_return(s, -EINVAL);
|
||||
assert_return(name, -EINVAL);
|
||||
@@ -3764,11 +3764,11 @@ _public_ int sd_varlink_server_listen_name(sd_varlink_server *s, const char *nam
|
||||
* See https://varlink.org/#activation for the environment variables this is backed by and the
|
||||
* recommended "varlink" identifier in $LISTEN_FDNAMES. */
|
||||
|
||||
r = sd_listen_fds_with_names(/* unset_environment= */ false, &names);
|
||||
if (r < 0)
|
||||
return r;
|
||||
m = sd_listen_fds_with_names(/* unset_environment= */ false, &names);
|
||||
if (m < 0)
|
||||
return m;
|
||||
|
||||
for (int i = 0; i < r; i++) {
|
||||
for (int i = 0; i < m; i++) {
|
||||
int b, fd;
|
||||
socklen_t l = sizeof(b);
|
||||
|
||||
|
||||
@@ -5970,7 +5970,7 @@ static int run(int argc, char *argv[]) {
|
||||
bool remove_directory = false, remove_image = false, veth_created = false;
|
||||
_cleanup_close_ int master = -EBADF, userns_fd = -EBADF, mount_fd = -EBADF;
|
||||
_cleanup_fdset_free_ FDSet *fds = NULL;
|
||||
int r, n_fd_passed, ret = EXIT_SUCCESS;
|
||||
int r, ret = EXIT_SUCCESS;
|
||||
char veth_name[IFNAMSIZ] = "";
|
||||
struct ExposeArgs expose_args = {};
|
||||
_cleanup_(release_lock_file) LockFile tree_global_lock = LOCK_FILE_INIT, tree_local_lock = LOCK_FILE_INIT;
|
||||
@@ -6057,13 +6057,10 @@ static int run(int argc, char *argv[]) {
|
||||
* so just turning this off here means we only turn it off in nspawn itself, not any children. */
|
||||
(void) ignore_signals(SIGPIPE);
|
||||
|
||||
n_fd_passed = sd_listen_fds(false);
|
||||
if (n_fd_passed > 0) {
|
||||
r = fdset_new_listen_fds(&fds, false);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to collect file descriptors: %m");
|
||||
goto finish;
|
||||
}
|
||||
r = fdset_new_listen_fds(&fds, /* unset = */ false);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to collect file descriptors: %m");
|
||||
goto finish;
|
||||
}
|
||||
|
||||
/* The "default" umask. This is appropriate for most file and directory
|
||||
|
||||
@@ -115,10 +115,12 @@ static int run(int argc, char *argv[]) {
|
||||
* requirements do not have a reliable means to check for in code. */
|
||||
|
||||
int n = sd_listen_fds(0);
|
||||
if (n < 0)
|
||||
return log_error_errno(n, "Failed to determine number of listening fds: %m");
|
||||
if (n > 1)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Received too many file descriptors");
|
||||
|
||||
int fd = n == 1 ? SD_LISTEN_FDS_START : -1;
|
||||
int fd = n == 1 ? SD_LISTEN_FDS_START : -EBADF;
|
||||
|
||||
/* SwapTotal is always available in /proc/meminfo and defaults to 0, even on swap-disabled kernels. */
|
||||
r = get_proc_field("/proc/meminfo", "SwapTotal", WHITESPACE, &swap);
|
||||
|
||||
@@ -254,25 +254,32 @@ int fdset_cloexec(FDSet *fds, bool b) {
|
||||
|
||||
int fdset_new_listen_fds(FDSet **ret, bool unset) {
|
||||
_cleanup_(fdset_shallow_freep) FDSet *s = NULL;
|
||||
int n, fd, r;
|
||||
int n, r;
|
||||
|
||||
assert(ret);
|
||||
|
||||
/* Creates an fdset and fills in all passed file descriptors */
|
||||
|
||||
n = sd_listen_fds(unset);
|
||||
if (n < 0)
|
||||
return n;
|
||||
if (n == 0) {
|
||||
*ret = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
s = fdset_new();
|
||||
if (!s)
|
||||
return -ENOMEM;
|
||||
|
||||
n = sd_listen_fds(unset);
|
||||
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
|
||||
for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
|
||||
r = fdset_put(s, fd);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
*ret = TAKE_PTR(s);
|
||||
return 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
int fdset_to_array(FDSet *fds, int **ret) {
|
||||
|
||||
@@ -2513,11 +2513,11 @@ static int run(int argc, char *argv[]) {
|
||||
glyph(GLYPH_LIGHT_SHADE), ansi_grey(), ansi_highlight(), ansi_grey(), ansi_normal());
|
||||
}
|
||||
|
||||
r = sd_listen_fds_with_names(true, &names);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to get passed file descriptors: %m");
|
||||
int n = sd_listen_fds_with_names(true, &names);
|
||||
if (n < 0)
|
||||
return log_error_errno(n, "Failed to get passed file descriptors: %m");
|
||||
|
||||
for (int i = 0; i < r; i++) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
int fd = SD_LISTEN_FDS_START + i;
|
||||
if (streq(names[i], "kvm"))
|
||||
kvm_device_fd = fd;
|
||||
|
||||
Reference in New Issue
Block a user