.argv = argv,
.argp = 0
};
+ static int lockfd = -1;
cmdenv_push(&env, root);
if (!completion)
for (n = 0; n < argc; n++)
/* 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++;
}