public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 6/7] kill, procs: use pid_t for pids
Date: Thu,  7 Mar 2013 23:27:23 +0000	[thread overview]
Message-ID: <1362698844-21292-7-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1362698844-21292-1-git-send-email-kerolasa@iki.fi>

Reference: http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/include/linux/threads.h#n30
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 misc-utils/kill.c  |  6 +++---
 misc-utils/kill.h  |  2 +-
 misc-utils/procs.c | 17 ++++++++---------
 3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/misc-utils/kill.c b/misc-utils/kill.c
index b0145cd..742f349 100644
--- a/misc-utils/kill.c
+++ b/misc-utils/kill.c
@@ -156,7 +156,7 @@ int main(int argc, char **argv)
 	int errors, numsig, pid;
 	char *ep, *arg;
 	int do_pid, do_kill, check_all;
-	int *pids, *ip;
+	pid_t *pids, *ip;
 
 	setlocale(LC_ALL, "");
 	bindtextdomain(PACKAGE, LOCALEDIR);
@@ -414,12 +414,12 @@ static int usage(int status)
 	return status;
 }
 
-static int kill_verbose(char *procname, int pid, int sig)
+static int kill_verbose(char *procname, pid_t pid, int sig)
 {
 	int rc;
 
 	if (sig < 0) {
-		printf("%d\n", pid);
+		printf("%ld\n", (long)pid);
 		return 0;
 	}
 #ifdef HAVE_SIGQUEUE
diff --git a/misc-utils/kill.h b/misc-utils/kill.h
index 43c6ed7..fb02994 100644
--- a/misc-utils/kill.h
+++ b/misc-utils/kill.h
@@ -1,7 +1,7 @@
 #ifndef UTIL_LINUX_KILL
 #define UTIL_LINUX_KILL
 
-extern int *get_pids(char *process_name, int get_all);
+extern pid_t *get_pids(char *process_name, int get_all);
 extern char *mybasename(char *);
 
 #endif
diff --git a/misc-utils/procs.c b/misc-utils/procs.c
index 544966e..c4cb06e 100644
--- a/misc-utils/procs.c
+++ b/misc-utils/procs.c
@@ -25,16 +25,16 @@
 
 static char *parse_parens(char *buf);
 
-int *get_pids(char *process_name, int get_all)
+pid_t *get_pids(char *process_name, int get_all)
 {
 	DIR *dir;
 	struct dirent *ent;
 	int status;
-	char *dname, fname[100], *cp, buf[256];
+	char fname[100], *cp, buf[256], *end;
 	struct stat st;
 	uid_t uid;
 	FILE *fp;
-	int pid, *pids, num_pids, pids_size;
+	pid_t pid, *pids, num_pids, pids_size;
 
 	dir = opendir("/proc");
 	if (!dir) {
@@ -46,11 +46,10 @@ int *get_pids(char *process_name, int get_all)
 	num_pids = pids_size = 0;
 
 	while ((ent = readdir(dir)) != NULL) {
-		dname = ent->d_name;
-		if (!isdigit(*dname))
+		pid = strtol(ent->d_name, &end, 10);
+		if (errno || ent->d_name == end || (end && *end))
 			continue;
-		pid = atoi(dname);
-		sprintf(fname, "/proc/%d/cmdline", pid);
+		sprintf(fname, "/proc/%ld/cmdline", (long)pid);
 		/* get the process owner */
 		status = stat(fname, &st);
 		if (status != 0)
@@ -66,7 +65,7 @@ int *get_pids(char *process_name, int get_all)
 		/* an empty command line means the process is swapped out */
 		if (!cp || !*cp) {
 			/* get the process name from the statfile */
-			sprintf(fname, "/proc/%d/stat", pid);
+			sprintf(fname, "/proc/%ld/stat", (long)pid);
 			fp = fopen(fname, "r");
 			if (!fp)
 				continue;
@@ -83,7 +82,7 @@ int *get_pids(char *process_name, int get_all)
 			continue;
 		while (pids_size < num_pids + 2) {
 			pids_size += 5;
-			pids = (int *)xrealloc(pids, sizeof(int) * pids_size);
+			pids = xrealloc(pids, sizeof(pid_t) * pids_size);
 		}
 		if (pids) {
 			pids[num_pids++] = pid;
-- 
1.8.1.5


  parent reply	other threads:[~2013-03-07 23:27 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-07 23:27 [PATCH 0/7] kill: modernization after undeprecation Sami Kerola
2013-03-07 23:27 ` [PATCH 1/7] kill: add long options Sami Kerola
2013-03-07 23:27 ` [PATCH 2/7] kill: align with howto-usage-function.txt Sami Kerola
2013-03-07 23:27 ` [PATCH 3/7] kill: use libc error printing facilities and exit values Sami Kerola
2013-03-07 23:27 ` [PATCH 4/7] kill, procs: fix coding style Sami Kerola
2013-03-07 23:27 ` [PATCH 5/7] kill, procs: move function declarations to kill.h Sami Kerola
2013-03-07 23:27 ` Sami Kerola [this message]
2013-03-07 23:27 ` [PATCH 7/7] docs: add long options and tidy up kill.1 Sami Kerola
2013-03-12 14:29 ` [PATCH 0/7] kill: modernization after undeprecation Karel Zak

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=1362698844-21292-7-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /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