X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Ffdset.c;h=a4823e6659623602d71227929e797d83d87e38ef;hp=fd27398ebbec75f7a3554882fc7e017d1128ae5d;hb=545407b3b250edd0a05e314a0b5cf73cdb5a74fa;hpb=f274ece0f76b5709408821e317e87aef76123db6 diff --git a/src/shared/fdset.c b/src/shared/fdset.c index fd27398eb..a4823e665 100644 --- a/src/shared/fdset.c +++ b/src/shared/fdset.c @@ -22,7 +22,6 @@ #include #include #include -#include #include "set.h" #include "util.h" @@ -33,15 +32,39 @@ #define MAKE_SET(s) ((Set*) s) #define MAKE_FDSET(s) ((FDSet*) s) -/* Make sure we can distuingish fd 0 and NULL */ +/* Make sure we can distinguish fd 0 and NULL */ #define FD_TO_PTR(fd) INT_TO_PTR((fd)+1) #define PTR_TO_FD(p) (PTR_TO_INT(p)-1) FDSet *fdset_new(void) { - return MAKE_FDSET(set_new(trivial_hash_func, trivial_compare_func)); + 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 +84,7 @@ void fdset_free(FDSet *s) { } set_free(MAKE_SET(s)); + return NULL; } int fdset_put(FDSet *s, int fd) { @@ -70,6 +94,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; @@ -82,7 +119,7 @@ int fdset_put_dup(FDSet *s, int fd) { r = fdset_put(s, copy); if (r < 0) { - close_nointr_nofail(copy); + safe_close(copy); return r; } @@ -104,7 +141,7 @@ int fdset_remove(FDSet *s, int fd) { } int fdset_new_fill(FDSet **_s) { - DIR *d; + _cleanup_closedir_ DIR *d = NULL; struct dirent *de; int r = 0; FDSet *s; @@ -127,7 +164,7 @@ int fdset_new_fill(FDSet **_s) { while ((de = readdir(d))) { int fd = -1; - if (ignore_file(de->d_name)) + if (hidden_file(de->d_name)) continue; r = safe_atoi(de->d_name, &fd); @@ -150,8 +187,6 @@ int fdset_new_fill(FDSet **_s) { s = NULL; finish: - closedir(d); - /* We won't close the fds here! */ if (s) set_free(MAKE_SET(s)); @@ -225,10 +260,23 @@ 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; - p = set_iterate(MAKE_SET(s), i); + if (!set_iterate(MAKE_SET(s), i, &p)) + return -ENOENT; + + return PTR_TO_FD(p); +} + +int fdset_steal_first(FDSet *fds) { + void *p; + + p = set_steal_first(MAKE_SET(fds)); if (!p) return -ENOENT;