1
0
mirror of https://github.com/systemd/systemd.git synced 2025-02-09 13:57:42 +03:00

use CLOEXEC flags instead of fcntl()

This commit is contained in:
Kay Sievers 2009-10-30 12:31:59 +01:00
parent 82c6558e01
commit 26347a4c55
7 changed files with 7 additions and 25 deletions

View File

@ -30,7 +30,7 @@ CLEANFILES =
# libudev # libudev
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
LIBUDEV_CURRENT=5 LIBUDEV_CURRENT=5
LIBUDEV_REVISION=0 LIBUDEV_REVISION=1
LIBUDEV_AGE=5 LIBUDEV_AGE=5
SUBDIRS += libudev/docs SUBDIRS += libudev/docs

View File

@ -83,6 +83,7 @@ udev_enumerate_add_nomatch_subsystem
udev_enumerate_add_match_sysattr udev_enumerate_add_match_sysattr
udev_enumerate_add_nomatch_sysattr udev_enumerate_add_nomatch_sysattr
udev_enumerate_add_match_property udev_enumerate_add_match_property
udev_enumerate_add_match_sysname
udev_enumerate_add_syspath udev_enumerate_add_syspath
udev_enumerate_scan_devices udev_enumerate_scan_devices
udev_enumerate_scan_subsystems udev_enumerate_scan_subsystems

View File

@ -139,13 +139,12 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path); util_strscpy(&udev_monitor->sun.sun_path[1], sizeof(udev_monitor->sun.sun_path)-1, socket_path);
udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1; udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(socket_path)+1;
} }
udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0); udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0);
if (udev_monitor->sock == -1) { if (udev_monitor->sock == -1) {
err(udev, "error getting socket: %m\n"); err(udev, "error getting socket: %m\n");
free(udev_monitor); free(udev_monitor);
return NULL; return NULL;
} }
util_set_fd_cloexec(udev_monitor->sock);
dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path); dbg(udev, "monitor %p created with '%s'\n", udev_monitor, socket_path);
return udev_monitor; return udev_monitor;
@ -197,13 +196,12 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char
if (udev_monitor == NULL) if (udev_monitor == NULL)
return NULL; return NULL;
udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); udev_monitor->sock = socket(PF_NETLINK, SOCK_DGRAM|SOCK_CLOEXEC, NETLINK_KOBJECT_UEVENT);
if (udev_monitor->sock == -1) { if (udev_monitor->sock == -1) {
err(udev, "error getting socket: %m\n"); err(udev, "error getting socket: %m\n");
free(udev_monitor); free(udev_monitor);
return NULL; return NULL;
} }
util_set_fd_cloexec(udev_monitor->sock);
udev_monitor->snl.nl_family = AF_NETLINK; udev_monitor->snl.nl_family = AF_NETLINK;
udev_monitor->snl.nl_groups = group; udev_monitor->snl.nl_groups = group;

View File

@ -205,7 +205,6 @@ size_t util_strscpyl(char *dest, size_t size, const char *src, ...) __attribute_
int udev_util_replace_whitespace(const char *str, char *to, size_t len); int udev_util_replace_whitespace(const char *str, char *to, size_t len);
int udev_util_replace_chars(char *str, const char *white); int udev_util_replace_chars(char *str, const char *white);
int udev_util_encode_string(const char *str, char *str_enc, size_t len); int udev_util_encode_string(const char *str, char *str_enc, size_t len);
void util_set_fd_cloexec(int fd);
unsigned int util_string_hash32(const char *str); unsigned int util_string_hash32(const char *str);
/* libudev-util-private.c */ /* libudev-util-private.c */

View File

@ -481,18 +481,6 @@ err:
return -1; return -1;
} }
void util_set_fd_cloexec(int fd)
{
int flags;
flags = fcntl(fd, F_GETFD);
if (flags < 0)
flags = FD_CLOEXEC;
else
flags |= FD_CLOEXEC;
fcntl(fd, F_SETFD, flags);
}
unsigned int util_string_hash32(const char *str) unsigned int util_string_hash32(const char *str)
{ {
unsigned int hash = 0; unsigned int hash = 0;

View File

@ -38,10 +38,8 @@ static int inotify_fd = -1;
*/ */
int udev_watch_init(struct udev *udev) int udev_watch_init(struct udev *udev)
{ {
inotify_fd = inotify_init(); inotify_fd = inotify_init1(IN_CLOEXEC);
if (inotify_fd >= 0) if (inotify_fd < 0)
util_set_fd_cloexec(inotify_fd);
else
err(udev, "inotify_init failed: %m\n"); err(udev, "inotify_init failed: %m\n");
return inotify_fd; return inotify_fd;
} }

View File

@ -223,7 +223,6 @@ static void worker_new(struct event *event)
/* allow the main daemon netlink address to send devices to the worker */ /* allow the main daemon netlink address to send devices to the worker */
udev_monitor_allow_unicast_sender(worker_monitor, monitor); udev_monitor_allow_unicast_sender(worker_monitor, monitor);
udev_monitor_enable_receiving(worker_monitor); udev_monitor_enable_receiving(worker_monitor);
util_set_fd_cloexec(udev_monitor_get_fd(worker_monitor));
worker = calloc(1, sizeof(struct worker)); worker = calloc(1, sizeof(struct worker));
if (worker == NULL) if (worker == NULL)
@ -945,14 +944,13 @@ int main(int argc, char *argv[])
} }
/* unnamed socket from workers to the main daemon */ /* unnamed socket from workers to the main daemon */
if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, worker_watch) < 0) { if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
fprintf(stderr, "error getting socketpair\n"); fprintf(stderr, "error getting socketpair\n");
err(udev, "error getting socketpair\n"); err(udev, "error getting socketpair\n");
rc = 6; rc = 6;
goto exit; goto exit;
} }
pfd[FD_WORKER].fd = worker_watch[READ_END]; pfd[FD_WORKER].fd = worker_watch[READ_END];
util_set_fd_cloexec(worker_watch[WRITE_END]);
rules = udev_rules_new(udev, resolve_names); rules = udev_rules_new(udev, resolve_names);
if (rules == NULL) { if (rules == NULL) {