]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: introduce fd_set_{snd,rcv}buf()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 8 Sep 2020 16:12:38 +0000 (01:12 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 8 Sep 2020 21:39:05 +0000 (06:39 +0900)
src/basic/socket-util.c
src/basic/socket-util.h

index da207377e073a3e0c705581b2efb4c21595bc12f..4b3a4f206cdac249e4fe79b5e60694c2c8ea7a63 100644 (file)
@@ -617,7 +617,7 @@ bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b
         return false;
 }
 
-int fd_inc_sndbuf(int fd, size_t n) {
+int fd_set_sndbuf(int fd, size_t n, bool increase) {
         int r, value;
         socklen_t l = sizeof(value);
 
@@ -625,7 +625,7 @@ int fd_inc_sndbuf(int fd, size_t n) {
                 return -ERANGE;
 
         r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
-        if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
+        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                 return 0;
 
         /* First, try to set the buffer size with SO_SNDBUF. */
@@ -636,7 +636,7 @@ int fd_inc_sndbuf(int fd, size_t n) {
         /* SO_SNDBUF above may set to the kernel limit, instead of the requested size.
          * So, we need to check the actual buffer size here. */
         r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
-        if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
+        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                 return 1;
 
         /* If we have the privileges we will ignore the kernel limit. */
@@ -647,7 +647,7 @@ int fd_inc_sndbuf(int fd, size_t n) {
         return 1;
 }
 
-int fd_inc_rcvbuf(int fd, size_t n) {
+int fd_set_rcvbuf(int fd, size_t n, bool increase) {
         int r, value;
         socklen_t l = sizeof(value);
 
@@ -655,7 +655,7 @@ int fd_inc_rcvbuf(int fd, size_t n) {
                 return -ERANGE;
 
         r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
-        if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
+        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                 return 0;
 
         /* First, try to set the buffer size with SO_RCVBUF. */
@@ -666,7 +666,7 @@ int fd_inc_rcvbuf(int fd, size_t n) {
         /* SO_RCVBUF above may set to the kernel limit, instead of the requested size.
          * So, we need to check the actual buffer size here. */
         r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
-        if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2)
+        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
                 return 1;
 
         /* If we have the privileges we will ignore the kernel limit. */
index 5610a99e53318f4ba23e850574eca130b6d01aac..6a320a976f434a59a6dbc35273b1e928466b235b 100644 (file)
@@ -118,8 +118,14 @@ int netlink_family_from_string(const char *s) _pure_;
 
 bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b);
 
-int fd_inc_sndbuf(int fd, size_t n);
-int fd_inc_rcvbuf(int fd, size_t n);
+int fd_set_sndbuf(int fd, size_t n, bool increase);
+static inline int fd_inc_sndbuf(int fd, size_t n) {
+        return fd_set_sndbuf(fd, n, true);
+}
+int fd_set_rcvbuf(int fd, size_t n, bool increase);
+static inline int fd_inc_rcvbuf(int fd, size_t n) {
+        return fd_set_rcvbuf(fd, n, true);
+}
 
 int ip_tos_to_string_alloc(int i, char **s);
 int ip_tos_from_string(const char *s);