summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-01-06 00:26:25 +0100
committerLennart Poettering <lennart@poettering.net>2015-01-06 03:16:39 +0100
commita354329f724d6ce913d2ccffb2be8f3327a67faa (patch)
treeb6b05cc2c42c274f8385a16b1896d6c88bf4fc8f /src/shared
parent75399049653f2d5e22032da70cf96f20d7b4d9a6 (diff)
core: add new logic for services to store file descriptors in PID 1
With this change it is possible to send file descriptors to PID 1, via sd_pid_notify_with_fds() which PID 1 will store individually for each service, and pass via the usual fd passing logic on next invocation. This is useful for enable daemon reload schemes where daemons serialize their state to /run, push their fds into PID 1 and terminate, restoring their state on next start from the data in /run and passed in from PID 1. The fds are kept by PID 1 as long as no POLLHUP or POLLERR is seen on them, and the service they belong to are either not dead or failed, or have a job queued.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/fdset.c54
-rw-r--r--src/shared/fdset.h11
-rw-r--r--src/shared/util.c25
-rw-r--r--src/shared/util.h2
4 files changed, 88 insertions, 4 deletions
diff --git a/src/shared/fdset.c b/src/shared/fdset.c
index 46f7773a9e..9e35ce5cec 100644
--- a/src/shared/fdset.c
+++ b/src/shared/fdset.c
@@ -41,7 +41,31 @@ FDSet *fdset_new(void) {
return MAKE_FDSET(set_new(NULL));
}
-void fdset_free(FDSet *s) {
+int fdset_new_array(FDSet **ret, int *fds, unsigned n_fds) {
+ unsigned i;
+ FDSet *s;
+ int r;
+
+ assert(ret);
+
+ s = fdset_new();
+ if (!s)
+ return -ENOMEM;
+
+ for (i = 0; i < n_fds; i++) {
+
+ r = fdset_put(s, fds[i]);
+ if (r < 0) {
+ set_free(MAKE_SET(s));
+ return r;
+ }
+ }
+
+ *ret = s;
+ return 0;
+}
+
+FDSet* fdset_free(FDSet *s) {
void *p;
while ((p = set_steal_first(MAKE_SET(s)))) {
@@ -61,6 +85,7 @@ void fdset_free(FDSet *s) {
}
set_free(MAKE_SET(s));
+ return NULL;
}
int fdset_put(FDSet *s, int fd) {
@@ -70,6 +95,19 @@ int fdset_put(FDSet *s, int fd) {
return set_put(MAKE_SET(s), FD_TO_PTR(fd));
}
+int fdset_consume(FDSet *s, int fd) {
+ int r;
+
+ assert(s);
+ assert(fd >= 0);
+
+ r = fdset_put(s, fd);
+ if (r <= 0)
+ safe_close(fd);
+
+ return r;
+}
+
int fdset_put_dup(FDSet *s, int fd) {
int copy, r;
@@ -223,6 +261,10 @@ unsigned fdset_size(FDSet *fds) {
return set_size(MAKE_SET(fds));
}
+bool fdset_isempty(FDSet *fds) {
+ return set_isempty(MAKE_SET(fds));
+}
+
int fdset_iterate(FDSet *s, Iterator *i) {
void *p;
@@ -232,3 +274,13 @@ int fdset_iterate(FDSet *s, Iterator *i) {
return PTR_TO_FD(p);
}
+
+int fdset_steal_first(FDSet *fds) {
+ void *p;
+
+ p = set_steal_first(MAKE_SET(fds));
+ if (!p)
+ return -ENOENT;
+
+ return PTR_TO_FD(p);
+}
diff --git a/src/shared/fdset.h b/src/shared/fdset.h
index 907acd76dd..c3c5e52286 100644
--- a/src/shared/fdset.h
+++ b/src/shared/fdset.h
@@ -27,25 +27,30 @@
typedef struct FDSet FDSet;
FDSet* fdset_new(void);
-void fdset_free(FDSet *s);
+FDSet* fdset_free(FDSet *s);
int fdset_put(FDSet *s, int fd);
int fdset_put_dup(FDSet *s, int fd);
+int fdset_consume(FDSet *s, int fd);
bool fdset_contains(FDSet *s, int fd);
int fdset_remove(FDSet *s, int fd);
-int fdset_new_fill(FDSet **_s);
-int fdset_new_listen_fds(FDSet **_s, bool unset);
+int fdset_new_array(FDSet **ret, int *fds, unsigned n_fds);
+int fdset_new_fill(FDSet **ret);
+int fdset_new_listen_fds(FDSet **ret, bool unset);
int fdset_cloexec(FDSet *fds, bool b);
int fdset_close_others(FDSet *fds);
unsigned fdset_size(FDSet *fds);
+bool fdset_isempty(FDSet *fds);
int fdset_iterate(FDSet *s, Iterator *i);
+int fdset_steal_first(FDSet *fds);
+
#define FDSET_FOREACH(fd, fds, i) \
for ((i) = ITERATOR_FIRST, (fd) = fdset_iterate((fds), &(i)); (fd) >= 0; (fd) = fdset_iterate((fds), &(i)))
diff --git a/src/shared/util.c b/src/shared/util.c
index bda3c93ae3..f01022ed0b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -7672,3 +7672,28 @@ int fd_setcrtime(int fd, usec_t usec) {
return 0;
}
+
+int same_fd(int a, int b) {
+ struct stat sta, stb;
+
+ assert(a >= 0);
+ assert(b >= 0);
+
+ if (a == b)
+ return true;
+
+ if (fstat(a, &sta) < 0)
+ return -errno;
+
+ if (fstat(b, &stb) < 0)
+ return -errno;
+
+ if ((sta.st_mode & S_IFMT) != (stb.st_mode & S_IFMT))
+ return false;
+
+ if (S_ISREG(sta.st_mode) || S_ISDIR(sta.st_mode) || S_ISFIFO(sta.st_mode) || S_ISSOCK(sta.st_mode) || S_ISLNK(sta.st_mode))
+ return (sta.st_dev == stb.st_dev) && (sta.st_ino == stb.st_ino);
+
+ /* We consider all device fds different... */
+ return false;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index a131a3c0f1..4b7e12e628 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -1069,3 +1069,5 @@ int fd_setcrtime(int fd, usec_t usec);
int fd_getcrtime(int fd, usec_t *usec);
int path_getcrtime(const char *p, usec_t *usec);
int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);
+
+int same_fd(int a, int b);