[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] more devfsd clean up and size reduction
From:       Tito <farmatito () tiscali ! it>
Date:       2007-06-30 12:16:33
Message-ID: 200706301416.33468.farmatito () tiscali ! it
[Download RAW message or body]

Hi,
as nobody complained about my previous devfsd patch
(....i presume because nobody is using it ;-) )
here is one more round
of size reduction and code clean style clean up.
This patch removes the functions
msg_logger, msg_logger_and_die, fork_and_execute
and uses libbb stuff instead.

Bloat-o-meter says:

root@localhost:~/Desktop/busybox# scripts/bloat-o-meter busybox_old busybox_unstripped
function                                             old     new   delta
expand_variable                                      756     759      +3
get_variable                                         259     257      -2
get_uid_gid                                          122     120      -2
do_ioctl_and_die                                      27      25      -2
st_expr_expand                                       488     485      -3
signal_handler                                        94      91      -3
devfsd_main                                          842     833      -9
service_name                                        2444    2433     -11
read_config_file                                    1119    1098     -21
msg_logger_and_die                                    31       -     -31
.rodata                                           125143  125111     -32
msg_logger                                            88       -     -88
fork_and_execute                                      95       -     -95
------------------------------------------------------------------------------
(add/remove: 0/3 grow/shrink: 1/9 up/down: 3/-299)           Total: -296 bytes

BE WARNED THAT THIS IS ONLY COMPILE TESTED!!!

If there are some volunteers for testing it .....

Denis could you please take a look at my use
of spawn, xspawn, wait4pid and bb_daemonize_or_rexec
as I never used them before and i'm not quite sure 
if i've done things right.............
and apply the patch if you like it.

Ciao,
Tito

["devfsd03.patch" (text/x-diff)]

--- miscutils/devfsd.c.orig	2007-06-30 13:49:56.000000000 +0200
+++ miscutils/devfsd.c	2007-06-30 14:02:17.000000000 +0200
@@ -234,11 +234,8 @@
 static char *write_old_sd_name(char *, unsigned, unsigned, const char *);
 
 /* busybox functions */
-static void msg_logger(int pri, const char * fmt, ...)__attribute__((format(printf, \
                2, 3)));
-static void msg_logger_and_die(int pri, const char * fmt, \
...)__attribute__((noreturn, format(printf, 2, 3)));  static void \
                do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag);
-static void fork_and_execute(int die, char *arg0, char **arg);
-static int get_uid_gid(int, const char *);
+static int get_uid_gid(int flag, const char *string);
 static void safe_memcpy(char * dest, const char * src, int len);
 static unsigned int scan_dev_name_common(const char *d, unsigned int n, int \
addendum, const char *ptr);  static unsigned int scan_dev_name(const char *d, \
unsigned int n, const char *ptr); @@ -286,48 +283,19 @@
 static const char * const bb_msg_small_buffer		= "buffer too small";
 static const char * const bb_msg_variable_not_found = "variable: %s not found";
 
-/* Busybox functions  */
-static void msg_logger(int pri, const char * fmt, ...)
-{
-	va_list ap;
-	int ret;
-
-	va_start(ap, fmt);
-	ret = access("/dev/log", F_OK);
-	if (ret == 0) {
-		openlog(applet_name, 0, LOG_DAEMON);
-		vsyslog(pri , fmt, ap);
-		/* Man: A trailing newline is added when needed. */
-		closelog();
-	}
-	/* ENABLE_DEVFSD_VERBOSE is always enabled if msg_logger is used */
-	if ((ENABLE_DEVFSD_VERBOSE && ret) || ENABLE_DEBUG) {
-		bb_error_msg(fmt, ap);
-	}
-	va_end(ap);
-}
-
-static void msg_logger_and_die(int pri, const char* fmt, ...)
-{
-	va_list ap;
-
-	va_start(ap, fmt);
-	msg_logger(pri, fmt, ap);
-	va_end(ap);
-	exit(EXIT_FAILURE);
-}
-
 /* Busybox stuff */
-#if defined(CONFIG_DEVFSD_VERBOSE) || defined(CONFIG_DEBUG)
-#define devfsd_error_msg(fmt, args...)                bb_error_msg(fmt, ## args)
-#define devfsd_perror_msg_and_die(fmt, args...)       bb_perror_msg_and_die(fmt, ## \
                args)
-#define devfsd_error_msg_and_die(fmt, args...)        bb_error_msg_and_die(fmt, ## \
args) +#if ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG
+#define info_logger(p, fmt, args...)                 bb_info_msg(fmt, ## args)
+#define msg_logger(p, fmt, args...)                  bb_error_msg(fmt, ## args)
+#define msg_logger_and_die(p, fmt, args...)          bb_error_msg_and_die(fmt, ## \
args) +#define error_logger(p, fmt, args...)                bb_perror_msg(fmt, ## \
args) +#define error_logger_and_die(p, fmt, args...)        \
bb_perror_msg_and_die(fmt, ## args)  #else
+#define info_logger(p, fmt, args...)
 #define msg_logger(p, fmt, args...)
 #define msg_logger_and_die(p, fmt, args...)           exit(1)
-#define devfsd_perror_msg_and_die(fmt, args...)       exit(1)
-#define devfsd_error_msg_and_die(fmt, args...)        exit(1)
-#define devfsd_error_msg(fmt, args...)
+#define error_logger(p, fmt, args...)
+#define error_logger_and_die(p, fmt, args...)         exit(1)
 #endif
 
 static void do_ioctl_and_die(int fd, int request, unsigned long event_mask_flag)
@@ -336,33 +304,6 @@
 		msg_logger_and_die(LOG_ERR, "ioctl");
 }
 
-static void fork_and_execute(int die, char *arg0, char **arg)
-{
-	switch (fork()) {
-		case 0:
-			/*  Child  */
-			break;
-		case -1:
-			/*  Parent: Error  : die or return */
-			msg_logger(LOG_ERR,(char *)bb_msg_memory_exhausted);
-			if (die)
-				exit(EXIT_FAILURE);
-			return;
-		default:
-			/*  Parent : ok : return or exit  */
-			if (arg0 != NULL) {
-				wait(NULL);
-				return;
-			}
-			exit(EXIT_SUCCESS);
-	}
-	 /* Child : if arg0 != NULL do execvp */
-	if (arg0 != NULL) {
-		BB_EXECVP(arg0, arg);
-		msg_logger_and_die(LOG_ERR, "execvp");
-	}
-}
-
 static void safe_memcpy(char *dest, const char *src, int len)
 {
 	memcpy(dest , src, len);
@@ -447,10 +388,10 @@
 	fd = xopen(".devfsd", O_RDONLY);
 
 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0)
-		devfsd_perror_msg_and_die("FD_CLOEXEC");
+		bb_perror_msg_and_die("FD_CLOEXEC");
 
 	if (ioctl(fd, DEVFSDIOC_GET_PROTO_REV, &proto_rev) == -1)
-		msg_logger_and_die(LOG_ERR, "ioctl");
+		bb_perror_msg_and_die("ioctl");
 
 	/*setup initial entries */
     for (curr = initial_symlinks; curr->dest != NULL; ++curr)
@@ -475,7 +416,7 @@
 	/*  Set up SIGHUP and SIGUSR1 handlers  */
 	new_action.sa_handler = signal_handler;
 	if (sigaction(SIGHUP, &new_action, NULL) != 0 || sigaction(SIGUSR1, &new_action, \
                NULL) != 0)
-		devfsd_error_msg_and_die("sigaction");
+		bb_error_msg_and_die("sigaction");
 
 	printf("%s v%s  started for %s\n",applet_name, DEVFSD_VERSION, mount_point);
 
@@ -487,15 +428,23 @@
 
 	if (ENABLE_DEVFSD_FG_NP && no_polling)
 		exit(0);
+	
+	if (ENABLE_DEVFSD_VERBOSE || ENABLE_DEBUG)
+		logmode = LOGMODE_BOTH;
+	else if (do_daemon == TRUE)
+		logmode = LOGMODE_SYSLOG;
+	/* This is the default */
+	/*else
+		logmode = LOGMODE_STDIO; */
+
 	if (do_daemon) {
 		/*  Release so that the child can grab it  */
 		do_ioctl_and_die(fd, DEVFSDIOC_RELEASE_EVENT_QUEUE, 0);
-		fork_and_execute(DIE, NULL, NULL);
-		setsid();        /*  Prevent hangups and become pgrp leader         */
+		bb_daemonize_or_rexec(0, NULL);
 	} else if (ENABLE_DEVFSD_FG_NP) {
 		setpgid(0, 0);  /*  Become process group leader                    */
 	}
-
+	
 	while (TRUE) {
 		do_scan = do_servicing(fd, event_mask);
 
@@ -535,23 +484,20 @@
 			dir_operation(READ_CONFIG, path, 0, event_mask);
 			return;
 		}
-		if ((fp = fopen(path, "r")) != NULL) {
+		if ((fp = xfopen(path, "r")) != NULL) {
 			while (fgets(buf, STRING_LENGTH, fp) != NULL) {
 				/*  Skip whitespace  */
-				for (line = buf; isspace(*line); ++line)
-					/*VOID*/;
+				line = buf;
+				line = skip_whitespace(line);
 				if (line[0] == '\0' || line[0] == '#')
 					continue;
 				process_config_line(line, event_mask);
 			}
 			fclose(fp);
-		} else {
-			goto read_config_file_err;
 		}
 	} else {
-read_config_file_err:
-	if (optional ==  0  && errno == ENOENT)
-		msg_logger_and_die(LOG_ERR, "read config file: %s: %m", path);
+		if (optional ==  0  && errno == ENOENT)
+			error_logger_and_die(LOG_ERR, "read config file: %s", path);
 	}
 }   /*  End Function read_config_file   */
 
@@ -600,7 +546,7 @@
 	/* "INCLUDE" & "OPTIONAL_INCLUDE" */
 	if (i == 1 || i == 2) {
 		st_expr_expand(name, STRING_LENGTH, name, get_variable, NULL);
-		msg_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": "optional_", \
name); +		info_logger(LOG_INFO, "%sinclude: %s", (toupper(when[0]) == 'I') ? "": \
"optional_", name);  read_config_file(name, (toupper(when[0]) == 'I') ? FALSE : TRUE, \
event_mask);  return;
 	}
@@ -809,11 +755,11 @@
 {
 	struct stat statbuf;
 
-	if (stat(info->devname, &statbuf) != 0	||
-		 chmod(info->devname,(statbuf.st_mode & S_IFMT) |(entry->u.permissions.mode & \
                ~S_IFMT)) != 0 ||
-		chown(info->devname, entry->u.permissions.uid, entry->u.permissions.gid) != 0
+	if (stat(info->devname, &statbuf) != 0
+	 || chmod(info->devname,(statbuf.st_mode & S_IFMT) |(entry->u.permissions.mode & \
~S_IFMT)) != 0 +	 ||	chown(info->devname, entry->u.permissions.uid, \
entry->u.permissions.gid) != 0  )
-		msg_logger(LOG_ERR, "Can't chmod or chown: %s: %m", info->devname);
+		error_logger(LOG_ERR, "Can't chmod or chown: %s", info->devname);
 }   /*  End Function action_permissions  */
 
 static void action_modload(const struct devfsd_notify_struct *info,
@@ -826,14 +772,14 @@
 {
 	char *argv[6];
 
-	argv[0] =(char*)MODPROBE;
-	argv[1] =(char*)MODPROBE_SWITCH_1; /* "-k" */
-	argv[2] =(char*)MODPROBE_SWITCH_2; /* "-C" */
-	argv[3] =(char*)CONFIG_MODULES_DEVFS;
+	argv[0] = (char*)MODPROBE;
+	argv[1] = (char*)MODPROBE_SWITCH_1; /* "-k" */
+	argv[2] = (char*)MODPROBE_SWITCH_2; /* "-C" */
+	argv[3] = (char*)CONFIG_MODULES_DEVFS;
 	argv[4] = concat_path_file("/dev", info->devname); /* device */
 	argv[5] = NULL;
 
-	fork_and_execute(DIE, argv[0], argv);
+	wait4pid(xspawn(argv));
 	free(argv[4]);
 }  /*  End Function action_modload  */
 
@@ -865,7 +811,7 @@
 		argv[count] = largv[count];
 	}
 	argv[count] = NULL;
-	fork_and_execute(NO_DIE, argv[0], argv);
+	wait4pid(spawn(argv));
 }   /*  End Function action_execute  */
 
 
@@ -913,7 +859,7 @@
 		new_mode |= S_ISVTX;
 	ret = copy_inode(destination, &dest_stat, new_mode, source, &source_stat);
 	if (ENABLE_DEBUG && ret && (errno != EEXIST))
-		msg_logger(LOG_ERR, "copy_inode: %s to %s: %m", source, destination);
+		error_logger(LOG_ERR, "copy_inode: %s to %s", source, destination);
 }   /*  End Function action_copy  */
 
 static void action_compat(const struct devfsd_notify_struct *info, unsigned int \
action) @@ -1007,7 +953,7 @@
 		case AC_RMNEWCOMPAT:
 			ret = unlink(compat_name);
 			if (ENABLE_DEBUG && ret)
-				msg_logger(LOG_ERR, "unlink: %s: %m", compat_name);
+				error_logger(LOG_ERR, "unlink: %s", compat_name);
 			break;
 		/*esac*/
 	} /* switch (action) */
@@ -1022,7 +968,7 @@
 	dpath = concat_path_file(mount_point, spath + rootlen);
 	lstat(dpath, &dest_stat);
 	free(dpath);
-	if (S_ISLNK(source_stat.st_mode) ||(source_stat.st_mode & S_ISVTX))
+	if (S_ISLNK(source_stat.st_mode) || (source_stat.st_mode & S_ISVTX))
 		copy_inode(dpath, &dest_stat,(source_stat.st_mode & ~S_ISVTX) , spath, \
&source_stat);  
 	if (S_ISDIR(source_stat.st_mode))
@@ -1198,7 +1144,7 @@
 	if (sig == SIGHUP)
 		caught_sighup = TRUE;
 
-	msg_logger(LOG_INFO, "Caught signal %d", sig);
+	info_logger(LOG_INFO, "Caught signal %d", sig);
 }   /*  End Function signal_handler  */
 
 static const char *get_variable(const char *variable, void *info)
@@ -1211,7 +1157,7 @@
 	int i;
 
 	if (gethostname(hostname, STRING_LENGTH - 1) != 0)
-		msg_logger_and_die(LOG_ERR, "gethostname: %m");
+		error_logger_and_die(LOG_ERR, "gethostname");
 
 		/* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) \
*/  hostname[STRING_LENGTH - 1] = '\0';
@@ -1651,7 +1597,7 @@
 				if (isspace(ch) ||(ch == '/') ||(ch == '\0')) {
 					/* User's own home directory: leave separator for next time */
 					if ((env = getenv("HOME")) == NULL) {
-						msg_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
+						info_logger(LOG_INFO, bb_msg_variable_not_found, "HOME");
 						return FALSE;
 					}
 					len = strlen(env);
@@ -1670,7 +1616,7 @@
 				safe_memcpy(tmp, input, len);
 				input = ptr - 1;
 				if ((pwent = getpwnam(tmp)) == NULL) {
-					msg_logger(LOG_INFO, "no pwent for: %s", tmp);
+					info_logger(LOG_INFO, "no pwent for: %s", tmp);
 					return FALSE;
 				}
 				len = strlen(pwent->pw_dir);
@@ -1695,7 +1641,7 @@
 	}
 	return FALSE;
 st_expr_expand_out:
-	msg_logger(LOG_INFO, bb_msg_small_buffer);
+	info_logger(LOG_INFO, bb_msg_small_buffer);
 	return FALSE;
 }   /*  End Function st_expr_expand  */
 
@@ -1750,7 +1696,7 @@
 		safe_memcpy(tmp, input, len);
 		input = ptr - 1;
 		if ((env = get_variable_v2(tmp, func, info)) == NULL) {
-			msg_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
+			info_logger(LOG_INFO, bb_msg_variable_not_found, tmp);
 			return NULL;
 		}
 		len = strlen(env);
@@ -1778,7 +1724,7 @@
 		return input + len;
 	}
 	if (ch != ':' || ptr[1] != '-') {
-		msg_logger(LOG_INFO, "illegal char in var name");
+		info_logger(LOG_INFO, "illegal char in var name");
 		return NULL;
 	}
 	/*  It's that handy "${var:-word}" expression. Check if var is defined  */
@@ -1801,7 +1747,7 @@
 				--open_braces;
 				break;
 			case '\0':
-				msg_logger(LOG_INFO,"\"}\" not found in: %s", input);
+				info_logger(LOG_INFO,"\"}\" not found in: %s", input);
 				return NULL;
 			default:
 				break;
@@ -1840,7 +1786,7 @@
 	*out_pos += len;
 	return input;
 expand_variable_out:
-	msg_logger(LOG_INFO, bb_msg_small_buffer);
+	info_logger(LOG_INFO, bb_msg_small_buffer);
 	return NULL;
 }   /*  End Function expand_variable  */
 



_______________________________________________
busybox mailing list
busybox@busybox.net
http://busybox.net/cgi-bin/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic