Set a progname to send with our command.
authorRoy Marples <roy@marples.name>
Tue, 16 Sep 2014 10:10:38 +0000 (10:10 +0000)
committerRoy Marples <roy@marples.name>
Tue, 16 Sep 2014 10:10:38 +0000 (10:10 +0000)
Special ctrl commands to not take a progname.
Fix compile on NetBSD.

src/dhcpcd-gtk/main.c
src/dhcpcd-qt/dhcpcd-preferences.cpp
src/dhcpcd-qt/dhcpcd-qt.cpp
src/libdhcpcd/dhcpcd.c
src/libdhcpcd/dhcpcd.h

index 9226cb12cb82128fb63264ceec0ae8ff125c5112..5b9d8abf8d1bff1d84b9fd9981273afe5117c4a3 100644 (file)
@@ -596,6 +596,7 @@ main(int argc, char *argv[])
                g_critical("libdhcpcd: %s", strerror(errno));
                exit(EXIT_FAILURE);
        }
+       dhcpcd_set_progname(con, "dhcpcd-gtk");
        dhcpcd_set_status_callback(con, dhcpcd_status_cb, NULL);
        dhcpcd_set_if_callback(con, dhcpcd_if_cb, NULL);
        dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, NULL);
index b7e349011f73f6fbd5184ab904cc8a1e2234dd53..02974a7a912c93f9fd7bf5513561c7986bb5df52 100644 (file)
@@ -40,6 +40,8 @@
 #include <QStandardItemModel>
 #include <QVBoxLayout>
 
+#include <cerrno>
+
 #include "config.h"
 #include "dhcpcd-preferences.h"
 #include "dhcpcd-ipv4validator.h"
@@ -103,7 +105,7 @@ DhcpcdPreferences::DhcpcdPreferences(DhcpcdQt *parent)
        dnssl = new QLineEdit();
 #if defined(__NetBSD__) || (__OpenBSD__)
        dnssl->setMaxLength(1024);
-else
+#else
        dnssl->setMaxLength(256);
 #endif
        QFormLayout *ipLayout = new QFormLayout();
index 7fb0b8c6e3f5120da1c915220380a4ad3e4d1a9c..e7a3d808f83218be811f8c9600d10d91592655a0 100644 (file)
@@ -66,6 +66,7 @@ DhcpcdQt::DhcpcdQt()
                exit(EXIT_FAILURE);
                return;
        }
+       dhcpcd_set_progname(con, "dhcpcd-qt");
        dhcpcd_set_status_callback(con, dhcpcd_status_cb, this);
        dhcpcd_set_if_callback(con, dhcpcd_if_cb, this);
        dhcpcd_wpa_set_scan_callback(con, dhcpcd_wpa_scan_cb, this);
index 13e9c8ec4606acf49634bbd2d0380c183f78b2a2..508137ce51b6c904505082b6a251590a5dbe92cd 100644 (file)
@@ -65,9 +65,9 @@ static const char * const dhcpcd_types[] =
 
 static ssize_t
 dhcpcd_command_fd(DHCPCD_CONNECTION *con,
-    int fd, const char *cmd, char **buffer)
+    int fd, bool progname, const char *cmd, char **buffer)
 {
-       size_t len;
+       size_t pl, cl, len;
        ssize_t bytes;
        char buf[1024], *p;
        char *nbuf;
@@ -76,15 +76,28 @@ dhcpcd_command_fd(DHCPCD_CONNECTION *con,
         * Each argument is NULL seperated.
         * We may need to send a space one day, so the API
         * in this function may need to be improved */
-       len = strlen(cmd) + 1;
+       cl = strlen(cmd);
+       if (progname) {
+               pl = strlen(con->progname);
+               len = pl + 1 + cl + 1;
+       } else {
+               pl = 0;
+               len = cl + 1;
+       }
        if (con->terminate_commands)
                len++;
        if (len > sizeof(buf)) {
                errno = ENOBUFS;
                return -1;
        }
-       strlcpy(buf, cmd, sizeof(buf));
        p = buf;
+       if (progname) {
+               memcpy(buf, con->progname, pl);
+               buf[pl] = '\0';
+               p = buf + pl + 1;
+       }
+       memcpy(p, cmd, cl);
+       p[cl] = '\0';
        while ((p = strchr(p, ' ')) != NULL)
                *p++ = '\0';
        if (con->terminate_commands) {
@@ -115,7 +128,14 @@ ssize_t
 dhcpcd_command(DHCPCD_CONNECTION *con, const char *cmd, char **buffer)
 {
 
-       return dhcpcd_command_fd(con, con->command_fd, cmd, buffer);
+       return dhcpcd_command_fd(con, con->command_fd, true, cmd, buffer);
+}
+
+static ssize_t
+dhcpcd_ctrl_command(DHCPCD_CONNECTION *con, const char *cmd, char **buffer)
+{
+
+       return dhcpcd_command_fd(con, con->command_fd, false, cmd, buffer);
 }
 
 bool
@@ -153,7 +173,7 @@ dhcpcd_command_arg(DHCPCD_CONNECTION *con, const char *cmd, const char *arg,
                strlcpy(con->buf + cmdlen + 1, arg, con->buflen - 1 - cmdlen);
        }
 
-       return dhcpcd_command_fd(con, con->command_fd, con->buf, buffer);
+       return dhcpcd_command_fd(con, con->command_fd, true, con->buf, buffer);
 }
 
 
@@ -548,9 +568,26 @@ dhcpcd_new(void)
        con = calloc(1, sizeof(*con));
        con->command_fd = con->listen_fd = -1;
        con->open = false;
+       con->progname = "libdhcpcd";
        return con;
 }
 
+void
+dhcpcd_set_progname(DHCPCD_CONNECTION *con, const char *progname)
+{
+
+       assert(con);
+       con->progname = progname;
+}
+
+const char *
+dhcpcd_get_progname(const DHCPCD_CONNECTION *con)
+{
+
+       assert(con);
+       return con->progname;
+}
+
 #ifndef __GLIBC__
 /* Good enough for our needs */
 static int
@@ -597,12 +634,12 @@ dhcpcd_open(DHCPCD_CONNECTION *con, bool privileged)
                goto err_exit;
 
        con->terminate_commands = false;
-       if (dhcpcd_command(con, "--version", &con->version) <= 0)
+       if (dhcpcd_ctrl_command(con, "--version", &con->version) <= 0)
                goto err_exit;
        con->terminate_commands =
            strverscmp(con->version, "6.4.1") >= 0 ? true : false;
 
-       if (dhcpcd_command(con, "--getconfigfile", &con->cffile) <= 0)
+       if (dhcpcd_ctrl_command(con, "--getconfigfile", &con->cffile) <= 0)
                goto err_exit;
 
        con->open = true;
@@ -613,8 +650,8 @@ dhcpcd_open(DHCPCD_CONNECTION *con, bool privileged)
        if (con->listen_fd == -1)
                goto err_exit;
 
-       dhcpcd_command_fd(con, con->listen_fd, "--listen", NULL);
-       dhcpcd_command_fd(con, con->command_fd, "--getinterfaces", NULL);
+       dhcpcd_command_fd(con, con->listen_fd, false, "--listen", NULL);
+       dhcpcd_command_fd(con, con->command_fd, false, "--getinterfaces", NULL);
        bytes = read(con->command_fd, cmd, sizeof(nifs));
        if (bytes != sizeof(nifs))
                goto err_exit;
index 39aa36e299fe2c5a3af1e5d038b0976e672189f2..68f9a88eb1360d7cca647fd284dfb77b6613ee25 100644 (file)
@@ -142,6 +142,7 @@ typedef struct dhcpcd_connection {
        bool privileged;
        int command_fd;
        int listen_fd;
+       const char *progname;
 
        DHCPCD_IF *interfaces;
        DHCPCD_WPA *wpa;
@@ -178,6 +179,8 @@ typedef void *DHCPCD_CONNECTION;
 
 DHCPCD_CONNECTION * dhcpcd_new(void);
 const char * dhcpcd_version(DHCPCD_CONNECTION *);
+void dhcpcd_set_progname(DHCPCD_CONNECTION *, const char *);
+const char * dhcpcd_get_progname(const DHCPCD_CONNECTION *);
 const char * dhcpcd_status(DHCPCD_CONNECTION *);
 const char * dhcpcd_cffile(DHCPCD_CONNECTION *);
 bool dhcpcd_realloc(DHCPCD_CONNECTION *, size_t);