[lldp-devel] [PATCH 2/3] liblldp_clif add funcion to return PID of lldpad

Thomas Richter tmricht at linux.vnet.ibm.com
Thu Feb 21 15:38:43 UTC 2013


Offer a function clif_getpid() to return the pid of
lldpad. Export this function in the liblldp_clif.{a,so}
library for other programs to use it.

Signed-off-by: Thomas Richter <tmricht at linux.vnet.ibm.com>
---
 clif.c         |   45 +++++++++++++++++++++++++++++++++++++++++++--
 include/clif.h |    9 +++++++++
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/clif.c b/clif.c
index 120f0ec..304c63c 100644
--- a/clif.c
+++ b/clif.c
@@ -66,6 +66,7 @@ struct clif  *clif_open()
 		    "%s/%d", LLDP_CLIF_SOCK, getpid());
 	addrlen = sizeof(sa_family_t) + strlen(clif->local.sun_path + 1) + 1;
 	if (bind(clif->s, (struct sockaddr *) &clif->local, addrlen) < 0) {
+		perror("bind");
 		close(clif->s);
 		free(clif);
 		return NULL;
@@ -78,6 +79,7 @@ struct clif  *clif_open()
 	addrlen = sizeof(sa_family_t) + strlen(clif->dest.sun_path + 1) + 1;
 
 	if (connect(clif->s, (struct sockaddr *) &clif->dest, addrlen) < 0) {
+		perror("connect");
 		close(clif->s);
 		free(clif);
 		return NULL;
@@ -231,6 +233,45 @@ int clif_get_fd(struct clif *clif)
 	return clif->s;
 }
 
+/*
+ * Get PID of lldpad from 'ping' command
+ */
+pid_t clif_getpid(void)
+{
+	struct clif *clif_conn;
+	char buf[MAX_CLIF_MSGBUF];
+	size_t len;
+	char *ppong;
+	int ret;
+	pid_t lldpad = 0;		/* LLDPAD process identifier */
 
-
-
+	clif_conn = clif_open();
+	if (!clif_conn) {
+		fprintf(stderr, "couldn't connect to lldpad\n");
+		return 0;
+	}
+	if (clif_attach(clif_conn, NULL)) {
+		fprintf(stderr, "failed to attach to lldpad\n");
+		clif_close(clif_conn);
+		return 0;
+	}
+	ret = clif_request(clif_conn, "P", 1, buf, &len, NULL);
+	if (ret == -2) {
+		fprintf(stderr, "connection to lldpad timed out\n");
+		goto out;
+	}
+	if (ret < 0) {
+		fprintf(stderr, "ping command failed\n");
+		goto out;
+	}
+	buf[len] = '\0';
+	ppong = strstr(buf, "PPONG");		/* Ignore leading chars */
+	if (!ppong || sscanf(ppong, "PPONG%d", &lldpad) != 1) {
+		fprintf(stderr, "error parsing pid of lldpad\n");
+		lldpad = 0;
+	}
+out:
+	clif_detach(clif_conn);
+	clif_close(clif_conn);
+	return lldpad;
+}
diff --git a/include/clif.h b/include/clif.h
index 56a14ec..fa19149 100644
--- a/include/clif.h
+++ b/include/clif.h
@@ -171,4 +171,13 @@ int clif_pending(struct clif *clif);
  */
 int clif_get_fd(struct clif *clif);
 
+/**
+ * clif_getpid - Get PID of running lldpad process
+ * Returns: The PID of lldpad or 0 on failure
+ *
+ * This function is returns the PID of the running lldpad. It connects to the
+ * lldpad and sends a PING command. Lldpad returns with a PONG followed by
+ * its PID. Extract the PID and return it to the caller.
+ */
+pid_t clif_getpid(void);
 #endif /* CLIF_H */
-- 
1.7.1



More information about the lldp-devel mailing list