]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldpctl: don't reopen control socket each time we want to lock
authorVincent Bernat <vincent@bernat.ch>
Sun, 29 Sep 2019 12:09:54 +0000 (14:09 +0200)
committerVincent Bernat <vincent@bernat.ch>
Sun, 29 Sep 2019 14:10:52 +0000 (16:10 +0200)
src/client/commands.c

index 905652ee073997895d26f6ce35af3d5831450a26..291a63b6ce753f450568fe3b5e1f2dcd6892bb19 100644 (file)
@@ -371,6 +371,7 @@ _commands_execute(struct lldpctl_conn_t *conn, struct writer *w,
                .argv = argv,
                .argp = 0
        };
+       static int lockfd = -1;
        cmdenv_push(&env, root);
        if (!completion)
                for (n = 0; n < argc; n++)
@@ -444,36 +445,37 @@ _commands_execute(struct lldpctl_conn_t *conn, struct writer *w,
                /* Push and execute */
                cmdenv_push(&env, best);
                if (best->execute) {
-                       int lockfd;
                        struct sockaddr_un su;
                        if (needlock) {
-                               log_debug("lldpctl", "getting lock for %s", ctlname);
-                               if ((lockfd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
-                                       log_warn("lldpctl", "cannot open for lock %s", ctlname);
-                                       rc = -1;
-                                       goto end;
-                               }
-                               su.sun_family = AF_UNIX;
-                               strlcpy(su.sun_path, ctlname, sizeof(su.sun_path));
-                               if (connect(lockfd, (struct sockaddr *)&su, sizeof(struct sockaddr_un)) == -1) {
-                                       log_warn("lldpctl", "cannot connect to socket %s", ctlname);
-                                       rc = -1;
-                                       close(lockfd);
-                                       goto end;
+                               if (lockfd == -1) {
+                                       log_debug("lldpctl", "reopen %s for locking", ctlname);
+                                       if ((lockfd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
+                                               log_warn("lldpctl", "cannot open for lock %s", ctlname);
+                                               rc = -1;
+                                               goto end;
+                                       }
+                                       su.sun_family = AF_UNIX;
+                                       strlcpy(su.sun_path, ctlname, sizeof(su.sun_path));
+                                       if (connect(lockfd, (struct sockaddr *)&su, sizeof(struct sockaddr_un)) == -1) {
+                                               log_warn("lldpctl", "cannot connect to socket %s", ctlname);
+                                               rc = -1;
+                                               close(lockfd); lockfd = -1;
+                                               goto end;
+                                       }
                                }
                                if (lockf(lockfd, F_LOCK, 0) == -1) {
                                        log_warn("lldpctl", "cannot get lock on %s", ctlname);
                                        rc = -1;
-                                       close(lockfd);
+                                       close(lockfd); lockfd = -1;
                                        goto end;
                                }
                        }
-                       if (best->execute(conn, w, &env, best->arg) != 1) {
-                               rc = -1;
-                               if (needlock) close(lockfd);
-                               goto end;
+                       rc = best->execute(conn, w, &env, best->arg) != 1 ? -1 : rc;
+                       if (needlock && lockf(lockfd, F_ULOCK, 0) == -1) {
+                               log_warn("lldpctl", "cannot unlock %s", ctlname);
+                               close(lockfd); lockfd = -1;
                        }
-                       close(lockfd);
+                       if (rc == -1) goto end;
                }
                env.argp++;
        }