+ if (zcp->pid_fd < 0) {
+ zed_log_msg(LOG_ERR, "Failed to open PID file \"%s\": %s",
+ zcp->pid_file, strerror(errno));
+ goto err;
+ }
+ rv = zed_file_lock(zcp->pid_fd);
+ if (rv < 0) {
+ zed_log_msg(LOG_ERR, "Failed to lock PID file \"%s\": %s",
+ zcp->pid_file, strerror(errno));
+ goto err;
+ } else if (rv > 0) {
+ pid_t pid = zed_file_is_locked(zcp->pid_fd);
+ if (pid < 0) {
+ zed_log_msg(LOG_ERR,
+ "Failed to test lock on PID file \"%s\"",
+ zcp->pid_file);
+ } else if (pid > 0) {
+ zed_log_msg(LOG_ERR,
+ "Found PID %d bound to PID file \"%s\"",
+ pid, zcp->pid_file);
+ } else {
+ zed_log_msg(LOG_ERR,
+ "Inconsistent lock state on PID file \"%s\"",
+ zcp->pid_file);
+ }
+ goto err;
+ }
+ /*
+ * Write PID file.
+ */
+ n = snprintf(buf, sizeof (buf), "%d\n", (int)getpid());
+ if ((n < 0) || (n >= sizeof (buf))) {
+ errno = ERANGE;
+ zed_log_msg(LOG_ERR, "Failed to write PID file \"%s\": %s",