]> granicus.if.org Git - pgbouncer/commitdiff
<usual/socket.h>
authorMarko Kreen <markokr@gmail.com>
Mon, 11 Jan 2010 06:07:30 +0000 (08:07 +0200)
committerMarko Kreen <markokr@gmail.com>
Tue, 4 May 2010 11:30:50 +0000 (14:30 +0300)
include/bouncer.h
include/system.h
include/util.h
src/system.c
src/util.c

index 441dee02b8d8023e5931a51c437d4dc2c5a0fe3f..b415ced53c4ef27687b93f3da0777451712d1d91 100644 (file)
@@ -30,6 +30,7 @@
 #include <usual/aatree.h>
 #include <usual/lookup3.h>
 #include <usual/slab.h>
+#include <usual/socket.h>
 
 #include <event.h>
 
index beb7d0f9b850c6e0f652fcbe39ab2d5b01d978c3..ced43b8233ed7977f7ec23253a4e8b64ddf93d63 100644 (file)
 
 #include <sys/stat.h>
 
-#ifdef HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
-#ifdef HAVE_SYS_UN_H
-#include <sys/un.h>
-#endif
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif
-#ifdef HAVE_NETINET_TCP_H
-#include <netinet/tcp.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
-#include <arpa/inet.h>
-#endif
-#ifdef HAVE_ARPA_INET_H
-#include <sys/resource.h>
-#endif
-
-#include <fcntl.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <ctype.h>
  * libc compat functions.
  */
 
-#ifndef HAVE_GETPEEREID
-int getpeereid(int fd, uid_t *uid_p, gid_t *gid_p) _MUSTCHECK;
-#endif
 #ifndef HAVE_CRYPT
 static inline char *crypt(const char *p, const char *s) { return NULL; }
 #endif
-#ifndef HAVE_INET_NTOP
-const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
-#endif
 #ifndef HAVE_LSTAT
 static inline int lstat(const char *path, struct stat *st) { return stat(path, st); }
 #endif
index 7f5f835d40c3738b5d75d8a7083df975ff733cb4..4bbc91c6fc3d316dc6bc39e1d1b06420e64ac9a6 100644 (file)
@@ -60,7 +60,6 @@ int safe_accept(int fd, struct sockaddr *sa, socklen_t *sa_len)       _MUSTCHECK;
 void pg_md5_encrypt(const char *part1, const char *part2, size_t p2len, char *dest);
 void get_random_bytes(uint8_t *dest, int len);
 
-void socket_set_nonblocking(int fd, int val);
 void tune_socket(int sock, bool is_unix);
 
 bool strlist_contains(const char *liststr, const char *str);
index abf92c2ddc00942c975d1f7d9fb27b0c684168c8..8447495a51812de4281c03f63941430e8220cddc 100644 (file)
 #include <grp.h>
 #endif
 
-/*
- * Get other side's uid for UNIX socket.
- *
- * Standardise on getpeereid() from BSDs.
- */
-#ifndef HAVE_GETPEEREID
-int getpeereid(int fd, uid_t *uid_p, gid_t *gid_p)
-{
-#ifdef SO_PEERCRED
-       struct ucred cred;
-       socklen_t len = sizeof(cred);
-       if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) >= 0) {
-               *uid_p = cred.uid;
-               *gid_p = cred.gid;
-               return 0;
-       }
-#else /* !SO_PEERCRED */
-#ifdef HAVE_GETPEERUCRED
-       ucred_t *cred = NULL;
-       if (getpeerucred(fd, &cred) >= 0) {
-               *uid_p = ucred_geteuid(cred);
-               *gid_p = ucred_getegid(cred);
-               ucred_free(cred);
-               if (*uid_p >= 0 && *gid_p >= 0)
-                       return 0;
-       }
-#endif /* HAVE_GETPEERUCRED */
-#endif /* !SO_PEERCRED */
-       return -1;
-}
-#endif /* !HAVE_GETPEEREID */
-
 void change_user(const char *user)
 {
        const struct passwd *pw;
@@ -93,15 +61,3 @@ void change_user(const char *user)
                fatal("setuid() failed to work");
 }
 
-#ifndef HAVE_INET_NTOP
-const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
-{
-       const unsigned char *p = src;
-       if (af != AF_INET)
-               return NULL;
-       snprintf(dst, cnt, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
-       return dst;
-}
-#endif
-
-
index 1633456dd87734ba9d27e5315a840fce6638d0ca..3e3a588e737a309994b1c443d1cd5760c0db187f 100644 (file)
@@ -164,39 +164,25 @@ loop:
        return res;
 }
 
-static const char *sa2str(const struct sockaddr *sa)
-{
-       static char buf[256];
-
-       if (sa->sa_family == AF_INET) {
-               struct sockaddr_in *in = (struct sockaddr_in *)sa;
-               snprintf(buf, sizeof(buf), "%s:%d", inet_ntoa(in->sin_addr), ntohs(in->sin_port));
-       } if (sa->sa_family == AF_UNIX) {
-               struct sockaddr_un *un = (struct sockaddr_un *)sa;
-               snprintf(buf, sizeof(buf), "unix:%s", un->sun_path);
-       } else {
-               snprintf(buf, sizeof(buf), "sa2str: unknown proto");
-       }
-       return buf;
-}
-
 int safe_connect(int fd, const struct sockaddr *sa, socklen_t sa_len)
 {
        int res;
+       char buf[128];
 loop:
        res = connect(fd, sa, sa_len);
        if (res < 0 && errno == EINTR)
                goto loop;
        if (res < 0 && (errno != EINPROGRESS || cf_verbose > 2))
-               log_noise("connect(%d, %s) = %s", fd, sa2str(sa), strerror(errno));
+               log_noise("connect(%d, %s) = %s", fd, sa2str(sa, buf, sizeof(buf)), strerror(errno));
        else if (cf_verbose > 2)
-               log_noise("connect(%d, %s) = %d", fd, sa2str(sa), res);
+               log_noise("connect(%d, %s) = %d", fd, sa2str(sa, buf, sizeof(buf)), res);
        return res;
 }
 
 int safe_accept(int fd, struct sockaddr *sa, socklen_t *sa_len_p)
 {
        int res;
+       char buf[128];
 loop:
        res = accept(fd, sa, sa_len_p);
        if (res < 0 && errno == EINTR)
@@ -204,7 +190,7 @@ loop:
        if (res < 0)
                log_noise("safe_accept(%d) = %s", fd, strerror(errno));
        else if (cf_verbose > 2)
-               log_noise("safe_accept(%d) = %d (%s)", fd, res, sa2str(sa));
+               log_noise("safe_accept(%d) = %d (%s)", fd, res, sa2str(sa, buf, sizeof(buf)));
        return res;
 }
 
@@ -290,27 +276,6 @@ void get_random_bytes(uint8_t *dest, int len)
                dest[i] = random() & 255;
 }
 
-void socket_set_nonblocking(int fd, int val)
-{
-       int flags, res;
-
-       /* get old flags */
-       flags = fcntl(fd, F_GETFL, 0);
-       if (flags < 0)
-               fatal_perror("fcntl(F_GETFL)");
-
-       /* flip O_NONBLOCK */
-       if (val)
-               flags |= O_NONBLOCK;
-       else
-               flags &= ~O_NONBLOCK;
-
-       /* set new flags */
-       res = fcntl(fd, F_SETFL, flags);
-       if (res < 0)
-               fatal_perror("fcntl(F_SETFL)");
-}
-
 /* set needed socket options */
 void tune_socket(int sock, bool is_unix)
 {