Add support to set primary-dns server configuration for Android

Add support to configure primary-dns server for Android platforms using
dnsmasq.

Signed-off-by: Shivani Baranwal <quic_shivbara@quicinc.com>
diff --git a/sigma_dut.h b/sigma_dut.h
index 7032421..a54d3ec 100644
--- a/sigma_dut.h
+++ b/sigma_dut.h
@@ -1311,5 +1311,6 @@
 void miracast_register_cmds(void);
 int set_ipv6_addr(struct sigma_dut *dut, const char *ip, const char *mask,
 		  const char *ifname);
+void kill_pid(struct sigma_dut *dut, const char *pid_file);
 
 #endif /* SIGMA_DUT_H */
diff --git a/sta.c b/sta.c
index 5230feb..07f18e6 100644
--- a/sta.c
+++ b/sta.c
@@ -1594,9 +1594,24 @@
 	val = get_param(cmd, "primary-dns");
 	if (val) {
 #ifdef ANDROID
-		/* TODO */
-		sigma_dut_print(dut, DUT_MSG_INFO, "Ignored primary-dns %s "
-				"setting", val);
+		char dns_cmd[200];
+		int len;
+		char dnsmasq[100];
+
+		kill_pid(dut, concat_sigma_tmpdir(dut, "/sigma_dut-dnsmasq.pid",
+						  dnsmasq, sizeof(dnsmasq)));
+
+		len = snprintf(dns_cmd, sizeof(dns_cmd),
+			       "/system/bin/dnsmasq -uroot --no-resolv -S%s -x/%s", val,
+			       dnsmasq);
+		if (len < 0 || len >= sizeof(dns_cmd))
+			return ERROR_SEND_STATUS;
+		sigma_dut_print(dut, DUT_MSG_DEBUG, "Running %s", dns_cmd);
+		if (system(dns_cmd) != 0) {
+			send_resp(dut, conn, SIGMA_ERROR,
+				  "ErrorCode,Failed to set primary-dns");
+			return STATUS_SENT_ERROR;
+		}
 #else /* ANDROID */
 		char dns_cmd[200];
 		int len;
@@ -9170,6 +9185,11 @@
 	char buf[100];
 	int ret;
 
+#ifdef ANDROID
+	kill_pid(dut, concat_sigma_tmpdir(dut, "/sigma_dut-dnsmasq.pid",
+					  buf, sizeof(buf)));
+#endif /* ANDROID */
+
 	if (dut->station_ifname_2g &&
 	    strcmp(dut->station_ifname_2g, intf) == 0)
 		dut->use_5g = 0;
diff --git a/utils.c b/utils.c
index c823f83..201dad5 100644
--- a/utils.c
+++ b/utils.c
@@ -10,6 +10,7 @@
 #include "sigma_dut.h"
 #include <sys/ioctl.h>
 #include <sys/stat.h>
+#include <signal.h>
 #include "wpa_helpers.h"
 
 enum driver_type wifi_chip_type = DRIVER_NOT_SET;
@@ -1068,3 +1069,32 @@
 {
 	return res < 0 || (unsigned int) res >= size;
 }
+
+
+void kill_pid(struct sigma_dut *dut, const char *pid_file)
+{
+	int pid;
+	FILE *f;
+
+	f = fopen(pid_file, "r");
+	if (!f)
+		return; /* process is not running */
+
+	if (fscanf(f, "%d", &pid) != 1 || pid <= 0) {
+		sigma_dut_print(dut, DUT_MSG_ERROR,
+				"No PID for process in %s", pid_file);
+		fclose(f);
+		unlink(pid_file);
+		return;
+	}
+	fclose(f);
+
+	sigma_dut_print(dut, DUT_MSG_DEBUG, "Process PID found in %s: %d",
+			pid_file, pid);
+	if (kill(pid, SIGINT) < 0 && errno != ESRCH)
+		sigma_dut_print(dut, DUT_MSG_DEBUG, "kill failed: %s",
+				strerror(errno));
+
+	unlink(pid_file);
+	sleep(1);
+}