Split out unix socket connection function
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 18 Mar 2015 17:03:11 +0000 (13:03 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 18 Mar 2015 17:03:11 +0000 (13:03 -0400)
src/include/libradius.h
src/lib/all.mk
src/lib/unix.c [new file with mode: 0644]
src/main/radmin.c

index 5233715..fff0df0 100644 (file)
@@ -897,6 +897,11 @@ void               *fr_fifo_pop(fr_fifo_t *fi);
 void           *fr_fifo_peek(fr_fifo_t *fi);
 int            fr_fifo_num_elements(fr_fifo_t *fi);
 
+/*
+ *     Unix domain sockets
+ */
+int            fr_domain_socket(char const *path);
+
 #ifdef __cplusplus
 }
 #endif
index c323438..f9e78b6 100644 (file)
@@ -32,6 +32,7 @@ SOURCES               := cbuff.c \
                   strlcpy.c \
                   token.c \
                   udpfromto.c \
+                  unix.c \
                   value.c \
                   fifo.c \
                   packet.c \
diff --git a/src/lib/unix.c b/src/lib/unix.c
new file mode 100644 (file)
index 0000000..bf69175
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+/**
+ * $Id$
+ * @file unix.c
+ * @brief Functions to work with Unix domain sockets.
+ *
+ * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
+ * @copyright 2015 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
+ */
+RCSID("$Id$")
+
+#include <freeradius-devel/libradius.h>
+
+#ifdef HAVE_SYS_UN_H
+#  include <sys/un.h>
+#  ifndef SUN_LEN
+#    define SUN_LEN(su)  (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
+#  endif
+
+/** Open a Unix socket
+ *
+ * @note If the file doesn't exist then errno will be set to ENOENT.
+ *
+ * @param path to the file bound to the unix socket.
+ * @return 0 on success, -1 on error.
+ */
+int fr_domain_socket(char const *path)
+{
+       int                     sockfd = -1;
+       size_t                  len;
+       socklen_t               socklen;
+       struct sockaddr_un      saremote;
+
+       len = strlen(path);
+       if (len >= sizeof(saremote.sun_path)) {
+               fr_strerror_printf("Path too long, maximum length is %zu", sizeof(saremote.sun_path) - 1);
+               errno = EINVAL;
+               return -1;
+       }
+
+       if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+               fr_strerror_printf("Failed creating socket: %s", fr_syserror(errno));
+               return -1;
+       }
+
+       saremote.sun_family = AF_UNIX;
+       memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
+
+       socklen = SUN_LEN(&saremote);
+
+       if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
+               close(sockfd);
+               fr_strerror_printf("Failed connecting to %s: %s", path, fr_syserror(errno));
+
+               return -1;
+       }
+       return sockfd;
+}
+#else
+int fr_domain_socket(UNUSED char const *path)
+{
+       fprintf(stderr, "Unix domain sockets not supported on this system");
+       return -1;
+}
+#endif /* WITH_SYS_UN_H */
index e84692a..daac762 100644 (file)
@@ -30,13 +30,6 @@ RCSID("$Id$")
 #include <pwd.h>
 #include <grp.h>
 
-#ifdef HAVE_SYS_UN_H
-#  include <sys/un.h>
-#  ifndef SUN_LEN
-#    define SUN_LEN(su)  (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
-#  endif
-#endif
-
 #ifdef HAVE_GETOPT_H
 #  include <getopt.h>
 #endif
@@ -122,53 +115,6 @@ static void NEVER_RETURNS usage(int status)
        exit(status);
 }
 
-static int fr_domain_socket(char const *path)
-{
-       int sockfd = -1;
-#ifdef HAVE_SYS_UN_H
-       size_t len;
-       socklen_t socklen;
-       struct sockaddr_un saremote;
-
-       len = strlen(path);
-       if (len >= sizeof(saremote.sun_path)) {
-               fprintf(stderr, "%s: Path too long in filename\n", progname);
-               return -1;
-       }
-
-       if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-               fprintf(stderr, "%s: Failed creating socket: %s\n",
-                       progname, fr_syserror(errno));
-               return -1;
-       }
-
-       saremote.sun_family = AF_UNIX;
-       memcpy(saremote.sun_path, path, len + 1); /* SUN_LEN does strlen */
-
-       socklen = SUN_LEN(&saremote);
-
-       if (connect(sockfd, (struct sockaddr *)&saremote, socklen) < 0) {
-               struct stat buf;
-
-               close(sockfd);
-               fprintf(stderr, "%s: Failed connecting to %s: %s\n",
-                       progname, path, fr_syserror(errno));
-
-               /*
-                *      The file doesn't exist.  Tell the user how to
-                *      fix it.
-                */
-               if ((stat(path, &buf) < 0) &&
-                   (errno == ENOENT)) {
-                       fprintf(stderr, "  Perhaps you need to run the commands:\n\tcd /etc/raddb\n\tln -s sites-available/control-socket sites-enabled/control-socket\n  and then re-start the server?\n");
-               }
-
-               return -1;
-       }
-#endif
-       return sockfd;
-}
-
 static int client_socket(char const *server)
 {
        int sockfd;
@@ -311,7 +257,17 @@ static int do_connect(int *out, char const *file, char const *server)
                 *      FIXME: Get destination from command line, if possible?
                 */
                sockfd = fr_domain_socket(file);
-               if (sockfd < 0) return -1;
+               if (sockfd < 0) {
+                       fr_perror("radmin");
+                       if (errno == ENOENT) {
+                                       fprintf(stderr, "Perhaps you need to run the commands:");
+                                       fprintf(stderr, "\tcd /etc/raddb\n");
+                                       fprintf(stderr, "\tln -s sites-available/control-socket "
+                                               "sites-enabled/control-socket\n");
+                                       fprintf(stderr, "and then re-start the server?\n");
+                       }
+                       return -1;
+               }
        } else {
                sockfd = client_socket(server);
        }