[Git][NTPsec/ntpsec][master] Write pidfile in parent

Richard Laager (@rlaager) gitlab at mg.gitlab.com
Sun Mar 19 21:10:38 UTC 2023



Richard Laager pushed to branch master at NTPsec / ntpsec


Commits:
3418b33f by Richard Laager at 2023-03-19T21:05:04+00:00
Write pidfile in parent

systemd expects the PID file to be written by the parent process, not the
child.  Fixing this eliminates an error message:
    Can't open PID file /run/ntpd.pid (yet?) after start:
    Operation not permitted

- - - - -


3 changed files:

- include/ntpd.h
- ntpd/ntp_util.c
- ntpd/ntpd.c


Changes:

=====================================
include/ntpd.h
=====================================
@@ -231,6 +231,7 @@ extern	uptime_t	orphwait;		/* orphan wait time */
 
 /* ntp_util.c */
 extern	void	init_util	(void);
+extern	void 	write_pidfile	(const char *, pid_t);
 extern	void	write_stats	(void);
 extern	void	stats_config	(int, const char *);
 extern	void	record_peer_stats (struct peer *, int);


=====================================
ntpd/ntp_util.c
=====================================
@@ -142,6 +142,24 @@ init_util(void)
 #endif /* DEBUG */
 }
 
+/*
+ * write_pidfile - write the PID file
+ */
+void
+write_pidfile(
+	const char *pidfile,
+	pid_t pid)
+{
+	FILE	*fp;
+
+	if ((fp = fopen(pidfile, "w")) == NULL) {
+		msyslog(LOG_ERR, "LOG: pid file %s: %s",
+		    pidfile, strerror(errno));
+		return;
+	}
+	fprintf(fp, "%ld", (long)pid);
+	fclose(fp);
+}
 
 /*
  * drift_write - write drift to file, speeds up restart
@@ -231,7 +249,6 @@ stats_config(
 	const char *invalue	/* only one type so far */
 	)
 {
-	FILE	*fp;
 	const char *value;
 	size_t	len;
 	double	new_drift = 0;
@@ -298,13 +315,7 @@ stats_config(
 	 * Open pid file.
 	 */
 	case STATS_PID_FILE:
-		if ((fp = fopen(value, "w")) == NULL) {
-			msyslog(LOG_ERR, "LOG: pid file %s: %s",
-			    value, strerror(errno));
-			break;
-		}
-		fprintf(fp, "%d", (int)getpid());
-		fclose(fp);
+		write_pidfile(value, getpid());
 		break;
 
 	/*


=====================================
ntpd/ntpd.c
=====================================
@@ -561,6 +561,8 @@ main(
 		}
 		if (rc > 0) {
 			/* parent */
+			if (pidfile)
+				write_pidfile(pidfile, rc);
 			exit_code = wait_child_sync_if(pipe_fds[0],
 						       wait_sync);
 			exit(exit_code);
@@ -586,6 +588,8 @@ main(
 		sa.sa_flags = SA_RESTART;
 		sigaction(SIGDANGER, &sa, NULL);
 #endif	/* SIGDANGER */
+	} else if (pidfile) {
+		write_pidfile(pidfile, getpid());
 	}
 
 	/*
@@ -684,11 +688,7 @@ main(
             case 'm':
             case 'n':
             case 'N':
-                /* handled elsewhere */
-                break;
-	    case 'p':
-		stats_config(STATS_PID_FILE, pidfile);
-		break;
+            case 'p':
             case 'P':
             case 'q':
                 /* handled elsewhere */



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/3418b33fc969af3bf9bb0fd91fec5080925853ed

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/3418b33fc969af3bf9bb0fd91fec5080925853ed
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20230319/97409399/attachment-0001.htm>


More information about the vc mailing list