Mirror of the lttng-dev mailing list
 help / color / mirror / Atom feed
From: nils.carlson@ericsson.com (Nils Carlson)
Subject: [ltt-dev] [UST PATCH 3/6] Restructure the ustctl_get_online_pids command v2
Date: Fri, 1 Apr 2011 09:32:17 +0200	[thread overview]
Message-ID: <1301643140-6880-3-git-send-email-nils.carlson@ericsson.com> (raw)
In-Reply-To: <1301643140-6880-1-git-send-email-nils.carlson@ericsson.com>

Changes since v1:
	* Use list length, not the size in bytes
	* Fix a possible bug for pid_t in a sscanf
	* Use list length in the dir scanning function

Restructure the command to separate the pid gathering.
This will allow a root user to gather pid from multiple user
directories.

Signed-off-by: Nils Carlson <nils.carlson at ericsson.com>
---
 libustctl/libustctl.c |   81 +++++++++++++++++++++++++++++++------------------
 1 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/libustctl/libustctl.c b/libustctl/libustctl.c
index d57e645..5625f43 100644
--- a/libustctl/libustctl.c
+++ b/libustctl/libustctl.c
@@ -88,53 +88,74 @@ int ustctl_connect_pid(pid_t pid)
 	return sock;
 }
 
-pid_t *ustctl_get_online_pids(void)
+static void get_pids_in_dir(DIR *dir, pid_t **pid_list,
+			    unsigned int *pid_list_size)
 {
 	struct dirent *dirent;
-	DIR *dir;
-	unsigned int ret_size = 1 * sizeof(pid_t), i = 0;
-
-	dir = opendir(SOCK_DIR);
-	if (!dir) {
-		return NULL;
-	}
-
-	pid_t *ret = (pid_t *) malloc(ret_size);
+	unsigned int read_pid;
 
 	while ((dirent = readdir(dir))) {
 		if (!strcmp(dirent->d_name, ".") ||
-		    !strcmp(dirent->d_name, "..")) {
+		    !strcmp(dirent->d_name, "..") ||
+		    !strcmp(dirent->d_name, "ust-consumer") ||
+		    dirent->d_type == DT_DIR) {
 
 			continue;
 		}
 
-		if (dirent->d_type != DT_DIR &&
-		    !!strcmp(dirent->d_name, "ust-consumer")) {
-
-			sscanf(dirent->d_name, "%u", (unsigned int *) &ret[i]);
-			/* FIXME: Here we previously called pid_is_online, which
-			 * always returned 1, now I replaced it with just 1.
-			 * We need to figure out an intelligent way of solving
-			 * this, maybe connect-disconnect.
-			 */
-			if (1) {
-				ret_size += sizeof(pid_t);
-				ret = (pid_t *) realloc(ret, ret_size);
-				++i;
-			}
+		sscanf(dirent->d_name, "%u", &read_pid);
+
+		(*pid_list)[*pid_list_size - 1] = read_pid;
+		/* FIXME: Here we previously called pid_is_online, which
+		 * always returned 1, now I replaced it with just 1.
+		 * We need to figure out an intelligent way of solving
+		 * this, maybe connect-disconnect.
+		 */
+		if (1) {
+			(*pid_list_size)++;
+			*pid_list = realloc(*pid_list,
+					    *pid_list_size * sizeof(pid_t));
 		}
 	}
 
-	ret[i] = 0; /* Array end */
+	(*pid_list)[*pid_list_size - 1] = 0; /* Array end */
+}
 
-	if (ret[0] == 0) {
-		/* No PID at all */
-		free(ret);
+pid_t *ustctl_get_online_pids(void)
+{
+	char *dir_name;
+	DIR *dir;
+	unsigned int pid_list_size = 1;
+	pid_t *pid_list = NULL;
+
+	dir_name = ustcomm_user_sock_dir();
+	if (!dir_name) {
 		return NULL;
 	}
 
+	dir = opendir(dir_name);
+	if (!dir) {
+		goto free_dir_name;
+	}
+
+	pid_list = malloc(pid_list_size * sizeof(pid_t));
+
+	get_pids_in_dir(dir, &pid_list, &pid_list_size);
+
+	if (pid_list[0] == 0) {
+		/* No PID at all */
+		free(pid_list);
+		pid_list = NULL;
+		goto close_dir;
+	}
+
+close_dir:
 	closedir(dir);
-	return ret;
+
+free_dir_name:
+	free(dir_name);
+
+	return pid_list;
 }
 
 /**
-- 
1.7.1




  parent reply	other threads:[~2011-04-01  7:32 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-01  7:32 [ltt-dev] [UST PATCH 1/6] Make app socket directories per-user v2 Nils Carlson
2011-04-01  7:14 ` Nils Carlson
2011-04-01  7:32 ` [ltt-dev] [UST PATCH 2/6] Add mode setting to socket directory creation Nils Carlson
2011-04-01  7:15   ` Nils Carlson
2011-04-01  7:32 ` Nils Carlson [this message]
2011-04-01  7:15   ` [ltt-dev] [UST PATCH 3/6] Restructure the ustctl_get_online_pids command v2 Nils Carlson
2011-04-01  7:32 ` [ltt-dev] [UST PATCH 4/6] Add list-pids command to ustctl Nils Carlson
2011-04-01  7:16   ` Nils Carlson
2011-04-01  7:32 ` [ltt-dev] [UST PATCH 5/6] Make root see all available pids Nils Carlson
2011-04-01  7:17   ` Nils Carlson
2011-04-01  7:32 ` [ltt-dev] [UST PATCH 6/6] Make root able to connect to any traceable app Nils Carlson
2011-04-01  7:17   ` Nils Carlson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1301643140-6880-3-git-send-email-nils.carlson@ericsson.com \
    --to=nils.carlson@ericsson.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox