[PATCH] dmesg: use strtol_or_err instead of atoi

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Davidlohr Bueso <dave@xxxxxxx>

We shouldn't be accepting things like 'dmesg -n 2crapinput'
This patch also changes the exit's value to use EXIT_* constants.

Signed-off-by: Davidlohr Bueso <dave@xxxxxxx>
---
 sys-utils/Makefile.am |    1 +
 sys-utils/dmesg.c     |   17 +++++++++--------
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am
index ee6f2d7..d916d3d 100644
--- a/sys-utils/Makefile.am
+++ b/sys-utils/Makefile.am
@@ -29,6 +29,7 @@ cytune_SOURCES = cytune.c cyclades.h
 tunelp_SOURCES = tunelp.c lp.h
 fstrim_SOURCES = fstrim.c $(top_srcdir)/lib/strutils.c
 rtcwake_SOURCES = rtcwake.c $(top_srcdir)/lib/strutils.c
+dmesg_SOURCES = dmesg.c $(top_srcdir)/lib/strutils.c
 
 if BUILD_FALLOCATE
 usrbin_exec_PROGRAMS += fallocate
diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c
index f1a7dcb..49fd707 100644
--- a/sys-utils/dmesg.c
+++ b/sys-utils/dmesg.c
@@ -33,9 +33,10 @@
 #include <stdio.h>
 #include <getopt.h>
 #include <stdlib.h>
-# include <sys/klog.h>
+#include <sys/klog.h>
 
 #include "nls.h"
+#include "strutils.h"
 
 static char *progname;
 
@@ -70,20 +71,20 @@ main(int argc, char *argv[]) {
 			break;
 		case 'n':
 			cmd = 8;	/* Set level of messages */
-			level = atoi(optarg);
+			level = strtol_or_err(optarg, _("failed to parse level"));
 			break;
 		case 'r':
 			raw = 1;
 			break;
 		case 's':
-			bufsize = atoi(optarg);
+			bufsize = strtol_or_err(optarg, _("failed to parse buffer size"));
 			if (bufsize < 4096)
 				bufsize = 4096;
 			break;
 		case '?':
 		default:
 			usage();
-			exit(1);
+			exit(EXIT_FAILURE);
 		}
 	}
 	argc -= optind;
@@ -91,16 +92,16 @@ main(int argc, char *argv[]) {
 
 	if (argc > 1) {
 		usage();
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 
 	if (cmd == 8) {
 		n = klogctl(cmd, NULL, level);
 		if (n < 0) {
 			perror("klogctl");
-			exit(1);
+			exit(EXIT_FAILURE);
 		}
-		exit(0);
+		exit(EXIT_SUCCESS);
 	}
 
 	if (!bufsize) {
@@ -130,7 +131,7 @@ main(int argc, char *argv[]) {
 
 	if (n < 0) {
 		perror("klogctl");
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 
 	lastc = '\n';
-- 
1.7.1



--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux