9c49c9
From af504e99abde04b881768d18eaa0054b36b16303 Mon Sep 17 00:00:00 2001
7c0972
From: Pavel Zhukov <pzhukov@redhat.com>
7c0972
Date: Thu, 21 Feb 2019 10:21:14 +0100
9c49c9
Subject: [PATCH 03/26] Handle releasing interfaces requested by /sbin/ifup
7c0972
Cc: pzhukov@redhat.com
7c0972
7c0972
---
7c0972
 client/dhclient.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
7c0972
 1 file changed, 72 insertions(+)
7c0972
7c0972
diff --git a/client/dhclient.c b/client/dhclient.c
7c0972
index 26a333c..2a2e9e6 100644
7c0972
--- a/client/dhclient.c
7c0972
+++ b/client/dhclient.c
7c0972
@@ -787,9 +787,81 @@ main(int argc, char **argv) {
e83fb1
 				}
203b45
 			}
203b45
 			fclose(pidfd);
203b45
+		} else {
203b45
+			/* handle release for interfaces requested with Red Hat
4bf24c
+			 * /sbin/ifup - pidfile will be /var/run/dhclient-$interface.pid
203b45
+			 */
203b45
+
203b45
+			if ((path_dhclient_pid == NULL) || (*path_dhclient_pid == '\0'))
203b45
+				path_dhclient_pid = "/var/run/dhclient.pid";
203b45
+
203b45
+			char *new_path_dhclient_pid;
203b45
+			struct interface_info *ip;
203b45
+			int pdp_len = strlen(path_dhclient_pid), pfx, dpfx;
203b45
+
203b45
+			/* find append point: beginning of any trailing '.pid'
203b45
+			 * or '-$IF.pid' */
203b45
+			for (pfx=pdp_len; (pfx >= 0) && (path_dhclient_pid[pfx] != '.') && (path_dhclient_pid[pfx] != '/'); pfx--);
203b45
+				if (pfx == -1)
203b45
+					pfx = pdp_len;
203b45
+
203b45
+			if (path_dhclient_pid[pfx] == '/')
203b45
+				pfx += 1;
203b45
+
203b45
+			for (dpfx=pfx; (dpfx >= 0) && (path_dhclient_pid[dpfx] != '-') && (path_dhclient_pid[dpfx] != '/'); dpfx--);
203b45
+				if ((dpfx > -1) && (path_dhclient_pid[dpfx] != '/'))
4bf24c
+					pfx = dpfx;
203b45
+
203b45
+			for (ip = interfaces; ip; ip = ip->next) {
2b40dd
+				if (interfaces_requested && (ip->flags & (INTERFACE_REQUESTED))) {
203b45
+					int n_len = strlen(ip->name);
203b45
+
203b45
+					new_path_dhclient_pid = (char*) malloc(pfx + n_len + 6);
203b45
+					strncpy(new_path_dhclient_pid, path_dhclient_pid, pfx);
203b45
+					sprintf(new_path_dhclient_pid + pfx, "-%s.pid", ip->name);
203b45
+
203b45
+					if ((pidfd = fopen(new_path_dhclient_pid, "r")) != NULL) {
203b45
+						e = fscanf(pidfd, "%ld\n", &temp);
203b45
+						oldpid = (pid_t)temp;
203b45
+
203b45
+						if (e != 0 && e != EOF) {
203b45
+							if (oldpid) {
203b45
+								if (kill(oldpid, SIGTERM) == 0)
203b45
+									unlink(path_dhclient_pid);
203b45
+							}
203b45
+						}
203b45
+
203b45
+						fclose(pidfd);
203b45
+					}
203b45
+
203b45
+					free(new_path_dhclient_pid);
203b45
+				}
203b45
+			}
203b45
+		}
203b45
+	} else {
203b45
+		FILE *pidfp = NULL;
203b45
+		long temp = 0;
203b45
+		pid_t dhcpid = 0;
203b45
+		int dhc_running = 0;
203b45
+		char procfn[256] = "";
203b45
+
203b45
+		if ((pidfp = fopen(path_dhclient_pid, "r")) != NULL) {
203b45
+			if ((fscanf(pidfp, "%ld", &temp)==1) && ((dhcpid=(pid_t)temp) > 0)) {
203b45
+				snprintf(procfn,256,"/proc/%u",dhcpid);
4bf24c
+				dhc_running = (access(procfn, F_OK) == 0);
203b45
+			}
203b45
+
203b45
+			fclose(pidfp);
203b45
+		}
203b45
+
203b45
+		if (dhc_running) {
203b45
+			log_fatal("dhclient(%u) is already running - exiting. ", dhcpid);
203b45
+			return(1);
203b45
 		}
203b45
 	}
203b45
 
203b45
+	write_client_pid_file();
203b45
+
203b45
 	if (!quiet) {
4bf24c
 		log_info("%s %s", message, PACKAGE_VERSION);
4bf24c
 		log_info(copyright);
7c0972
-- 
7c0972
2.14.5
7c0972