From vda at busybox.net Wed Nov 1 01:13:27 2006 From: vda at busybox.net (vda at busybox.net) Date: Wed, 1 Nov 2006 01:13:27 -0800 (PST) Subject: svn commit: trunk/busybox: libbb shell Message-ID: <20061101091327.14A1D485CE@busybox.net> Author: vda Date: 2006-11-01 01:13:26 -0800 (Wed, 01 Nov 2006) New Revision: 16483 Log: #if CONFIG_xxx -> #if ENABLE_xxx Modified: trunk/busybox/libbb/md5.c trunk/busybox/shell/cmdedit.c trunk/busybox/shell/cmdedit.h Changeset: Modified: trunk/busybox/libbb/md5.c =================================================================== --- trunk/busybox/libbb/md5.c 2006-10-31 23:39:37 UTC (rev 16482) +++ trunk/busybox/libbb/md5.c 2006-11-01 09:13:26 UTC (rev 16483) @@ -13,21 +13,13 @@ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ -#include -#include -#include -#include -#include -#include -#include - #include "libbb.h" -# if CONFIG_MD5_SIZE_VS_SPEED < 0 || CONFIG_MD5_SIZE_VS_SPEED > 3 +#if CONFIG_MD5_SIZE_VS_SPEED < 0 || CONFIG_MD5_SIZE_VS_SPEED > 3 # define MD5_SIZE_VS_SPEED 2 -# else +#else # define MD5_SIZE_VS_SPEED CONFIG_MD5_SIZE_VS_SPEED -# endif +#endif /* Initialize structure containing state of computation. * (RFC 1321, 3.3: Step 3) Modified: trunk/busybox/shell/cmdedit.c =================================================================== --- trunk/busybox/shell/cmdedit.c 2006-10-31 23:39:37 UTC (rev 16482) +++ trunk/busybox/shell/cmdedit.c 2006-11-01 09:13:26 UTC (rev 16483) @@ -1565,7 +1565,7 @@ while (cursor > 0 &&!isspace(command[cursor-1])) input_backspace(); break; -#if CONFIG_FEATURE_COMMAND_EDITING_VI +#if ENABLE_FEATURE_COMMAND_EDITING_VI case 'i'|vbit: vi_cmdmode = 0; break; @@ -1697,7 +1697,7 @@ case ESC: -#if CONFIG_FEATURE_COMMAND_EDITING_VI +#if ENABLE_FEATURE_COMMAND_EDITING_VI if (vi_mode) { /* ESC: insert mode --> command mode */ vi_cmdmode = 1; @@ -1749,7 +1749,7 @@ /* change command */ len = strlen(strcpy(command, history[cur_history])); /* redraw and go to eol (bol, in vi */ -#if CONFIG_FEATURE_COMMAND_EDITING_VI +#if ENABLE_FEATURE_COMMAND_EDITING_VI redraw(cmdedit_y, vi_mode ? 9999:0); #else redraw(cmdedit_y, 0); @@ -1797,7 +1797,7 @@ } else #endif { -#if CONFIG_FEATURE_COMMAND_EDITING_VI +#if ENABLE_FEATURE_COMMAND_EDITING_VI if (vi_cmdmode) /* don't self-insert */ break; #endif Modified: trunk/busybox/shell/cmdedit.h =================================================================== --- trunk/busybox/shell/cmdedit.h 2006-10-31 23:39:37 UTC (rev 16482) +++ trunk/busybox/shell/cmdedit.h 2006-11-01 09:13:26 UTC (rev 16483) @@ -2,19 +2,19 @@ #ifndef CMDEDIT_H #define CMDEDIT_H -int cmdedit_read_input(char* promptStr, char* command); +int cmdedit_read_input(char* promptStr, char* command); #ifdef CONFIG_ASH extern const char *cmdedit_path_lookup; #endif #ifdef CONFIG_FEATURE_COMMAND_SAVEHISTORY -void load_history ( const char *fromfile ); -void save_history ( const char *tofile ); +void load_history(const char *fromfile); +void save_history(const char *tofile); #endif -#if CONFIG_FEATURE_COMMAND_EDITING_VI -void setvimode ( int viflag ); +#if ENABLE_FEATURE_COMMAND_EDITING_VI +void setvimode(int viflag); #endif #endif /* CMDEDIT_H */ From vda at busybox.net Wed Nov 1 01:14:52 2006 From: vda at busybox.net (vda at busybox.net) Date: Wed, 1 Nov 2006 01:14:52 -0800 (PST) Subject: svn commit: trunk/busybox/procps Message-ID: <20061101091452.CD259485DB@busybox.net> Author: vda Date: 2006-11-01 01:14:52 -0800 (Wed, 01 Nov 2006) New Revision: 16484 Log: ps: reduce #ifdef forest Modified: trunk/busybox/procps/ps.c Changeset: Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-01 09:13:26 UTC (rev 16483) +++ trunk/busybox/procps/ps.c 2006-11-01 09:14:52 UTC (rev 16484) @@ -13,31 +13,19 @@ { procps_status_t * p; int i, len; - -#if ENABLE_SELINUX - int use_selinux = 0; - security_context_t sid = NULL; -#endif - -#if ENABLE_FEATURE_PS_WIDE + SKIP_SELINUX(const) int use_selinux = 0; + USE_SELINUX(security_context_t sid = NULL;) +#if !ENABLE_FEATURE_PS_WIDE + enum { terminal_width = 79 }; +#else int terminal_width; int w_count = 0; - - opt_complementary = "-:ww"; -#else -# define terminal_width 79 #endif #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX - /* handle arguments */ -#if ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX - i = getopt32(argc, argv, "wc", &w_count); -#elif ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX - getopt32(argc, argv, "w", &w_count); -#else /* !ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX */ - i = getopt32(argc, argv, "c"); -#endif #if ENABLE_FEATURE_PS_WIDE + opt_complementary = "-:ww"; + USE_SELINUX(i =) getopt32(argc, argv, "w" USE_SELINUX("c"), &w_count); /* if w is given once, GNU ps sets the width to 132, * if w is given more than once, it is "unlimited" */ @@ -48,18 +36,18 @@ /* Go one less... */ terminal_width--; } +#else /* only ENABLE_SELINUX */ + i = getopt32(argc, argv, "c"); #endif #if ENABLE_SELINUX if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled()) use_selinux = 1; #endif -#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ +#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ -#if ENABLE_SELINUX if (use_selinux) puts(" PID Context Stat Command"); else -#endif puts(" PID Uid VmSize Stat Command"); while ((p = procps_scan(1)) != 0) { @@ -75,7 +63,7 @@ } if (sid) { - /* I assume sid initilized with NULL */ + /* I assume sid initialized with NULL */ len = strlen(sid)+1; safe_strncpy(sbuf, sid, len); freecon(sid); @@ -99,7 +87,7 @@ i = 0; if (strlen(namecmd) > (size_t)i) namecmd[i] = 0; - printf("%s\n", namecmd); + puts(namecmd); } else { namecmd = p->short_cmd; if (i < 2) @@ -108,7 +96,7 @@ namecmd[i-2] = 0; printf("[%s]\n", namecmd); } - /* no check needed, but to make valgrind happy.. */ + /* no check needed, but to make valgrind happy.. */ if (ENABLE_FEATURE_CLEAN_UP && p->cmd) free(p->cmd); } From vda at busybox.net Wed Nov 1 01:16:50 2006 From: vda at busybox.net (vda at busybox.net) Date: Wed, 1 Nov 2006 01:16:50 -0800 (PST) Subject: svn commit: trunk/busybox: include init libbb procps Message-ID: <20061101091650.DA199485CE@busybox.net> Author: vda Date: 2006-11-01 01:16:49 -0800 (Wed, 01 Nov 2006) New Revision: 16485 Log: PID should be stored in pid_t, not int or long. find_pid_by_name() was returning 0 or -1 in last array element, but -1 was never checked. We can use just 0 intead. Modified: trunk/busybox/include/libbb.h trunk/busybox/init/halt.c trunk/busybox/libbb/find_pid_by_name.c trunk/busybox/libbb/procps.c trunk/busybox/procps/kill.c trunk/busybox/procps/pidof.c trunk/busybox/procps/ps.c trunk/busybox/procps/top.c Changeset: Modified: trunk/busybox/include/libbb.h =================================================================== --- trunk/busybox/include/libbb.h 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/include/libbb.h 2006-11-01 09:16:49 UTC (rev 16485) @@ -501,11 +501,10 @@ #endif #endif typedef struct { - int pid; + pid_t pid, ppid; char user[9]; char state[4]; unsigned long rss; - int ppid; #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE unsigned pcpu; unsigned pscpu; @@ -518,8 +517,8 @@ char short_cmd[COMM_LEN]; } procps_status_t; procps_status_t* procps_scan(int save_user_arg0); -long *find_pid_by_name( const char* pidName); -long *pidlist_reverse(long *pidList); +pid_t *find_pid_by_name(const char* procName); +pid_t *pidlist_reverse(pid_t *pidList); extern const char bb_uuenc_tbl_base64[]; Modified: trunk/busybox/init/halt.c =================================================================== --- trunk/busybox/init/halt.c 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/init/halt.c 2006-11-01 09:16:49 UTC (rev 16485) @@ -25,29 +25,34 @@ #endif RB_AUTOBOOT }; - static const int signals[] = {SIGUSR1, SIGUSR2, SIGTERM}; + static const int signals[] = { SIGUSR1, SIGUSR2, SIGTERM }; - char *delay = "hpr"; + char *delay; int which, flags, rc = 1; /* Figure out which applet we're running */ - for(which=0;delay[which]!=*applet_name;which++); + for (which = 0; "hpr"[which] != *applet_name; which++); /* Parse and handle arguments */ flags = getopt32(argc, argv, "d:nf", &delay); - if (flags&1) sleep(xatou(delay)); - if (!(flags&2)) sync(); + if (flags & 1) sleep(xatou(delay)); + if (!(flags & 2)) sync(); /* Perform action. */ if (ENABLE_INIT && !(flags & 4)) { if (ENABLE_FEATURE_INITRD) { - long *pidlist=find_pid_by_name("linuxrc"); - if (*pidlist>0) rc = kill(*pidlist,signals[which]); - if (ENABLE_FEATURE_CLEAN_UP) free(pidlist); + pid_t *pidlist = find_pid_by_name("linuxrc"); + if (pidlist[0] > 0) + rc = kill(pidlist[0], signals[which]); + if (ENABLE_FEATURE_CLEAN_UP) + free(pidlist); } - if (rc) rc = kill(1,signals[which]); - } else rc = reboot(magic[which]); + if (rc) + rc = kill(1, signals[which]); + } else + rc = reboot(magic[which]); - if (rc) bb_error_msg("no"); + if (rc) + bb_error_msg("no"); return rc; } Modified: trunk/busybox/libbb/find_pid_by_name.c =================================================================== --- trunk/busybox/libbb/find_pid_by_name.c 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/libbb/find_pid_by_name.c 2006-11-01 09:16:49 UTC (rev 16485) @@ -7,10 +7,6 @@ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */ -#include -#include -#include -#include #include "libbb.h" /* find_pid_by_name() @@ -23,30 +19,31 @@ * Returns a list of all matching PIDs * It is the caller's duty to free the returned pidlist. */ -long* find_pid_by_name(const char* pidName) +pid_t* find_pid_by_name(const char* procName) { - long* pidList; + pid_t* pidList; int i = 0; procps_status_t* p; - pidList = xmalloc(sizeof(long)); + pidList = xmalloc(sizeof(*pidList)); while ((p = procps_scan(0)) != 0) { - if (strncmp(p->short_cmd, pidName, COMM_LEN-1) == 0) { - pidList = xrealloc( pidList, sizeof(long) * (i+2)); + if (strncmp(p->short_cmd, procName, COMM_LEN-1) == 0) { + pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); pidList[i++] = p->pid; } } - pidList[i] = i==0 ? -1 : 0; + pidList[i] = 0; return pidList; } -long *pidlist_reverse(long *pidList) +pid_t *pidlist_reverse(pid_t *pidList) { int i = 0; - while (pidList[i] > 0 && ++i); - if (i-- > 0) { - long k; + while (pidList[i]) + i++; + if (--i >= 0) { + pid_t k; int j; for (j = 0; i > j; i--, j++) { k = pidList[i]; Modified: trunk/busybox/libbb/procps.c =================================================================== --- trunk/busybox/libbb/procps.c 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/libbb/procps.c 2006-11-01 09:16:49 UTC (rev 16485) @@ -32,8 +32,9 @@ procps_status_t * procps_scan(int save_user_arg0) { static DIR *dir; + static procps_status_t ret_status; + struct dirent *entry; - static procps_status_t ret_status; char *name; int n; char status[32]; Modified: trunk/busybox/procps/kill.c =================================================================== --- trunk/busybox/procps/kill.c 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/procps/kill.c 2006-11-01 09:16:49 UTC (rev 16485) @@ -103,31 +103,31 @@ } /* Pid or name required for kill/killall */ - if (argc<1) + if (argc < 1) bb_show_usage(); if (killall) { /* Looks like they want to do a killall. Do that */ pid = getpid(); while (arg) { - long* pidList; + pid_t* pidList; pidList = find_pid_by_name(arg); - if (!pidList || *pidList<=0) { + if (*pidList == 0) { errors++; if (!quiet) bb_error_msg("%s: no process killed", arg); } else { - long *pl; + pid_t *pl; - for (pl = pidList; *pl!=0; pl++) { - if (*pl==pid) + for (pl = pidList; *pl; pl++) { + if (*pl == pid) continue; - if (kill(*pl, signo)!=0) { - errors++; - if (!quiet) - bb_perror_msg("cannot kill pid %ld", *pl); - } + if (kill(*pl, signo) == 0) + continue; + errors++; + if (!quiet) + bb_perror_msg("cannot kill pid %u", (unsigned)*pl); } } free(pidList); @@ -138,12 +138,14 @@ /* Looks like they want to do a kill. Do that */ while (arg) { - if (!isdigit(arg[0]) && arg[0]!='-') + /* Huh? + if (!isdigit(arg[0]) && arg[0] != '-') bb_error_msg_and_die("bad pid '%s'", arg); + */ pid = xatou(arg); /* FIXME: better overflow check? */ - if (kill(pid, signo)!=0) { - bb_perror_msg("cannot kill pid %ld", (long)pid); + if (kill(pid, signo) != 0) { + bb_perror_msg("cannot kill pid %u", (unsigned)pid); errors++; } arg = *++argv; Modified: trunk/busybox/procps/pidof.c =================================================================== --- trunk/busybox/procps/pidof.c 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/procps/pidof.c 2006-11-01 09:16:49 UTC (rev 16485) @@ -18,19 +18,19 @@ #endif #if ENABLE_FEATURE_PIDOF_OMIT -#define _OMIT_COMPL(a) a -#define _OMIT(a) ,a -#if ENABLE_FEATURE_PIDOF_SINGLE -#define OMIT (1<<1) +# define _OMIT_COMPL(a) a +# define _OMIT(a) ,a +# if ENABLE_FEATURE_PIDOF_SINGLE +# define OMIT (1<<1) +# else +# define OMIT (1<<0) +# endif #else -#define OMIT (1<<0) +# define _OMIT_COMPL(a) "" +# define _OMIT(a) +# define OMIT (0) +# define omitted (0) #endif -#else -#define _OMIT_COMPL(a) "" -#define _OMIT(a) -#define OMIT (0) -#define omitted (0) -#endif int pidof_main(int argc, char **argv) { @@ -65,21 +65,23 @@ #endif /* Looks like everything is set to go. */ while (optind < argc) { - long *pidList; - long *pl; + pid_t *pidList; + pid_t *pl; /* reverse the pidlist like GNU pidof does. */ pidList = pidlist_reverse(find_pid_by_name(argv[optind])); - for (pl = pidList; *pl > 0; pl++) { + for (pl = pidList; *pl; pl++) { #if ENABLE_FEATURE_PIDOF_OMIT unsigned omitted = 0; if (opt & OMIT) { llist_t *omits_p = omits; - while (omits_p) + while (omits_p) { if (xatoul(omits_p->data) == *pl) { - omitted = 1; break; + omitted = 1; + break; } else omits_p = omits_p->link; + } } #endif if (!omitted) { @@ -88,7 +90,7 @@ } else { n = 1; } - printf("%ld", *pl); + printf("%u", (unsigned)*pl); } fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/procps/ps.c 2006-11-01 09:16:49 UTC (rev 16485) @@ -58,7 +58,7 @@ len = sizeof(sbuf); if (is_selinux_enabled()) { - if (getpidcon(p->pid,&sid) < 0) + if (getpidcon(p->pid, &sid) < 0) sid = NULL; } @@ -71,14 +71,14 @@ } else { safe_strncpy(sbuf, "unknown", 7); } - len = printf("%5d %-32s %s ", p->pid, sbuf, p->state); + len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state); } else #endif if (p->rss == 0) - len = printf("%5d %-8s %s ", p->pid, p->user, p->state); + len = printf("%5u %-8s %s ", (unsigned)p->pid, p->user, p->state); else - len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); + len = printf("%5u %-8s %6ld %s ", (unsigned)p->pid, p->user, p->rss, p->state); i = terminal_width-len; Modified: trunk/busybox/procps/top.c =================================================================== --- trunk/busybox/procps/top.c 2006-11-01 09:14:52 UTC (rev 16484) +++ trunk/busybox/procps/top.c 2006-11-01 09:16:49 UTC (rev 16485) @@ -78,7 +78,7 @@ the next. Mostly used for sorting. */ struct save_hist { int ticks; - int pid; + pid_t pid; }; /* @@ -119,7 +119,8 @@ static void do_stats(void) { procps_status_t *cur; - int pid, total_time, i, last_i, n; + pid_t pid; + int total_time, i, last_i, n; struct save_hist *new_hist; get_jiffy_counts(); @@ -328,9 +329,9 @@ else sprintf(rss_str_buf, "%7ld", s->rss); USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);) - col -= printf("\n%5d %-8s %s %s%6d%3u.%c" \ + col -= printf("\n%5u %-8s %s %s%6u%3u.%c" \ USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ", - s->pid, s->user, s->state, rss_str_buf, s->ppid, + (unsigned)s->pid, s->user, s->state, rss_str_buf, (unsigned)s->ppid, USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,) pmem.quot, '0'+pmem.rem); if (col > 0) From vda at busybox.net Wed Nov 1 01:17:48 2006 From: vda at busybox.net (vda at busybox.net) Date: Wed, 1 Nov 2006 01:17:48 -0800 (PST) Subject: svn commit: trunk/busybox/procps Message-ID: <20061101091748.12366485F2@busybox.net> Author: vda Date: 2006-11-01 01:17:47 -0800 (Wed, 01 Nov 2006) New Revision: 16486 Log: pidof: reduce #ifdef forest Modified: trunk/busybox/procps/pidof.c Changeset: Modified: trunk/busybox/procps/pidof.c =================================================================== --- trunk/busybox/procps/pidof.c 2006-11-01 09:16:49 UTC (rev 16485) +++ trunk/busybox/procps/pidof.c 2006-11-01 09:17:47 UTC (rev 16486) @@ -9,54 +9,38 @@ #include "busybox.h" -#if ENABLE_FEATURE_PIDOF_SINGLE -#define _SINGLE_COMPL(a) a -#define SINGLE (1<<0) -#else -#define _SINGLE_COMPL(a) -#define SINGLE 0 -#endif +enum { + USE_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,) + USE_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,) + OPT_SINGLE = USE_FEATURE_PIDOF_SINGLE((1<data, "%PPID", 5)) { llist_pop(&omits_p); - snprintf(getppid_str, sizeof(getppid_str), "%ld", (long)getppid()); + snprintf(getppid_str, sizeof(getppid_str), "%u", (unsigned)getppid()); llist_add_to(&omits_p, getppid_str); } omits_p = omits_p->link; @@ -71,9 +55,9 @@ /* reverse the pidlist like GNU pidof does. */ pidList = pidlist_reverse(find_pid_by_name(argv[optind])); for (pl = pidList; *pl; pl++) { + SKIP_FEATURE_PIDOF_OMIT(const) unsigned omitted = 0; #if ENABLE_FEATURE_PIDOF_OMIT - unsigned omitted = 0; - if (opt & OMIT) { + if (opt & OPT_OMIT) { llist_t *omits_p = omits; while (omits_p) { if (xatoul(omits_p->data) == *pl) { @@ -85,16 +69,12 @@ } #endif if (!omitted) { - if (n) { - putchar(' '); - } else { - n = 1; - } - printf("%u", (unsigned)*pl); + printf(" %u" + first, (unsigned)*pl); + first = 0; } fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted); - if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE)) + if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE)) break; } free(pidList); From bugs at busybox.net Wed Nov 1 01:42:35 2006 From: bugs at busybox.net (bugs at busybox.net) Date: Wed, 1 Nov 2006 01:42:35 -0800 Subject: [BusyBox 0001079]: telnetd quits on SIGPIPE Message-ID: <8471a8f52a910c553a9b106e9a439a85@busybox.net> The following issue has been SUBMITTED. ====================================================================== http://busybox.net/bugs/view.php?id=1079 ====================================================================== Reported By: jmunsin Assigned To: BusyBox ====================================================================== Project: BusyBox Issue ID: 1079 Category: Networking Support Reproducibility: random Severity: crash Priority: normal Status: assigned ====================================================================== Date Submitted: 11-01-2006 01:42 PST Last Modified: 11-01-2006 01:42 PST ====================================================================== Summary: telnetd quits on SIGPIPE Description: The line w = write(ts->sockfd, ts->buf2 + ts->wridx2, maxlen); in telnetd.c may cause a SIGPIPE. The default handler for SIGPIPE is to terminate => telnetd quits. This is wrong, at least if you are using telnetd in stand alone mode. Reproducing this is a bit tricky (timing/load dependant?), but my scripts can reproduce it after connecting 200-300 times. A quick fix is to run signal(SIGPIPE, SIG_IGN);, but perhaps there is a better way to handle this. ====================================================================== Issue History Date Modified Username Field Change ====================================================================== 11-01-06 01:42 jmunsin New Issue 11-01-06 01:42 jmunsin Status new => assigned 11-01-06 01:42 jmunsin Assigned To => BusyBox ====================================================================== From vda at busybox.net Wed Nov 1 02:25:36 2006 From: vda at busybox.net (vda at busybox.net) Date: Wed, 1 Nov 2006 02:25:36 -0800 (PST) Subject: svn commit: trunk/busybox: applets coreutils e2fsprogs e2fsprogs/e2 etc... Message-ID: <20061101102536.92D7748615@busybox.net> Author: vda Date: 2006-11-01 02:25:35 -0800 (Wed, 01 Nov 2006) New Revision: 16487 Log: mostly style fixes Modified: trunk/busybox/applets/applets.c trunk/busybox/coreutils/who.c trunk/busybox/e2fsprogs/e2fsck.c trunk/busybox/e2fsprogs/e2p/ps.c trunk/busybox/findutils/find.c trunk/busybox/loginutils/adduser.c trunk/busybox/miscutils/mt.c trunk/busybox/networking/libiproute/iproute.c trunk/busybox/procps/ps.c trunk/busybox/util-linux/hwclock.c trunk/busybox/util-linux/readprofile.c trunk/busybox/util-linux/switch_root.c Changeset: Modified: trunk/busybox/applets/applets.c =================================================================== --- trunk/busybox/applets/applets.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/applets/applets.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -444,7 +444,7 @@ format_string = "%s\n\nUsage: %s %s\n\n"; if (*usage_string == '\b') format_string = "%s\n\nNo help available.\n\n"; - fprintf (stderr, format_string, bb_msg_full_version, + fprintf(stderr, format_string, bb_msg_full_version, applet_using->name, usage_string); } Modified: trunk/busybox/coreutils/who.c =================================================================== --- trunk/busybox/coreutils/who.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/coreutils/who.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -30,7 +30,7 @@ if (s < 60) return "."; if (s < (24 * 60 * 60)) { - sprintf (str, "%02d:%02d", + sprintf(str, "%02d:%02d", (int) (s / (60 * 60)), (int) ((s % (60 * 60)) / 60)); return str; Modified: trunk/busybox/e2fsprogs/e2fsck.c =================================================================== --- trunk/busybox/e2fsprogs/e2fsck.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/e2fsprogs/e2fsck.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -9818,7 +9818,7 @@ err = journal_bmap(journal, offset, &blocknr); if (err) { - printf ("JBD: bad block at offset %u\n", offset); + printf("JBD: bad block at offset %u\n", offset); return err; } @@ -9835,7 +9835,7 @@ } if (!buffer_uptodate(bh)) { - printf ("JBD: Failed to read block at offset %u\n", offset); + printf("JBD: Failed to read block at offset %u\n", offset); brelse(bh); return -EIO; } @@ -10049,7 +10049,7 @@ /* Recover what we can, but * report failure at the end. */ success = err; - printf ("JBD: IO error %d recovering " + printf("JBD: IO error %d recovering " "block %ld in log\n", err, io_block); } else { @@ -10074,7 +10074,7 @@ blocknr, journal->j_blocksize); if (nbh == NULL) { - printf ("JBD: Out of memory " + printf("JBD: Out of memory " "during recovery.\n"); err = -ENOMEM; brelse(bh); @@ -10153,7 +10153,7 @@ /* It's really bad news if different passes end up at * different places (but possible due to IO errors). */ if (info->end_transaction != next_commit_ID) { - printf ("JBD: recovery pass %d ended at " + printf("JBD: recovery pass %d ended at " "transaction %u, expected %u\n", pass, next_commit_ID, info->end_transaction); if (!success) @@ -12335,15 +12335,15 @@ int ask (e2fsck_t ctx, const char * string, int def) { if (ctx->options & E2F_OPT_NO) { - printf (_("%s? no\n\n"), string); + printf(_("%s? no\n\n"), string); return 0; } if (ctx->options & E2F_OPT_YES) { - printf (_("%s? yes\n\n"), string); + printf(_("%s? yes\n\n"), string); return 1; } if (ctx->options & E2F_OPT_PREEN) { - printf ("%s? %s\n\n", string, def ? _("yes") : _("no")); + printf("%s? %s\n\n", string, def ? _("yes") : _("no")); return def; } return ask_yn(string, def); @@ -12606,26 +12606,26 @@ blocks_used, blocks); return; } - printf ("\n%8d inode%s used (%d%%)\n", P_E2("", "s", inodes_used), + printf("\n%8d inode%s used (%d%%)\n", P_E2("", "s", inodes_used), 100 * inodes_used / inodes); - printf ("%8d non-contiguous inode%s (%0d.%d%%)\n", + printf("%8d non-contiguous inode%s (%0d.%d%%)\n", P_E2("", "s", ctx->fs_fragmented), frag_percent / 10, frag_percent % 10); - printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"), + printf(_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"), ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); - printf ("%8d block%s used (%d%%)\n", P_E2("", "s", blocks_used), + printf("%8d block%s used (%d%%)\n", P_E2("", "s", blocks_used), (int) ((long long) 100 * blocks_used / blocks)); - printf ("%8d large file%s\n", P_E2("", "s", ctx->large_files)); - printf ("\n%8d regular file%s\n", P_E2("", "s", ctx->fs_regular_count)); - printf ("%8d director%s\n", P_E2("y", "ies", ctx->fs_directory_count)); - printf ("%8d character device file%s\n", P_E2("", "s", ctx->fs_chardev_count)); - printf ("%8d block device file%s\n", P_E2("", "s", ctx->fs_blockdev_count)); - printf ("%8d fifo%s\n", P_E2("", "s", ctx->fs_fifo_count)); - printf ("%8d link%s\n", P_E2("", "s", ctx->fs_links_count - dir_links)); - printf ("%8d symbolic link%s", P_E2("", "s", ctx->fs_symlinks_count)); - printf (" (%d fast symbolic link%s)\n", P_E2("", "s", ctx->fs_fast_symlinks_count)); - printf ("%8d socket%s--------\n\n", P_E2("", "s", ctx->fs_sockets_count)); - printf ("%8d file%s\n", P_E2("", "s", ctx->fs_total_count - dir_links)); + printf("%8d large file%s\n", P_E2("", "s", ctx->large_files)); + printf("\n%8d regular file%s\n", P_E2("", "s", ctx->fs_regular_count)); + printf("%8d director%s\n", P_E2("y", "ies", ctx->fs_directory_count)); + printf("%8d character device file%s\n", P_E2("", "s", ctx->fs_chardev_count)); + printf("%8d block device file%s\n", P_E2("", "s", ctx->fs_blockdev_count)); + printf("%8d fifo%s\n", P_E2("", "s", ctx->fs_fifo_count)); + printf("%8d link%s\n", P_E2("", "s", ctx->fs_links_count - dir_links)); + printf("%8d symbolic link%s", P_E2("", "s", ctx->fs_symlinks_count)); + printf(" (%d fast symbolic link%s)\n", P_E2("", "s", ctx->fs_fast_symlinks_count)); + printf("%8d socket%s--------\n\n", P_E2("", "s", ctx->fs_sockets_count)); + printf("%8d file%s\n", P_E2("", "s", ctx->fs_total_count - dir_links)); } static void check_mount(e2fsck_t ctx) @@ -12663,7 +12663,7 @@ "SEVERE filesystem damage.\007\007\007\n\n")); cont = ask_yn(_("Do you really want to continue"), -1); if (!cont) { - printf (_("check aborted.\n")); + printf(_("check aborted.\n")); exit (0); } return; Modified: trunk/busybox/e2fsprogs/e2p/ps.c =================================================================== --- trunk/busybox/e2fsprogs/e2p/ps.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/e2fsprogs/e2p/ps.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -23,5 +23,5 @@ { fprintf(f, (state & EXT2_VALID_FS ? " clean" : " not clean")); if (state & EXT2_ERROR_FS) - fprintf (f, " with errors"); + fprintf(f, " with errors"); } Modified: trunk/busybox/findutils/find.c =================================================================== --- trunk/busybox/findutils/find.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/findutils/find.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -56,20 +56,20 @@ typedef struct { action_fp f; } action; -#define SACT(name, arg...) typedef struct { action a; arg; } action_##name; -#define SFUNC(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap) - SACT(print) - SACT(name, char *pattern;) -USE_FEATURE_FIND_PRINT0(SACT(print0)) -USE_FEATURE_FIND_TYPE( SACT(type, int type_mask;)) -USE_FEATURE_FIND_PERM( SACT(perm, char perm_char; int perm_mask;)) -USE_FEATURE_FIND_MTIME( SACT(mtime, char mtime_char; int mtime_days;)) -USE_FEATURE_FIND_MMIN( SACT(mmin, char mmin_char; int mmin_mins;)) -USE_FEATURE_FIND_NEWER( SACT(newer, time_t newer_mtime;)) -USE_FEATURE_FIND_INUM( SACT(inum, ino_t inode_num;)) -USE_FEATURE_FIND_EXEC( SACT(exec, char **exec_argv; int *subst_count; int exec_argc;)) -USE_DESKTOP( SACT(paren, action ***subexpr;)) -USE_DESKTOP( SACT(prune)) +#define ACTS(name, arg...) typedef struct { action a; arg; } action_##name; +#define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap) + ACTS(print) + ACTS(name, char *pattern;) +USE_FEATURE_FIND_PRINT0(ACTS(print0)) +USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) +USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; int perm_mask;)) +USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; int mtime_days;)) +USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; int mmin_mins;)) +USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) +USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) +USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; int *subst_count; int exec_argc;)) +USE_DESKTOP( ACTS(paren, action ***subexpr;)) +USE_DESKTOP( ACTS(prune)) static action ***actions; static int need_print = 1; @@ -131,7 +131,7 @@ } -SFUNC(name) +ACTF(name) { const char *tmp = strrchr(fileName, '/'); if (tmp == NULL) @@ -141,13 +141,13 @@ return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0; } #if ENABLE_FEATURE_FIND_TYPE -SFUNC(type) +ACTF(type) { return ((statbuf->st_mode & S_IFMT) == ap->type_mask); } #endif #if ENABLE_FEATURE_FIND_PERM -SFUNC(perm) +ACTF(perm) { return !((isdigit(ap->perm_char) && (statbuf->st_mode & 07777) == ap->perm_mask) || (ap->perm_char == '-' && (statbuf->st_mode & ap->perm_mask) == ap->perm_mask) @@ -155,7 +155,7 @@ } #endif #if ENABLE_FEATURE_FIND_MTIME -SFUNC(mtime) +ACTF(mtime) { time_t file_age = time(NULL) - statbuf->st_mtime; time_t mtime_secs = ap->mtime_days * 24 * 60 * 60; @@ -166,7 +166,7 @@ } #endif #if ENABLE_FEATURE_FIND_MMIN -SFUNC(mmin) +ACTF(mmin) { time_t file_age = time(NULL) - statbuf->st_mtime; time_t mmin_secs = ap->mmin_mins * 60; @@ -177,19 +177,19 @@ } #endif #if ENABLE_FEATURE_FIND_NEWER -SFUNC(newer) +ACTF(newer) { return (ap->newer_mtime >= statbuf->st_mtime); } #endif #if ENABLE_FEATURE_FIND_INUM -SFUNC(inum) +ACTF(inum) { return (statbuf->st_ino != ap->inode_num); } #endif #if ENABLE_FEATURE_FIND_EXEC -SFUNC(exec) +ACTF(exec) { int i, rc; char *argv[ap->exec_argc+1]; @@ -207,21 +207,21 @@ #endif #if ENABLE_FEATURE_FIND_PRINT0 -SFUNC(print0) +ACTF(print0) { printf("%s%c", fileName, '\0'); return TRUE; } #endif -SFUNC(print) +ACTF(print) { puts(fileName); return TRUE; } #if ENABLE_DESKTOP -SFUNC(paren) +ACTF(paren) { return exec_actions(ap->subexpr, fileName, statbuf); } @@ -231,7 +231,7 @@ * Example: * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print */ -SFUNC(prune) +ACTF(prune) { return SKIP; } @@ -501,6 +501,7 @@ int find_main(int argc, char **argv) { int dereference = FALSE; + char *arg; char **argp; int i, firstopt, status = EXIT_SUCCESS; @@ -524,10 +525,9 @@ // We implement: -follow, -xdev /* Process options, and replace then with -a */ - /* (that will be ignored by recursive parser later) */ + /* (-a will be ignored by recursive parser later) */ argp = &argv[firstopt]; - while (*argp) { - char *arg = argp[0]; + while ((arg = argp[0])) { if (strcmp(arg, "-follow") == 0) { dereference = TRUE; argp[0] = "-a"; Modified: trunk/busybox/loginutils/adduser.c =================================================================== --- trunk/busybox/loginutils/adduser.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/loginutils/adduser.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -180,7 +180,7 @@ /* create string for $HOME if not specified already */ if (!pw.pw_dir) { snprintf(bb_common_bufsiz1, BUFSIZ, "/home/%s", argv[optind]); - pw.pw_dir = &bb_common_bufsiz1[0]; + pw.pw_dir = &bb_common_bufsiz1[0]; } /* create a passwd struct */ Modified: trunk/busybox/miscutils/mt.c =================================================================== --- trunk/busybox/miscutils/mt.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/miscutils/mt.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -107,7 +107,7 @@ case MTTELL: if (ioctl(fd, MTIOCPOS, &position) < 0) bb_perror_msg_and_die("%s", file); - printf ("At block %d.\n", (int) position.mt_blkno); + printf("At block %d.\n", (int) position.mt_blkno); break; default: Modified: trunk/busybox/networking/libiproute/iproute.c =================================================================== --- trunk/busybox/networking/libiproute/iproute.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/networking/libiproute/iproute.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -455,14 +455,14 @@ char *buffer = "-1"; if (flush_fd < 0) { - fprintf (stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH); + fprintf(stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH); return -1; } len = strlen (buffer); if ((write (flush_fd, (void *)buffer, len)) < len) { - fprintf (stderr, "Cannot flush routing cache\n"); + fprintf(stderr, "Cannot flush routing cache\n"); return -1; } close(flush_fd); Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/procps/ps.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -72,8 +72,7 @@ safe_strncpy(sbuf, "unknown", 7); } len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state); - } - else + } else #endif if (p->rss == 0) len = printf("%5u %-8s %s ", (unsigned)p->pid, p->user, p->state); Modified: trunk/busybox/util-linux/hwclock.c =================================================================== --- trunk/busybox/util-linux/hwclock.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/util-linux/hwclock.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -10,14 +10,7 @@ #include #include -#include -#include #include -#include -#include -#include -#include -#include #include "busybox.h" /* Copied from linux/rtc.h to eliminate the kernel dependency */ @@ -42,59 +35,62 @@ # endif #endif -static time_t read_rtc(int utc) +static int xopen_rtc(int flags) { int rtc; + rtc = open("/dev/rtc", flags); + if (rtc < 0) { + rtc = open("/dev/misc/rtc", flags); + if (rtc < 0) + bb_perror_msg_and_die("cannot access RTC"); + } + return rtc; +} + +static time_t read_rtc(int utc) +{ struct tm tm; char *oldtz = 0; time_t t = 0; + int rtc = xopen_rtc(O_RDONLY); - if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) { - if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 ) - bb_perror_msg_and_die ( "cannot access RTC" ); - } - memset ( &tm, 0, sizeof( struct tm )); - if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 ) - bb_perror_msg_and_die ( "cannot read time from RTC" ); + memset(&tm, 0, sizeof(struct tm)); + if (ioctl(rtc, RTC_RD_TIME, &tm) < 0 ) + bb_perror_msg_and_die("cannot read time from RTC"); tm.tm_isdst = -1; /* not known */ - close ( rtc ); + close(rtc); - if ( utc ) { - oldtz = getenv ( "TZ" ); - setenv ( "TZ", "UTC 0", 1 ); - tzset ( ); + if (utc) { + oldtz = getenv("TZ"); + setenv("TZ", "UTC 0", 1); + tzset(); } - t = mktime ( &tm ); + t = mktime(&tm); - if ( utc ) { - if ( oldtz ) - setenv ( "TZ", oldtz, 1 ); + if (utc) { + if (oldtz) + setenv("TZ", oldtz, 1); else - unsetenv ( "TZ" ); - tzset ( ); + unsetenv("TZ"); + tzset(); } return t; } static void write_rtc(time_t t, int utc) { - int rtc; struct tm tm; + int rtc = xopen_rtc(O_WRONLY); - if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) { - if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 ) - bb_perror_msg_and_die ( "cannot access RTC" ); - } - - tm = *( utc ? gmtime ( &t ) : localtime ( &t )); + tm = *(utc ? gmtime(&t) : localtime(&t)); tm.tm_isdst = 0; - if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 ) - bb_perror_msg_and_die ( "cannot set the RTC time" ); + if (ioctl(rtc, RTC_SET_TIME, &tm) < 0) + bb_perror_msg_and_die("cannot set the RTC time"); - close ( rtc ); + close(rtc); } static int show_clock(int utc) @@ -103,15 +99,15 @@ time_t t; RESERVE_CONFIG_BUFFER(buffer, 64); - t = read_rtc ( utc ); - ptm = localtime ( &t ); /* Sets 'tzname[]' */ + t = read_rtc(utc); + ptm = localtime(&t); /* Sets 'tzname[]' */ - safe_strncpy ( buffer, ctime ( &t ), 64); - if ( buffer [0] ) - buffer [strlen ( buffer ) - 1] = 0; + safe_strncpy(buffer, ctime(&t), 64); + if (buffer[0]) + buffer[strlen(buffer) - 1] = 0; - //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] )); - printf ( "%s %.6f seconds\n", buffer, 0.0 ); + //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname [1] : tzname [0])); + printf( "%s %.6f seconds\n", buffer, 0.0); RELEASE_CONFIG_BUFFER(buffer); return 0; @@ -122,10 +118,10 @@ struct timeval tv = { 0, 0 }; const struct timezone tz = { timezone/60 - 60*daylight, 0 }; - tv.tv_sec = read_rtc ( utc ); + tv.tv_sec = read_rtc(utc); - if ( settimeofday ( &tv, &tz )) - bb_perror_msg_and_die ( "settimeofday() failed" ); + if (settimeofday(&tv, &tz)) + bb_perror_msg_and_die("settimeofday() failed"); return 0; } @@ -135,10 +131,10 @@ struct timeval tv = { 0, 0 }; struct timezone tz = { 0, 0 }; - if ( gettimeofday ( &tv, &tz )) - bb_perror_msg_and_die ( "gettimeofday() failed" ); + if (gettimeofday(&tv, &tz)) + bb_perror_msg_and_die("gettimeofday() failed"); - write_rtc ( tv.tv_sec, utc ); + write_rtc(tv.tv_sec, utc); return 0; } @@ -150,43 +146,43 @@ static int check_utc(void) { int utc = 0; - FILE *f = fopen ( ADJTIME_PATH, "r" ); + FILE *f = fopen(ADJTIME_PATH, "r"); - if ( f ) { + if (f) { RESERVE_CONFIG_BUFFER(buffer, 128); - while ( fgets ( buffer, sizeof( buffer ), f )) { - int len = strlen ( buffer ); + while (fgets(buffer, sizeof(buffer), f)) { + int len = strlen(buffer); - while ( len && isspace ( buffer [len - 1] )) + while (len && isspace(buffer[len - 1])) len--; - buffer [len] = 0; + buffer[len] = 0; - if ( strncmp ( buffer, "UTC", 3 ) == 0 ) { + if (strncmp(buffer, "UTC", 3) == 0 ) { utc = 1; break; } } - fclose ( f ); + fclose(f); RELEASE_CONFIG_BUFFER(buffer); } return utc; } -#define HWCLOCK_OPT_LOCALTIME 0x01 -#define HWCLOCK_OPT_UTC 0x02 -#define HWCLOCK_OPT_SHOW 0x04 -#define HWCLOCK_OPT_HCTOSYS 0x08 -#define HWCLOCK_OPT_SYSTOHC 0x10 +#define HWCLOCK_OPT_LOCALTIME 0x01 +#define HWCLOCK_OPT_UTC 0x02 +#define HWCLOCK_OPT_SHOW 0x04 +#define HWCLOCK_OPT_HCTOSYS 0x08 +#define HWCLOCK_OPT_SYSTOHC 0x10 -int hwclock_main ( int argc, char **argv ) +int hwclock_main(int argc, char **argv ) { unsigned opt; int utc; #if ENABLE_FEATURE_HWCLOCK_LONG_OPTIONS -static const struct option hwclock_long_options[] = { + static const struct option hwclock_long_options[] = { { "localtime", 0, 0, 'l' }, { "utc", 0, 0, 'u' }, { "show", 0, 0, 'r' }, @@ -196,7 +192,6 @@ }; applet_long_options = hwclock_long_options; #endif - opt_complementary = "?:r--ws:w--rs:s--wr:l--u:u--l"; opt = getopt32(argc, argv, "lursw"); @@ -207,12 +202,12 @@ utc = check_utc(); if (opt & HWCLOCK_OPT_HCTOSYS) { - return to_sys_clock ( utc ); + return to_sys_clock(utc); } else if (opt & HWCLOCK_OPT_SYSTOHC) { - return from_sys_clock ( utc ); + return from_sys_clock(utc); } else { /* default HWCLOCK_OPT_SHOW */ - return show_clock ( utc ); + return show_clock(utc); } } Modified: trunk/busybox/util-linux/readprofile.c =================================================================== --- trunk/busybox/util-linux/readprofile.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/util-linux/readprofile.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -44,19 +44,20 @@ int readprofile_main(int argc, char **argv) { FILE *map; - const char *mapFile, *proFile, *mult=0; - unsigned long indx=1; + const char *mapFile, *proFile, *mult = 0; + unsigned long indx = 1; size_t len; - uint64_t add0=0; + uint64_t add0 = 0; unsigned int step; unsigned int *buf, total, fn_len; unsigned long long fn_add, next_add; /* current and next address */ char fn_name[S_LEN], next_name[S_LEN]; /* current and next name */ + char mapline[S_LEN]; char mode[8]; - int optAll=0, optInfo=0, optReset=0, optVerbose=0, optNative=0; - int optBins=0, optSub=0; - char mapline[S_LEN]; - int maplineno=1; + int optAll = 0, optInfo = 0, optReset = 0; + int optVerbose = 0, optNative = 0; + int optBins = 0, optSub = 0; + int maplineno = 1; int header_printed; #define next (current^1) @@ -85,9 +86,9 @@ to_write = 1; /* sth different from sizeof(int) */ } - fd = xopen(defaultpro,O_WRONLY); + fd = xopen(defaultpro, O_WRONLY); - if (write(fd, &multiplier, to_write) != to_write) + if (full_write(fd, &multiplier, to_write) != to_write) bb_perror_msg_and_die("error writing %s", defaultpro); close(fd); @@ -101,7 +102,7 @@ buf = xmalloc_open_read_close(proFile, &len); if (!optNative) { int entries = len/sizeof(*buf); - int big = 0,small = 0,i; + int big = 0, small = 0, i; unsigned *p; for (p = buf+1; p < buf+entries; p++) { @@ -135,12 +136,12 @@ map = xfopen(mapFile, "r"); - while (fgets(mapline,S_LEN,map)) { - if (sscanf(mapline,"%llx %s %s",&fn_add,mode,fn_name) != 3) + while (fgets(mapline, S_LEN, map)) { + if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) bb_error_msg_and_die("%s(%i): wrong map line", mapFile, maplineno); - if (!strcmp(fn_name,"_stext")) /* only elf works like this */ { + if (!strcmp(fn_name, "_stext")) /* only elf works like this */ { add0 = fn_add; break; } @@ -153,12 +154,12 @@ /* * Main loop. */ - while (fgets(mapline,S_LEN,map)) { + while (fgets(mapline, S_LEN, map)) { unsigned int this = 0; - if (sscanf(mapline,"%llx %s %s",&next_add,mode,next_name) != 3) + if (sscanf(mapline, "%llx %s %s", &next_add, mode, next_name) != 3) bb_error_msg_and_die("%s(%i): wrong map line", - mapFile, maplineno); + mapFile, maplineno); header_printed = 0; @@ -176,10 +177,10 @@ while (indx < (next_add-add0)/step) { if (optBins && (buf[indx] || optAll)) { if (!header_printed) { - printf ("%s:\n", fn_name); + printf("%s:\n", fn_name); header_printed = 1; } - printf ("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]); + printf("\t%"PRIx64"\t%u\n", (indx - 1)*step + add0, buf[indx]); } this += buf[indx++]; } @@ -187,15 +188,15 @@ if (optBins) { if (optVerbose || this > 0) - printf (" total\t\t\t\t%u\n", this); + printf(" total\t\t\t\t%u\n", this); } else if ((this || optAll) && (fn_len = next_add-fn_add) != 0) { if (optVerbose) printf("%016llx %-40s %6i %8.4f\n", fn_add, - fn_name,this,this/(double)fn_len); + fn_name, this, this/(double)fn_len); else printf("%6i %-40s %8.4f\n", - this,fn_name,this/(double)fn_len); + this, fn_name, this/(double)fn_len); if (optSub) { unsigned long long scan; @@ -212,7 +213,7 @@ } fn_add = next_add; - strcpy(fn_name,next_name); + strcpy(fn_name, next_name); maplineno++; } @@ -223,10 +224,10 @@ /* trailer */ if (optVerbose) printf("%016x %-40s %6i %8.4f\n", - 0,"total",total,total/(double)(fn_add-add0)); + 0, "total", total, total/(double)(fn_add-add0)); else printf("%6i %-40s %8.4f\n", - total,"total",total/(double)(fn_add-add0)); + total, "total", total/(double)(fn_add-add0)); fclose(map); free(buf); Modified: trunk/busybox/util-linux/switch_root.c =================================================================== --- trunk/busybox/util-linux/switch_root.c 2006-11-01 09:17:47 UTC (rev 16486) +++ trunk/busybox/util-linux/switch_root.c 2006-11-01 10:25:35 UTC (rev 16487) @@ -35,7 +35,7 @@ struct stat st; // Don't descend into other filesystems - if (lstat(directory,&st) || st.st_dev != rootdev) return; + if (lstat(directory, &st) || st.st_dev != rootdev) return; // Recursively delete the contents of directories. if (S_ISDIR(st.st_mode)) { @@ -71,8 +71,8 @@ // Parse args (-c console) - opt_complementary="-2"; - getopt32(argc,argv,"c:",&console); + opt_complementary = "-2"; + getopt32(argc, argv, "c:", &console); // Change to new root directory and verify it's a different fs. @@ -81,7 +81,7 @@ if (chdir(newroot) || lstat(".", &st1) || lstat("/", &st2) || st1.st_dev == st2.st_dev) { - bb_error_msg_and_die("bad newroot %s",newroot); + bb_error_msg_and_die("bad newroot %s", newroot); } rootdev=st2.st_dev; @@ -111,12 +111,12 @@ if (console) { close(0); if(open(console, O_RDWR) < 0) - bb_error_msg_and_die("bad console '%s'",console); + bb_error_msg_and_die("bad console '%s'", console); dup2(0, 1); dup2(0, 2); } // Exec real init. (This is why we must be pid 1.) - execv(argv[optind],argv+optind); - bb_error_msg_and_die("bad init '%s'",argv[optind]); + execv(argv[optind], argv+optind); + bb_error_msg_and_die("bad init '%s'", argv[optind]); } From vda at busybox.net Wed Nov 1 12:20:38 2006 From: vda at busybox.net (vda at busybox.net) Date: Wed, 1 Nov 2006 12:20:38 -0800 (PST) Subject: svn commit: trunk/busybox: include libbb procps util-linux Message-ID: <20061101202038.3D81A486AF@busybox.net> Author: vda Date: 2006-11-01 12:20:37 -0800 (Wed, 01 Nov 2006) New Revision: 16488 Log: Unneeded code removed, usused field "unsigned pscpu" removed Modified: trunk/busybox/include/libbb.h trunk/busybox/libbb/procps.c trunk/busybox/procps/kill.c trunk/busybox/procps/ps.c trunk/busybox/util-linux/hwclock.c Changeset: Modified: trunk/busybox/include/libbb.h =================================================================== --- trunk/busybox/include/libbb.h 2006-11-01 10:25:35 UTC (rev 16487) +++ trunk/busybox/include/libbb.h 2006-11-01 20:20:37 UTC (rev 16488) @@ -501,13 +501,12 @@ #endif #endif typedef struct { - pid_t pid, ppid; + int pid, ppid; char user[9]; char state[4]; unsigned long rss; #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE unsigned pcpu; - unsigned pscpu; unsigned long stime, utime; #endif char *cmd; @@ -525,7 +524,7 @@ extern const char bb_uuenc_tbl_std[]; void bb_uuencode(const unsigned char *s, char *store, const int length, const char *tbl); -typedef struct _sha1_ctx_t_ { +typedef struct sha1_ctx_t { uint32_t count[2]; uint32_t hash[5]; uint32_t wbuf[16]; @@ -534,7 +533,7 @@ void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx); void *sha1_end(void *resbuf, sha1_ctx_t *ctx); -typedef struct _md5_ctx_t_ { +typedef struct md5_ctx_t { uint32_t A; uint32_t B; uint32_t C; Modified: trunk/busybox/libbb/procps.c =================================================================== --- trunk/busybox/libbb/procps.c 2006-11-01 10:25:35 UTC (rev 16487) +++ trunk/busybox/libbb/procps.c 2006-11-01 20:20:37 UTC (rev 16488) @@ -36,13 +36,13 @@ struct dirent *entry; char *name; - int n; - char status[32]; + char buf[PROCPS_BUFSIZE]; + char status[sizeof("/proc//cmdline") + sizeof(int)*3]; char *status_tail; - char buf[PROCPS_BUFSIZE]; procps_status_t curstatus; + long tasknice; int pid; - long tasknice; + int n; struct stat sb; if (!dir) { Modified: trunk/busybox/procps/kill.c =================================================================== --- trunk/busybox/procps/kill.c 2006-11-01 10:25:35 UTC (rev 16487) +++ trunk/busybox/procps/kill.c 2006-11-01 20:20:37 UTC (rev 16488) @@ -84,9 +84,10 @@ pid_t sid; procps_status_t* p; - /* kill(-1, sig) on Linux (at least 2.1.x) - * might send signal to the calling process too */ - signal(SIGTERM, SIG_IGN); +// Cannot happen anyway? We don't TERM ourself, we STOP +// /* kill(-1, sig) on Linux (at least 2.1.x) +// * might send signal to the calling process too */ +// signal(SIGTERM, SIG_IGN); /* Now stop all processes */ kill(-1, SIGSTOP); /* Find out our own session id */ Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-01 10:25:35 UTC (rev 16487) +++ trunk/busybox/procps/ps.c 2006-11-01 20:20:37 UTC (rev 16488) @@ -25,7 +25,7 @@ #if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX #if ENABLE_FEATURE_PS_WIDE opt_complementary = "-:ww"; - USE_SELINUX(i =) getopt32(argc, argv, "w" USE_SELINUX("c"), &w_count); + USE_SELINUX(i =) getopt32(argc, argv, USE_SELINUX("c") "w", &w_count); /* if w is given once, GNU ps sets the width to 132, * if w is given more than once, it is "unlimited" */ @@ -40,7 +40,7 @@ i = getopt32(argc, argv, "c"); #endif #if ENABLE_SELINUX - if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled()) + if ((i & 1) && is_selinux_enabled()) use_selinux = 1; #endif #endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */ Modified: trunk/busybox/util-linux/hwclock.c =================================================================== --- trunk/busybox/util-linux/hwclock.c 2006-11-01 10:25:35 UTC (rev 16487) +++ trunk/busybox/util-linux/hwclock.c 2006-11-01 20:20:37 UTC (rev 16488) @@ -106,7 +106,7 @@ if (buffer[0]) buffer[strlen(buffer) - 1] = 0; - //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname [1] : tzname [0])); + //printf("%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : (ptm->tm_isdst ? tzname[1] : tzname[0])); printf( "%s %.6f seconds\n", buffer, 0.0); RELEASE_CONFIG_BUFFER(buffer); From vda at busybox.net Thu Nov 2 14:09:38 2006 From: vda at busybox.net (vda at busybox.net) Date: Thu, 2 Nov 2006 14:09:38 -0800 (PST) Subject: svn commit: trunk/busybox: networking shell Message-ID: <20061102220938.4EA96485D8@busybox.net> Author: vda Date: 2006-11-02 14:09:37 -0800 (Thu, 02 Nov 2006) New Revision: 16490 Log: Fix kbuild bugs noticed by Bernhard Fischer Modified: trunk/busybox/networking/Config.in trunk/busybox/shell/Config.in trunk/busybox/shell/cmdedit.c Changeset: Modified: trunk/busybox/networking/Config.in =================================================================== --- trunk/busybox/networking/Config.in 2006-11-02 21:46:07 UTC (rev 16489) +++ trunk/busybox/networking/Config.in 2006-11-02 22:09:37 UTC (rev 16490) @@ -53,7 +53,7 @@ config FEATURE_FTPGETPUT_LONG_OPTIONS bool "Enable long options in ftpget/ftpput" default n - depends on GETOPT_LONG && (CONFIG_FTPGET || FTPPUT) + depends on GETOPT_LONG && (FTPGET || FTPPUT) help Support long options for the ftpget/ftpput applet. @@ -239,20 +239,20 @@ help Use the busybox iproute "ip" applet to implement "ifupdown". - If leave this disabled, you must install the full-blown iproute2 + If left disabled, you must install the full-blown iproute2 utility or the "ifup" and "ifdown" applets will not work. -config FEATURE_IFUPDOWN_IP_BUILTIN +config FEATURE_IFUPDOWN_IFCONFIG_BUILTIN bool "Use busybox ifconfig and route applets" default y - depends on IFUPDOWN && !CONFIG_FEATURE_IFUPDOWN_IP + depends on IFUPDOWN && !FEATURE_IFUPDOWN_IP select IFCONFIG select ROUTE help Use the busybox iproute "ifconfig" and "route" applets to implement the "ifup" and "ifdown" utilities. - If leave this disabled, you must install the full-blown ifconfig + If left disabled, you must install the full-blown ifconfig and route utilities, or the "ifup" and "ifdown" applets will not work. Modified: trunk/busybox/shell/Config.in =================================================================== --- trunk/busybox/shell/Config.in 2006-11-02 21:46:07 UTC (rev 16489) +++ trunk/busybox/shell/Config.in 2006-11-02 22:09:37 UTC (rev 16490) @@ -257,6 +257,7 @@ config FEATURE_COMMAND_HISTORY int "History size" + range 0 99999 default 15 depends on FEATURE_COMMAND_EDITING help Modified: trunk/busybox/shell/cmdedit.c =================================================================== --- trunk/busybox/shell/cmdedit.c 2006-11-02 21:46:07 UTC (rev 16489) +++ trunk/busybox/shell/cmdedit.c 2006-11-02 22:09:37 UTC (rev 16490) @@ -83,12 +83,10 @@ #ifndef CONFIG_FEATURE_COMMAND_HISTORY #define MAX_HISTORY 15 #else -#define MAX_HISTORY CONFIG_FEATURE_COMMAND_HISTORY +#define MAX_HISTORY (CONFIG_FEATURE_COMMAND_HISTORY + 0) #endif -#if MAX_HISTORY < 1 -#warning cmdedit: You set MAX_HISTORY < 1. The history algorithm switched off. -#else +#if MAX_HISTORY > 0 static char *history[MAX_HISTORY+1]; /* history + current */ /* saved history lines */ static int n_history; @@ -1166,7 +1164,7 @@ } #endif /* CONFIG_FEATURE_COMMAND_TAB_COMPLETION */ -#if MAX_HISTORY >= 1 +#if MAX_HISTORY > 0 static void get_previous_history(void) { if(command_ps[0] != 0 || history[cur_history] == 0) { @@ -1529,7 +1527,7 @@ printf("\033[H"); redraw(0, len-cursor); break; -#if MAX_HISTORY >= 1 +#if MAX_HISTORY > 0 case CNTRL('N'): vi_case( case CNTRL('N')|vbit: ) vi_case( case 'j'|vbit: ) @@ -1730,7 +1728,7 @@ input_tab(&lastWasTab); break; #endif -#if MAX_HISTORY >= 1 +#if MAX_HISTORY > 0 case 'A': /* Up Arrow -- Get previous command from history */ if (cur_history > 0) { @@ -1838,7 +1836,7 @@ setTermSettings(0, (void *) &initial_settings); handlers_sets &= ~SET_RESET_TERM; -#if MAX_HISTORY >= 1 +#if MAX_HISTORY > 0 /* Handle command history log */ /* cleanup may be saved current command line */ if (len> 0) { /* no put empty line */ @@ -1859,13 +1857,13 @@ num_ok_lines++; #endif } -#else /* MAX_HISTORY < 1 */ +#else /* MAX_HISTORY == 0 */ #if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) if (len > 0) { /* no put empty line */ num_ok_lines++; } #endif -#endif /* MAX_HISTORY >= 1 */ +#endif /* MAX_HISTORY > 0 */ if (break_out > 0) { command[len++] = '\n'; /* set '\n' */ command[len] = 0; From bugs at busybox.net Fri Nov 3 13:27:02 2006 From: bugs at busybox.net (bugs at busybox.net) Date: Fri, 3 Nov 2006 13:27:02 -0800 Subject: [BusyBox 0001080]: ls /dir/files* | grep -v k --- where k is not present on any file, it dosent report Message-ID: <848ac32df1d942c8c490f19e80d244d5@busybox.net> The following issue has been SUBMITTED. ====================================================================== http://busybox.net/bugs/view.php?id=1080 ====================================================================== Reported By: aldleonm Assigned To: BusyBox ====================================================================== Project: BusyBox Issue ID: 1080 Category: Standards Compliance Reproducibility: always Severity: major Priority: normal Status: assigned ====================================================================== Date Submitted: 11-03-2006 13:27 PST Last Modified: 11-03-2006 13:27 PST ====================================================================== Summary: ls /dir/files* | grep -v k --- where k is not present on any file, it dosent report Description: Was using 1.2.1 .. no problems of that kind. Upgraded to 1.2.2, also tried 1.2.2.1 ... the same error # ls /dir/ file.a file.b file.c # ls /dir/file.*|grep -v k # NOTHING !! ------------ I think is 'ls' that is somewhat broken. ------------ ( inside a normal unix ) # ./busybox ls file1 file2 file3 filea fileb # ./busybox ls | wc -c 0 # ./busybox ls | ./busybox wc -c 0 # the same, nothing ====================================================================== Issue History Date Modified Username Field Change ====================================================================== 11-03-06 13:27 aldleonm New Issue 11-03-06 13:27 aldleonm Status new => assigned 11-03-06 13:27 aldleonm Assigned To => BusyBox 11-03-06 13:27 aldleonm File Added: config-1.2.2.1 ====================================================================== From bugs at busybox.net Fri Nov 3 17:32:09 2006 From: bugs at busybox.net (bugs at busybox.net) Date: Fri, 3 Nov 2006 17:32:09 -0800 Subject: [BusyBox 0001080]: ls /dir/files* | grep -v k --- where k is not present on any file, it dosent report Message-ID: <2d1f91adda60ce405314a9b747c4f996@busybox.net> A NOTE has been added to this issue. ====================================================================== http://busybox.net/bugs/view.php?id=1080 ====================================================================== Reported By: aldleonm Assigned To: BusyBox ====================================================================== Project: BusyBox Issue ID: 1080 Category: Standards Compliance Reproducibility: always Severity: major Priority: normal Status: assigned ====================================================================== Date Submitted: 11-03-2006 13:27 PST Last Modified: 11-03-2006 17:32 PST ====================================================================== Summary: ls /dir/files* | grep -v k --- where k is not present on any file, it dosent report Description: Was using 1.2.1 .. no problems of that kind. Upgraded to 1.2.2, also tried 1.2.2.1 ... the same error # ls /dir/ file.a file.b file.c # ls /dir/file.*|grep -v k # NOTHING !! ------------ I think is 'ls' that is somewhat broken. ------------ ( inside a normal unix ) # ./busybox ls file1 file2 file3 filea fileb # ./busybox ls | wc -c 0 # ./busybox ls | ./busybox wc -c 0 # the same, nothing ====================================================================== ---------------------------------------------------------------------- vda - 11-03-06 17:32 ---------------------------------------------------------------------- CONFIG_STATIC=y It's interesting how you managed to slip past applets.c #if ENABLE_STATIC && defined(__GLIBC__) #warning Static linking against glibc produces buggy executables #warning See sources.redhat.com/bugzilla/show_bug.cgi?id=3400 #warning Note that glibc is utterly unsuitable for static linking anyway. #endif in the 1.2.2.1... Issue History Date Modified Username Field Change ====================================================================== 11-03-06 13:27 aldleonm New Issue 11-03-06 13:27 aldleonm Status new => assigned 11-03-06 13:27 aldleonm Assigned To => BusyBox 11-03-06 13:27 aldleonm File Added: config-1.2.2.1 11-03-06 17:32 vda Note Added: 0001728 ====================================================================== From vda at busybox.net Sat Nov 4 16:38:52 2006 From: vda at busybox.net (vda at busybox.net) Date: Sat, 4 Nov 2006 16:38:52 -0800 (PST) Subject: svn commit: trunk/busybox: include libbb procps Message-ID: <20061105003852.5A64A48607@busybox.net> Author: vda Date: 2006-11-04 16:38:51 -0800 (Sat, 04 Nov 2006) New Revision: 16508 Log: top: improve CPU% calculation style fixes Modified: trunk/busybox/include/libbb.h trunk/busybox/libbb/procps.c trunk/busybox/procps/kill.c trunk/busybox/procps/ps.c trunk/busybox/procps/top.c Changeset: Modified: trunk/busybox/include/libbb.h =================================================================== --- trunk/busybox/include/libbb.h 2006-11-04 20:17:03 UTC (rev 16507) +++ trunk/busybox/include/libbb.h 2006-11-05 00:38:51 UTC (rev 16508) @@ -494,10 +494,10 @@ #ifndef COMM_LEN #ifdef TASK_COMM_LEN -#define COMM_LEN TASK_COMM_LEN +enum { COMM_LEN = TASK_COMM_LEN }; #else /* synchronize with sizeof(task_struct.comm) in /usr/include/linux/sched.h */ -#define COMM_LEN 16 +enum { COMM_LEN = 16 }; #endif #endif typedef struct { Modified: trunk/busybox/libbb/procps.c =================================================================== --- trunk/busybox/libbb/procps.c 2006-11-04 20:17:03 UTC (rev 16507) +++ trunk/busybox/libbb/procps.c 2006-11-05 00:38:51 UTC (rev 16508) @@ -8,13 +8,6 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include -#include -#include -#include -#include - #include "libbb.h" Modified: trunk/busybox/procps/kill.c =================================================================== --- trunk/busybox/procps/kill.c 2006-11-04 20:17:03 UTC (rev 16507) +++ trunk/busybox/procps/kill.c 2006-11-05 00:38:51 UTC (rev 16508) @@ -15,26 +15,26 @@ char *arg; pid_t pid; int signo = SIGTERM, errors = 0, quiet = 0; - const int killall = (ENABLE_KILLALL && applet_name[4]=='a' - && (!ENABLE_KILLALL5 || applet_name[7]!='5')); - const int killall5 = (ENABLE_KILLALL5 && applet_name[4]=='a' - && (!ENABLE_KILLALL || applet_name[7]=='5')); + const int killall = (ENABLE_KILLALL && applet_name[4] == 'a' + && (!ENABLE_KILLALL5 || applet_name[7] != '5')); + const int killall5 = (ENABLE_KILLALL5 && applet_name[4] == 'a' + && (!ENABLE_KILLALL || applet_name[7] == '5')); /* Parse any options */ argc--; arg = *++argv; - if (argc<1 || arg[0]!='-') { + if (argc < 1 || arg[0] != '-') { goto do_it_now; } /* The -l option, which prints out signal names. */ - if (arg[1]=='l' && arg[2]=='\0') { + if (arg[1] == 'l' && arg[2] == '\0') { const char *name; - if (argc==1) { + if (argc == 1) { /* Print the whole signal list */ int col = 0; - for (signo = 1; signo<32; signo++) { + for (signo = 1; signo < 32; signo++) { name = get_signame(signo); if (isdigit(name[0])) continue; if (col > 66) { @@ -45,13 +45,13 @@ } puts(""); } else { /* -l */ - while ((arg = *++argv)!=NULL) { + while ((arg = *++argv)) { if (isdigit(arg[0])) { signo = xatoi_u(arg); name = get_signame(signo); } else { signo = get_signum(arg); - if (signo<0) + if (signo < 0) bb_error_msg_and_die("unknown signal '%s'", arg); name = get_signame(signo); } @@ -63,17 +63,17 @@ } /* The -q quiet option */ - if (killall && arg[1]=='q' && arg[2]=='\0') { + if (killall && arg[1] == 'q' && arg[2] == '\0') { quiet = 1; arg = *++argv; argc--; - if (argc<1) bb_show_usage(); - if (arg[0]!='-') goto do_it_now; + if (argc < 1) bb_show_usage(); + if (arg[0] != '-') goto do_it_now; } /* -SIG */ signo = get_signum(&arg[1]); - if (signo<0) + if (signo < 0) bb_error_msg_and_die("bad signal name '%s'", &arg[1]); arg = *++argv; argc--; Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-04 20:17:03 UTC (rev 16507) +++ trunk/busybox/procps/ps.c 2006-11-05 00:38:51 UTC (rev 16508) @@ -64,7 +64,7 @@ if (sid) { /* I assume sid initialized with NULL */ - len = strlen(sid)+1; + len = strlen(sid) + 1; safe_strncpy(sbuf, sid, len); freecon(sid); sid = NULL; Modified: trunk/busybox/procps/top.c =================================================================== --- trunk/busybox/procps/top.c 2006-11-04 20:17:03 UTC (rev 16507) +++ trunk/busybox/procps/top.c 2006-11-05 00:38:51 UTC (rev 16508) @@ -36,7 +36,7 @@ static int ntop; #define OPT_BATCH_MODE (option_mask32 & 0x4) -#ifdef CONFIG_FEATURE_USE_TERMIOS +#if ENABLE_FEATURE_USE_TERMIOS static int pid_sort(procps_status_t *P, procps_status_t *Q) { return (Q->pid - P->pid); @@ -48,10 +48,10 @@ return (int)(Q->rss - P->rss); } -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE -#define sort_depth 3 -static cmp_t sort_function[sort_depth]; +enum { SORT_DEPTH = 3 }; +static cmp_t sort_function[SORT_DEPTH]; static int pcpu_sort(procps_status_t *P, procps_status_t *Q) { @@ -66,7 +66,7 @@ static int mult_lvl_cmp(void* a, void* b) { int i, cmp_val; - for (i = 0; i < sort_depth; i++) { + for (i = 0; i < SORT_DEPTH; i++) { cmp_val = (*sort_function[i])(a, b); if (cmp_val != 0) return cmp_val; @@ -93,13 +93,12 @@ static unsigned total_pcpu; /* static unsigned long total_rss; */ -struct jiffy_counts { +typedef struct { unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal; unsigned long long total; unsigned long long busy; -}; -static struct jiffy_counts jif, prev_jif; - +} jiffy_counts_t; +static jiffy_counts_t jif, prev_jif; static void get_jiffy_counts(void) { FILE* fp = xfopen("stat", "r"); @@ -170,7 +169,7 @@ } #else static cmp_t sort_function; -#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ +#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ /* display generic info (meminfo / loadavg) */ static unsigned long display_generic(int scr_width) @@ -271,8 +270,9 @@ unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */ unsigned pmem_shift, pmem_scale; -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE unsigned pcpu_shift, pcpu_scale; + unsigned busy_jifs; /* what info of the processes is shown */ printf(OPT_BATCH_MODE ? "%.*s" : "\e[7m%.*s\e[0m", scr_width, @@ -296,7 +296,12 @@ pmem_scale /= 4; pmem_shift -= 2; } -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE + busy_jifs = jif.busy - prev_jif.busy; + /* This happens if there were lots of short-lived processes + * between two top updates (e.g. compilation) */ + if (total_pcpu < busy_jifs) total_pcpu = busy_jifs; + /* * CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks * (pcpu is delta of sys+user time between samples) @@ -306,7 +311,7 @@ * we assume that unsigned is at least 32-bit. */ pcpu_shift = 6; - pcpu_scale = (1000*64*(uint16_t)(jif.busy-prev_jif.busy) ? : 1); + pcpu_scale = (1000*64*(uint16_t)busy_jifs ? : 1); while (pcpu_scale < (1U<<(bits_per_int-2))) { pcpu_scale *= 4; pcpu_shift += 2; @@ -352,7 +357,7 @@ ntop = 0; } -#ifdef CONFIG_FEATURE_USE_TERMIOS +#if ENABLE_FEATURE_USE_TERMIOS #include #include @@ -362,12 +367,12 @@ static void reset_term(void) { tcsetattr(0, TCSANOW, (void *) &initial_settings); -#ifdef CONFIG_FEATURE_CLEAN_UP +#if ENABLE_FEATURE_CLEAN_UP clearmems(); -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE free(prev_hist); #endif -#endif /* CONFIG_FEATURE_CLEAN_UP */ +#endif /* FEATURE_CLEAN_UP */ } static void sig_catcher(int sig ATTRIBUTE_UNUSED) @@ -375,7 +380,7 @@ reset_term(); exit(1); } -#endif /* CONFIG_FEATURE_USE_TERMIOS */ +#endif /* FEATURE_USE_TERMIOS */ int top_main(int argc, char **argv) @@ -384,25 +389,24 @@ unsigned interval = 5; /* default update rate is 5 seconds */ unsigned iterations = UINT_MAX; /* 2^32 iterations by default :) */ char *sinterval, *siterations; -#ifdef CONFIG_FEATURE_USE_TERMIOS +#if ENABLE_FEATURE_USE_TERMIOS struct termios new_settings; struct timeval tv; fd_set readfds; unsigned char c; -#endif /* CONFIG_FEATURE_USE_TERMIOS */ +#endif /* FEATURE_USE_TERMIOS */ /* do normal option parsing */ interval = 5; opt_complementary = "-"; - getopt32(argc, argv, "d:n:b", - &sinterval, &siterations); + getopt32(argc, argv, "d:n:b", &sinterval, &siterations); if (option_mask32 & 0x1) interval = xatou(sinterval); // -d if (option_mask32 & 0x2) iterations = xatou(siterations); // -n //if (option_mask32 & 0x4) // -b /* change to /proc */ xchdir("/proc"); -#ifdef CONFIG_FEATURE_USE_TERMIOS +#if ENABLE_FEATURE_USE_TERMIOS tcgetattr(0, (void *) &initial_settings); memcpy(&new_settings, &initial_settings, sizeof(struct termios)); /* unbuffered input, turn off echo */ @@ -412,15 +416,15 @@ signal(SIGINT, sig_catcher); tcsetattr(0, TCSANOW, (void *) &new_settings); atexit(reset_term); -#endif /* CONFIG_FEATURE_USE_TERMIOS */ +#endif /* FEATURE_USE_TERMIOS */ -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE sort_function[0] = pcpu_sort; sort_function[1] = mem_sort; sort_function[2] = time_sort; #else sort_function = mem_sort; -#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ +#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ while (1) { procps_status_t *p; @@ -428,14 +432,14 @@ /* Default to 25 lines - 5 lines for status */ lines = 24 - 3; col = 79; -#ifdef CONFIG_FEATURE_USE_TERMIOS +#if ENABLE_FEATURE_USE_TERMIOS get_terminal_width_height(0, &col, &lines); if (lines < 5 || col < MIN_WIDTH) { sleep(interval); continue; } lines -= 3; -#endif /* CONFIG_FEATURE_USE_TERMIOS */ +#endif /* FEATURE_USE_TERMIOS */ /* read process IDs & status for all the processes */ while ((p = procps_scan(0)) != 0) { @@ -447,7 +451,7 @@ if (ntop == 0) { bb_error_msg_and_die("can't find process info in /proc"); } -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE if (!prev_hist_count) { do_stats(); sleep(1); @@ -458,14 +462,14 @@ qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp); #else qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); -#endif /* CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE */ +#endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ count = lines; if (OPT_BATCH_MODE || count > ntop) { count = ntop; } /* show status for each of the processes */ display_status(count, col); -#ifdef CONFIG_FEATURE_USE_TERMIOS +#if ENABLE_FEATURE_USE_TERMIOS tv.tv_sec = interval; tv.tv_usec = 0; FD_ZERO(&readfds); @@ -478,7 +482,7 @@ if (c == 'q' || c == initial_settings.c_cc[VINTR]) break; if (c == 'M') { -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE sort_function[0] = mem_sort; sort_function[1] = pcpu_sort; sort_function[2] = time_sort; @@ -486,7 +490,7 @@ sort_function = mem_sort; #endif } -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE if (c == 'P') { sort_function[0] = pcpu_sort; sort_function[1] = mem_sort; @@ -499,7 +503,7 @@ } #endif if (c == 'N') { -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE sort_function[0] = pid_sort; #else sort_function = pid_sort; @@ -510,7 +514,7 @@ break; #else sleep(interval); -#endif /* CONFIG_FEATURE_USE_TERMIOS */ +#endif /* FEATURE_USE_TERMIOS */ clearmems(); } if (ENABLE_FEATURE_CLEAN_UP) From vda at busybox.net Sat Nov 4 16:43:52 2006 From: vda at busybox.net (vda at busybox.net) Date: Sat, 4 Nov 2006 16:43:52 -0800 (PST) Subject: svn commit: trunk/busybox: include libbb procps Message-ID: <20061105004352.0F9E648609@busybox.net> Author: vda Date: 2006-11-04 16:43:51 -0800 (Sat, 04 Nov 2006) New Revision: 16509 Log: replace /proc scanning code by more versatile one. Use it where appropriate. Stop scanning /etc/passwd *for every process*!!! (uid->username) top: reduce memory usage - we won't save unneeded fields from /proc info anymore. Downside: ~+250 bytes of code Modified: trunk/busybox/include/libbb.h trunk/busybox/libbb/find_pid_by_name.c trunk/busybox/libbb/procps.c trunk/busybox/procps/kill.c trunk/busybox/procps/ps.c trunk/busybox/procps/top.c Changeset: Modified: trunk/busybox/include/libbb.h =================================================================== --- trunk/busybox/include/libbb.h 2006-11-05 00:38:51 UTC (rev 16508) +++ trunk/busybox/include/libbb.h 2006-11-05 00:43:51 UTC (rev 16509) @@ -501,23 +501,47 @@ #endif #endif typedef struct { - int pid, ppid; - char user[9]; - char state[4]; + DIR *dir; +/* Fields are set to 0/NULL if failed to determine (or not requested) */ + char *cmd; unsigned long rss; -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE - unsigned pcpu; unsigned long stime, utime; -#endif - char *cmd; - - /* basename of executable file in call to exec(2), - size from kernel headers */ - char short_cmd[COMM_LEN]; + unsigned pid; + unsigned ppid; + unsigned pgid; + unsigned sid; + unsigned uid; + unsigned gid; + /* basename of executable file in call to exec(2), size from */ + /* sizeof(task_struct.comm) in /usr/include/linux/sched.h */ + char state[4]; + char comm[COMM_LEN]; +// user/group? - use passwd/group parsing functions } procps_status_t; -procps_status_t* procps_scan(int save_user_arg0); +enum { + PSSCAN_PID = 1 << 0, + PSSCAN_PPID = 1 << 1, + PSSCAN_PGID = 1 << 2, + PSSCAN_SID = 1 << 3, + PSSCAN_UIDGID = 1 << 4, + PSSCAN_COMM = 1 << 5, + PSSCAN_CMD = 1 << 6, + PSSCAN_STATE = 1 << 7, + PSSCAN_RSS = 1 << 8, + PSSCAN_STIME = 1 << 9, + PSSCAN_UTIME = 1 << 10, + /* These are all retrieved from proc/NN/stat in one go: */ + PSSCAN_STAT = PSSCAN_PPID | PSSCAN_PGID | PSSCAN_SID + | PSSCAN_COMM | PSSCAN_STATE + | PSSCAN_RSS | PSSCAN_STIME | PSSCAN_UTIME, +}; +procps_status_t* alloc_procps_scan(int flags); +void free_procps_scan(procps_status_t* sp); +procps_status_t* procps_scan(procps_status_t* sp, int flags); pid_t *find_pid_by_name(const char* procName); pid_t *pidlist_reverse(pid_t *pidList); +void clear_username_cache(void); +const char* get_cached_username(uid_t uid); extern const char bb_uuenc_tbl_base64[]; Modified: trunk/busybox/libbb/find_pid_by_name.c =================================================================== --- trunk/busybox/libbb/find_pid_by_name.c 2006-11-05 00:38:51 UTC (rev 16508) +++ trunk/busybox/libbb/find_pid_by_name.c 2006-11-05 00:43:51 UTC (rev 16509) @@ -23,11 +23,11 @@ { pid_t* pidList; int i = 0; - procps_status_t* p; + procps_status_t* p = NULL; pidList = xmalloc(sizeof(*pidList)); - while ((p = procps_scan(0)) != 0) { - if (strncmp(p->short_cmd, procName, COMM_LEN-1) == 0) { + while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM))) { + if (strncmp(p->comm, procName, sizeof(p->comm)-1) == 0) { pidList = xrealloc(pidList, sizeof(*pidList) * (i+2)); pidList[i++] = p->pid; } Modified: trunk/busybox/libbb/procps.c =================================================================== --- trunk/busybox/libbb/procps.c 2006-11-05 00:38:51 UTC (rev 16508) +++ trunk/busybox/libbb/procps.c 2006-11-05 00:43:51 UTC (rev 16509) @@ -11,6 +11,35 @@ #include "libbb.h" +typedef struct { + uid_t uid; + char username[12]; +} user_map_t; + +static user_map_t *username_cache; +static int username_cache_size; + +void clear_username_cache(void) +{ + free(username_cache); + username_cache = NULL; + username_cache_size = 0; +} + +const char* get_cached_username(uid_t uid) +{ + int i; + for (i = 0; i < username_cache_size; i++) + if (username_cache[i].uid == uid) + return username_cache[i].username; + i = username_cache_size++; + username_cache = xrealloc(username_cache, username_cache_size * sizeof(*username_cache)); + username_cache[i].uid = uid; + bb_getpwuid(username_cache[i].username, uid, sizeof(username_cache[i].username)); + return username_cache[i].username; +} + + #define PROCPS_BUFSIZE 1024 static int read_to_buf(const char *filename, void *buf) @@ -21,119 +50,171 @@ return ret; } +procps_status_t* alloc_procps_scan(int flags) +{ + procps_status_t* sp = xzalloc(sizeof(procps_status_t)); + sp->dir = xopendir("/proc"); + return sp; +} -procps_status_t * procps_scan(int save_user_arg0) +void free_procps_scan(procps_status_t* sp) { - static DIR *dir; - static procps_status_t ret_status; + closedir(sp->dir); + free(sp->cmd); + free(sp); +} +void BUG_comm_size(void); +procps_status_t* procps_scan(procps_status_t* sp, int flags) +{ struct dirent *entry; - char *name; char buf[PROCPS_BUFSIZE]; - char status[sizeof("/proc//cmdline") + sizeof(int)*3]; - char *status_tail; - procps_status_t curstatus; + char filename[sizeof("/proc//cmdline") + sizeof(int)*3]; + char *filename_tail; long tasknice; - int pid; + unsigned pid; int n; struct stat sb; - if (!dir) { - dir = xopendir("/proc"); - } + if (!sp) + sp = alloc_procps_scan(flags); + for (;;) { - entry = readdir(dir); + entry = readdir(sp->dir); if (entry == NULL) { - closedir(dir); - dir = 0; - return 0; + free_procps_scan(sp); + return NULL; } - name = entry->d_name; - if (!(*name >= '0' && *name <= '9')) + if (safe_strtou(entry->d_name, &pid)) continue; - memset(&curstatus, 0, sizeof(procps_status_t)); - pid = atoi(name); - curstatus.pid = pid; + /* After this point we have to break, not continue + * ("continue" would mean that current /proc/NNN + * is not a valid process info) */ - status_tail = status + sprintf(status, "/proc/%d", pid); - if (stat(status, &sb)) - continue; - bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user)); + memset(&sp->rss, 0, sizeof(*sp) - offsetof(procps_status_t, rss)); - /* see proc(5) for some details on this */ - strcpy(status_tail, "/stat"); - n = read_to_buf(status, buf); - if (n < 0) - continue; - name = strrchr(buf, ')'); /* split into "PID (cmd" and "" */ - if (name == 0 || name[1] != ' ') - continue; - *name = 0; - sscanf(buf, "%*s (%15c", curstatus.short_cmd); - n = sscanf(name+2, - "%c %d " - "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ - "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE - "%lu %lu " /* utime, stime */ -#else - "%*s %*s " /* utime, stime */ -#endif - "%*s %*s %*s " /* cutime, cstime, priority */ - "%ld " /* nice */ - "%*s %*s %*s " /* timeout, it_real_value, start_time */ - "%*s " /* vsize */ - "%ld", /* rss */ - curstatus.state, &curstatus.ppid, -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE - &curstatus.utime, &curstatus.stime, -#endif - &tasknice, - &curstatus.rss); -#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE - if (n != 6) -#else - if (n != 4) -#endif - continue; + sp->pid = pid; + if (!(flags & ~PSSCAN_PID)) break; - if (curstatus.rss == 0 && curstatus.state[0] != 'Z') - curstatus.state[1] = 'W'; - else - curstatus.state[1] = ' '; - if (tasknice < 0) - curstatus.state[2] = '<'; - else if (tasknice > 0) - curstatus.state[2] = 'N'; - else - curstatus.state[2] = ' '; + filename_tail = filename + sprintf(filename, "/proc/%d", pid); + if (flags & PSSCAN_UIDGID) { + if (stat(filename, &sb)) + break; + /* Need comment - is this effective or read UID/GID? */ + sp->uid = sb.st_uid; + sp->gid = sb.st_gid; + } + + if (flags & PSSCAN_STAT) { + char *cp; + /* see proc(5) for some details on this */ + strcpy(filename_tail, "/stat"); + n = read_to_buf(filename, buf); + if (n < 0) + break; + cp = strrchr(buf, ')'); /* split into "PID (cmd" and "" */ + if (!cp || cp[1] != ' ') + break; + cp[0] = '\0'; + if (sizeof(sp->comm) < 16) + BUG_comm_size(); + sscanf(buf, "%*s (%15c", sp->comm); + n = sscanf(cp+2, + "%c %u " /* state, ppid */ + "%u %u %*s %*s " /* pgid, sid, tty, tpgid */ + "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ + "%lu %lu " /* utime, stime */ + "%*s %*s %*s " /* cutime, cstime, priority */ + "%ld " /* nice */ + "%*s %*s %*s " /* timeout, it_real_value, start_time */ + "%*s " /* vsize */ + "%lu", /* rss */ + sp->state, &sp->ppid, + &sp->pgid, &sp->sid, + &sp->utime, &sp->stime, + &tasknice, + &sp->rss); + if (n != 8) + break; + + if (sp->rss == 0 && sp->state[0] != 'Z') + sp->state[1] = 'W'; + else + sp->state[1] = ' '; + if (tasknice < 0) + sp->state[2] = '<'; + else if (tasknice > 0) + sp->state[2] = 'N'; + else + sp->state[2] = ' '; + #ifdef PAGE_SHIFT - curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ + sp->rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ #else - curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */ + sp->rss *= (getpagesize() >> 10); /* 2**10 = 1kb */ #endif + } - if (save_user_arg0) { - strcpy(status_tail, "/cmdline"); - n = read_to_buf(status, buf); - if (n > 0) { - if (buf[n-1]=='\n') - buf[--n] = 0; - name = buf; - while (n) { - if (((unsigned char)*name) < ' ') - *name = ' '; - name++; - n--; - } - *name = 0; - if (buf[0]) - curstatus.cmd = strdup(buf); - /* if NULL it work true also */ + if (flags & PSSCAN_CMD) { + free(sp->cmd); + sp->cmd = NULL; + strcpy(filename_tail, "/cmdline"); + n = read_to_buf(filename, buf); + if (n <= 0) + break; + if (buf[n-1] == '\n') { + if (!--n) + break; + buf[n] = '\0'; } + do { + n--; + if ((unsigned char)(buf[n]) < ' ') + buf[n] = ' '; + } while (n); + sp->cmd = strdup(buf); } - return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); + break; } + return sp; } +/* from kernel: + // pid comm S ppid pgid sid tty_nr tty_pgrp flg + sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \ +%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \ +%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n", + task->pid, + tcomm, + state, + ppid, + pgid, + sid, + tty_nr, + tty_pgrp, + task->flags, + min_flt, + + cmin_flt, + maj_flt, + cmaj_flt, + cputime_to_clock_t(utime), + cputime_to_clock_t(stime), + cputime_to_clock_t(cutime), + cputime_to_clock_t(cstime), + priority, + nice, + num_threads, + // 0, + start_time, + vsize, + mm ? get_mm_rss(mm) : 0, + rsslim, + mm ? mm->start_code : 0, + mm ? mm->end_code : 0, + mm ? mm->start_stack : 0, + esp, + eip, +the rest is some obsolete cruft +*/ Modified: trunk/busybox/procps/kill.c =================================================================== --- trunk/busybox/procps/kill.c 2006-11-05 00:38:51 UTC (rev 16508) +++ trunk/busybox/procps/kill.c 2006-11-05 00:43:51 UTC (rev 16509) @@ -82,7 +82,7 @@ if (killall5) { pid_t sid; - procps_status_t* p; + procps_status_t* p = NULL; // Cannot happen anyway? We don't TERM ourself, we STOP // /* kill(-1, sig) on Linux (at least 2.1.x) @@ -94,8 +94,8 @@ pid = getpid(); sid = getsid(pid); /* Now kill all processes except our session */ - while ((p = procps_scan(0))!=0) { - if (getsid(p->pid)!=sid && p->pid!=pid && p->pid!=1) + while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_SID))) { + if (p->sid != sid && p->pid != pid && p->pid != 1) kill(p->pid, signo); } /* And let them continue */ Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-05 00:38:51 UTC (rev 16508) +++ trunk/busybox/procps/ps.c 2006-11-05 00:43:51 UTC (rev 16509) @@ -11,7 +11,7 @@ int ps_main(int argc, char **argv) { - procps_status_t * p; + procps_status_t *p = NULL; int i, len; SKIP_SELINUX(const) int use_selinux = 0; USE_SELINUX(security_context_t sid = NULL;) @@ -50,7 +50,13 @@ else puts(" PID Uid VmSize Stat Command"); - while ((p = procps_scan(1)) != 0) { + while ((p = procps_scan(p, 0 + | PSSCAN_PID + | PSSCAN_UIDGID + | PSSCAN_STATE + | PSSCAN_RSS + | PSSCAN_CMD + ))) { char *namecmd = p->cmd; #if ENABLE_SELINUX if (use_selinux) { @@ -71,13 +77,18 @@ } else { safe_strncpy(sbuf, "unknown", 7); } - len = printf("%5u %-32s %s ", (unsigned)p->pid, sbuf, p->state); + len = printf("%5u %-32s %s ", p->pid, sbuf, p->state); } else #endif + { + const char *user = get_cached_username(p->uid); if (p->rss == 0) - len = printf("%5u %-8s %s ", (unsigned)p->pid, p->user, p->state); + len = printf("%5u %-8s %s ", + p->pid, user, p->state); else - len = printf("%5u %-8s %6ld %s ", (unsigned)p->pid, p->user, p->rss, p->state); + len = printf("%5u %-8s %6ld %s ", + p->pid, user, p->rss, p->state); + } i = terminal_width-len; @@ -88,16 +99,15 @@ namecmd[i] = 0; puts(namecmd); } else { - namecmd = p->short_cmd; + namecmd = p->comm; if (i < 2) i = 2; if (strlen(namecmd) > ((size_t)i-2)) namecmd[i-2] = 0; printf("[%s]\n", namecmd); } - /* no check needed, but to make valgrind happy.. */ - if (ENABLE_FEATURE_CLEAN_UP && p->cmd) - free(p->cmd); } + if (ENABLE_FEATURE_CLEAN_UP) + clear_username_cache(); return EXIT_SUCCESS; } Modified: trunk/busybox/procps/top.c =================================================================== --- trunk/busybox/procps/top.c 2006-11-05 00:38:51 UTC (rev 16508) +++ trunk/busybox/procps/top.c 2006-11-05 00:43:51 UTC (rev 16509) @@ -30,37 +30,76 @@ #include "busybox.h" -typedef int (*cmp_t)(procps_status_t *P, procps_status_t *Q); -static procps_status_t *top; /* Hehe */ +typedef struct { + unsigned long rss; +#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE + unsigned long ticks; + unsigned pcpu; /* delta of ticks */ +#endif + unsigned pid, ppid; + unsigned uid; + char state[4]; + char comm[COMM_LEN]; +} top_status_t; +static top_status_t *top; static int ntop; +/* This structure stores some critical information from one frame to + the next. Used for finding deltas. */ +struct save_hist { + unsigned long ticks; + unsigned pid; +}; +static struct save_hist *prev_hist; +static int prev_hist_count; +/* static int hist_iterations; */ +static unsigned total_pcpu; +/* static unsigned long total_rss; */ + + #define OPT_BATCH_MODE (option_mask32 & 0x4) #if ENABLE_FEATURE_USE_TERMIOS -static int pid_sort(procps_status_t *P, procps_status_t *Q) +static int pid_sort(top_status_t *P, top_status_t *Q) { + /* Buggy wrt pids with high bit set */ + /* (linux pids are in [1..2^15-1]) */ return (Q->pid - P->pid); } #endif -static int mem_sort(procps_status_t *P, procps_status_t *Q) +static int mem_sort(top_status_t *P, top_status_t *Q) { - return (int)(Q->rss - P->rss); + /* We want to avoid unsigned->signed and truncation errors */ + if (Q->rss < P->rss) return -1; + return Q->rss != P->rss; /* 0 if ==, 1 if > */ } -#if ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE +typedef int (*cmp_funcp)(top_status_t *P, top_status_t *Q); + +#if !ENABLE_FEATURE_TOP_CPU_USAGE_PERCENTAGE + +static cmp_funcp sort_function; + +#else + enum { SORT_DEPTH = 3 }; -static cmp_t sort_function[SORT_DEPTH]; -static int pcpu_sort(procps_status_t *P, procps_status_t *Q) +static cmp_funcp sort_function[SORT_DEPTH]; + +static int pcpu_sort(top_status_t *P, top_status_t *Q) { - return (Q->pcpu - P->pcpu); + /* Buggy wrt ticks with high bit set */ + /* Affects only processes for which ticks overflow */ + return (int)Q->pcpu - (int)P->pcpu; } -static int time_sort(procps_status_t *P, procps_status_t *Q) +static int time_sort(top_status_t *P, top_status_t *Q) { - return (int)((Q->stime + Q->utime) - (P->stime + P->utime)); + /* We want to avoid unsigned->signed and truncation errors */ + if (Q->ticks < P->ticks) return -1; + return Q->ticks != P->ticks; /* 0 if ==, 1 if > */ } static int mult_lvl_cmp(void* a, void* b) { @@ -74,25 +113,7 @@ return 0; } -/* This structure stores some critical information from one frame to - the next. Mostly used for sorting. */ -struct save_hist { - int ticks; - pid_t pid; -}; -/* - * Calculates percent cpu usage for each task. - */ - -static struct save_hist *prev_hist; -static int prev_hist_count; -/* static int hist_iterations; */ - - -static unsigned total_pcpu; -/* static unsigned long total_rss; */ - typedef struct { unsigned long long usr,nic,sys,idle,iowait,irq,softirq,steal; unsigned long long total; @@ -115,11 +136,12 @@ jif.busy = jif.total - jif.idle - jif.iowait; } + static void do_stats(void) { - procps_status_t *cur; + top_status_t *cur; pid_t pid; - int total_time, i, last_i, n; + int i, last_i, n; struct save_hist *new_hist; get_jiffy_counts(); @@ -139,8 +161,7 @@ * and system time */ pid = cur->pid; - total_time = cur->stime + cur->utime; - new_hist[n].ticks = total_time; + new_hist[n].ticks = cur->ticks; new_hist[n].pid = pid; /* find matching entry from previous pass */ @@ -150,13 +171,13 @@ last_i = i; if (prev_hist_count) do { if (prev_hist[i].pid == pid) { - cur->pcpu = total_time - prev_hist[i].ticks; + cur->pcpu = cur->ticks - prev_hist[i].ticks; + total_pcpu += cur->pcpu; break; } i = (i+1) % prev_hist_count; /* hist_iterations++; */ } while (i != last_i); - total_pcpu += cur->pcpu; /* total_rss += cur->rss; */ } @@ -167,10 +188,9 @@ prev_hist = new_hist; prev_hist_count = ntop; } -#else -static cmp_t sort_function; #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ + /* display generic info (meminfo / loadavg) */ static unsigned long display_generic(int scr_width) { @@ -265,7 +285,7 @@ bits_per_int = sizeof(int)*8 }; - procps_status_t *s = top; + top_status_t *s = top; char rss_str_buf[8]; unsigned long total_memory = display_generic(scr_width); /* or use total_rss? */ unsigned pmem_shift, pmem_scale; @@ -333,14 +353,19 @@ sprintf(rss_str_buf, "%6ldM", s->rss/1024); else sprintf(rss_str_buf, "%7ld", s->rss); - USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10);) - col -= printf("\n%5u %-8s %s %s%6u%3u.%c" \ - USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") " ", - (unsigned)s->pid, s->user, s->state, rss_str_buf, (unsigned)s->ppid, + USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE( + pcpu = div((s->pcpu*pcpu_scale) >> pcpu_shift, 10); + ) + col -= printf("\n%5u %-8s %s " + "%s%6u" + USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE("%3u.%c") + "%3u.%c ", + s->pid, get_cached_username(s->uid), s->state, + rss_str_buf, s->ppid, USE_FEATURE_TOP_CPU_USAGE_PERCENTAGE(pcpu.quot, '0'+pcpu.rem,) pmem.quot, '0'+pmem.rem); if (col > 0) - printf("%.*s", col, s->short_cmd); + printf("%.*s", col, s->comm); /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, jif.busy - prev_jif.busy, jif.total - prev_jif.total); */ s++; @@ -350,18 +375,20 @@ fflush(stdout); } + static void clearmems(void) { + clear_username_cache(); free(top); top = 0; ntop = 0; } + #if ENABLE_FEATURE_USE_TERMIOS #include #include - static struct termios initial_settings; static void reset_term(void) @@ -427,7 +454,7 @@ #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ while (1) { - procps_status_t *p; + procps_status_t *p = NULL; /* Default to 25 lines - 5 lines for status */ lines = 24 - 3; @@ -442,11 +469,26 @@ #endif /* FEATURE_USE_TERMIOS */ /* read process IDs & status for all the processes */ - while ((p = procps_scan(0)) != 0) { + while ((p = procps_scan(p, 0 + | PSSCAN_PID + | PSSCAN_PPID + | PSSCAN_RSS + | PSSCAN_STIME + | PSSCAN_UTIME + | PSSCAN_STATE + | PSSCAN_COMM + | PSSCAN_SID + | PSSCAN_UIDGID + ))) { int n = ntop; - - top = xrealloc(top, (++ntop)*sizeof(procps_status_t)); - memcpy(top + n, p, sizeof(procps_status_t)); + top = xrealloc(top, (++ntop)*sizeof(top_status_t)); + top[n].pid = p->pid; + top[n].ppid = p->ppid; + top[n].rss = p->rss; + top[n].ticks = p->stime + p->utime; + top[n].uid = p->uid; + strcpy(top[n].state, p->state); + strcpy(top[n].comm, p->comm); } if (ntop == 0) { bb_error_msg_and_die("can't find process info in /proc"); @@ -459,9 +501,9 @@ continue; } do_stats(); - qsort(top, ntop, sizeof(procps_status_t), (void*)mult_lvl_cmp); + qsort(top, ntop, sizeof(top_status_t), (void*)mult_lvl_cmp); #else - qsort(top, ntop, sizeof(procps_status_t), (void*)sort_function); + qsort(top, ntop, sizeof(top_status_t), (void*)sort_function); #endif /* FEATURE_TOP_CPU_USAGE_PERCENTAGE */ count = lines; if (OPT_BATCH_MODE || count > ntop) { From vda at busybox.net Sat Nov 4 16:44:40 2006 From: vda at busybox.net (vda at busybox.net) Date: Sat, 4 Nov 2006 16:44:40 -0800 (PST) Subject: svn commit: trunk/busybox: include libbb miscutils Message-ID: <20061105004440.1CBA548609@busybox.net> Author: vda Date: 2006-11-04 16:44:39 -0800 (Sat, 04 Nov 2006) New Revision: 16510 Log: smart_ulltoa5: make available in libbb Modified: trunk/busybox/include/libbb.h trunk/busybox/libbb/xfuncs.c trunk/busybox/miscutils/nmeter.c Changeset: Modified: trunk/busybox/include/libbb.h =================================================================== --- trunk/busybox/include/libbb.h 2006-11-05 00:43:51 UTC (rev 16509) +++ trunk/busybox/include/libbb.h 2006-11-05 00:44:39 UTC (rev 16510) @@ -278,6 +278,7 @@ extern FILE *fopen_or_warn_stdin(const char *filename); +extern void smart_ulltoa5(unsigned long long ul, char buf[5]); extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen); extern char *utoa(unsigned n); extern void itoa_to_buf(int n, char *buf, unsigned buflen); Modified: trunk/busybox/libbb/xfuncs.c =================================================================== --- trunk/busybox/libbb/xfuncs.c 2006-11-05 00:43:51 UTC (rev 16509) +++ trunk/busybox/libbb/xfuncs.c 2006-11-05 00:44:39 UTC (rev 16510) @@ -201,23 +201,71 @@ bb_error_msg_and_die(bb_msg_memory_exhausted); } + +// Converts unsigned long long value into compact 4-char +// representation. Examples: "1234", "1.2k", " 27M", "123T" +// Fifth char is always '\0' +void smart_ulltoa5(unsigned long long ul, char buf[5]) +{ + char *fmt; + char c; + unsigned v,idx = 0; + ul *= 10; + if (ul > 9999*10) { // do not scale if 9999 or less + while (ul >= 10000) { + ul /= 1024; + idx++; + } + } + v = ul; // ullong divisions are expensive, avoid them + + fmt = " 123456789"; + if (!idx) { // 9999 or less: use 1234 format + c = buf[0] = " 123456789"[v/10000]; + if (c!=' ') fmt = "0123456789"; + c = buf[1] = fmt[v/1000%10]; + if (c!=' ') fmt = "0123456789"; + buf[2] = fmt[v/100%10]; + buf[3] = "0123456789"[v/10%10]; + } else { + if (v>=10*10) { // scaled value is >=10: use 123M format + c = buf[0] = " 123456789"[v/1000]; + if (c!=' ') fmt = "0123456789"; + buf[1] = fmt[v/100%10]; + buf[2] = "0123456789"[v/10%10]; + } else { // scaled value is <10: use 1.2M format + buf[0] = "0123456789"[v/10]; + buf[1] = '.'; + buf[2] = "0123456789"[v%10]; + } + // see http://en.wikipedia.org/wiki/Tera + buf[3] = " kMGTPEZY"[idx]; + } + buf[4] = '\0'; +} + + // Convert unsigned integer to ascii, writing into supplied buffer. A // truncated result is always null terminated (unless buflen is 0), and // contains the first few digits of the result ala strncpy. +void BUG_sizeof_unsigned_not_4(void); void utoa_to_buf(unsigned n, char *buf, unsigned buflen) { - int i, out = 0; + unsigned i, out, res; + if (sizeof(unsigned) != 4) + BUG_sizeof_unsigned_not_4(); if (buflen) { - for (i=1000000000; i; i/=10) { - int res = n/i; - - if ((res || out || i == 1) && --buflen>0) { + out = 0; + for (i = 1000000000; i; i /= 10) { + res = n / i; + if (res || out || i == 1) { + if (!--buflen) break; out++; n -= res*i; *buf++ = '0' + res; } } - *buf = 0; + *buf = '\0'; } } Modified: trunk/busybox/miscutils/nmeter.c =================================================================== --- trunk/busybox/miscutils/nmeter.c 2006-11-05 00:43:51 UTC (rev 16509) +++ trunk/busybox/miscutils/nmeter.c 2006-11-05 00:44:39 UTC (rev 16510) @@ -214,42 +214,8 @@ static void scale(ullong ul) { - char *fmt; char buf[5]; - char c; - unsigned v,idx = 0; - ul *= 10; - if (ul > 9999*10) { // do not scale if 9999 or less - while (ul >= 10000) { - ul /= 1024; - idx++; - } - } - v = ul; // ullong divisions are expensive, avoid them - - fmt = " 123456789"; - if (!idx) { // 9999 or less: use 1234 format - c = buf[0] = " 123456789"[v/10000]; - if (c!=' ') fmt = "0123456789"; - c = buf[1] = fmt[v/1000%10]; - if (c!=' ') fmt = "0123456789"; - buf[2] = fmt[v/100%10]; - buf[3] = "0123456789"[v/10%10]; - } else { - if (v>=10*10) { // scaled value is >=10: use 123M format - c = buf[0] = " 123456789"[v/1000]; - if (c!=' ') fmt = "0123456789"; - buf[1] = fmt[v/100%10]; - buf[2] = "0123456789"[v/10%10]; - } else { // scaled value is <10: use 1.2M format - buf[0] = "0123456789"[v/10]; - buf[1] = '.'; - buf[2] = "0123456789"[v%10]; - } - // see http://en.wikipedia.org/wiki/Tera - buf[3] = " kMGTPEZY"[idx]; - } - buf[4] = '\0'; + smart_ulltoa5(ul, buf); put(buf); } From vda at busybox.net Sat Nov 4 16:45:47 2006 From: vda at busybox.net (vda at busybox.net) Date: Sat, 4 Nov 2006 16:45:47 -0800 (PST) Subject: svn commit: trunk/busybox: include procps Message-ID: <20061105004547.9BFE54860A@busybox.net> Author: vda Date: 2006-11-04 16:45:47 -0800 (Sat, 04 Nov 2006) New Revision: 16511 Log: ps: implement POSIX-like options, most notably -o (activated by CONFIG_DESKTOP) Modified: trunk/busybox/include/usage.h trunk/busybox/procps/ps.c Changeset: Modified: trunk/busybox/include/usage.h =================================================================== --- trunk/busybox/include/usage.h 2006-11-05 00:44:39 UTC (rev 16510) +++ trunk/busybox/include/usage.h 2006-11-05 00:45:47 UTC (rev 16511) @@ -2377,6 +2377,18 @@ "$ printf \"Val=%d\\n\" 5\n" \ "Val=5\n" + +#if ENABLE_DESKTOP + +#define ps_trivial_usage \ + "" +#define ps_full_usage \ + "Report process status\n" \ + "\nOptions:" \ + "\n\t-o col1,col2=header\tSelect columns for display" \ + +#else /* !ENABLE_DESKTOP */ + #if !defined CONFIG_SELINUX && !ENABLE_FEATURE_PS_WIDE #define USAGE_PS "\n\tThis version of ps accepts no options." #else @@ -2396,6 +2408,8 @@ USE_SELINUX("\n\t-c\tshow SE Linux context") \ USAGE_PS_WIDE("\n\tw\twide output") +#endif /* ENABLE_DESKTOP */ + #define ps_example_usage \ "$ ps\n" \ " PID Uid Gid State Command\n" \ @@ -2409,6 +2423,7 @@ " 745 root root S [getty]\n" \ " 2990 andersen andersen R ps\n" + #define pwd_trivial_usage \ "" #define pwd_full_usage \ Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-05 00:44:39 UTC (rev 16510) +++ trunk/busybox/procps/ps.c 2006-11-05 00:45:47 UTC (rev 16511) @@ -9,8 +9,281 @@ #include "busybox.h" +#if ENABLE_DESKTOP + +/* Print value to buf, max size+1 chars (including trailing '\0') */ + +void func_user(char *buf, int size, const procps_status_t *ps) +{ + safe_strncpy(buf, get_cached_username(ps->uid), size+1); +} + +void func_comm(char *buf, int size, const procps_status_t *ps) +{ + safe_strncpy(buf, ps->comm, size+1); +} + +void func_args(char *buf, int size, const procps_status_t *ps) +{ + buf[0] = '\0'; + if (ps->cmd) + safe_strncpy(buf, ps->cmd, size+1); + else if (size >= 2) + snprintf(buf, size+1, "[%.*s]", size-2, ps->comm); +} + +void func_pid(char *buf, int size, const procps_status_t *ps) +{ + snprintf(buf, size+1, "%*u", size, ps->pid); +} + +void func_ppid(char *buf, int size, const procps_status_t *ps) +{ + snprintf(buf, size+1, "%*u", size, ps->ppid); +} + +void func_pgid(char *buf, int size, const procps_status_t *ps) +{ + snprintf(buf, size+1, "%*u", size, ps->pgid); +} + +void func_rss(char *buf, int size, const procps_status_t *ps) +{ + char buf5[5]; + smart_ulltoa5( ((unsigned long long)ps->rss) << 10, buf5); + snprintf(buf, size+1, "%.*s", size, buf5); +} + +/* +void func_nice(char *buf, int size, const procps_status_t *ps) +{ + ps->??? +} + +void func_etime(char *buf, int size, const procps_status_t *ps) +{ + elapled time [[dd-]hh:]mm:ss +} + +void func_time(char *buf, int size, const procps_status_t *ps) +{ + cumulative time [[dd-]hh:]mm:ss +} + +void func_pcpu(char *buf, int size, const procps_status_t *ps) +{ +} + +void func_tty(char *buf, int size, const procps_status_t *ps) +{ +} +*/ + +typedef struct { + char name[8]; + const char *header; + void (*f)(char *buf, int size, const procps_status_t *ps); + int ps_flags; + int width; +} ps_out_t; + +static const ps_out_t out_spec[] = { +// Mandated by POSIX: + { "user" ,"USER" ,func_user ,PSSCAN_UIDGID,8 }, + { "comm" ,"COMMAND",func_comm ,PSSCAN_COMM ,16 }, + { "args" ,"COMMAND",func_args ,PSSCAN_CMD|PSSCAN_COMM,256 }, + { "pid" ,"PID" ,func_pid ,PSSCAN_PID ,5 }, + { "ppid" ,"PPID" ,func_ppid ,PSSCAN_PPID ,5 }, + { "pgid" ,"PGID" ,func_pgid ,PSSCAN_PGID ,5 }, +// { "etime" ,"ELAPSED",func_etime ,PSSCAN_ ,sizeof("ELAPSED")-1 }, +// { "group" ,"GROUP" ,func_group ,PSSCAN_UIDGID,sizeof("GROUP" )-1 }, +// { "nice" ,"NI" ,func_nice ,PSSCAN_ ,sizeof("NI" )-1 }, +// { "pcpu" ,"%CPU" ,func_pcpu ,PSSCAN_ ,sizeof("%CPU" )-1 }, +// { "rgroup","RGROUP" ,func_rgroup,PSSCAN_UIDGID,sizeof("RGROUP" )-1 }, +// { "ruser" ,"RUSER" ,func_ruser ,PSSCAN_UIDGID,sizeof("RUSER" )-1 }, +// { "time" ,"TIME" ,func_time ,PSSCAN_ ,sizeof("TIME" )-1 }, +// { "tty" ,"TT" ,func_tty ,PSSCAN_ ,sizeof("TT" )-1 }, +// { "vsz" ,"VSZ" ,func_vsz ,PSSCAN_VSZ ,4 }, +// Not mandated by POSIX: + { "rss" ,"RSS" ,func_rss ,PSSCAN_RSS ,4 }, +}; + +#define VEC_SIZE(v) ( sizeof(v) / sizeof((v)[0]) ) + +static ps_out_t* out; +static int out_cnt; +static int print_header; +static int ps_flags; +static char *buffer; +static unsigned terminal_width; + + +static ps_out_t* new_out_t(void) +{ + int i = out_cnt++; + out = xrealloc(out, out_cnt * sizeof(*out)); + return &out[i]; +} + +static const ps_out_t* find_out_spec(const char *name) +{ + int i; + for (i = 0; i < VEC_SIZE(out_spec); i++) { + if (!strcmp(name, out_spec[i].name)) + return &out_spec[i]; + } + bb_error_msg_and_die("bad -o argument '%s'", name); +} + +static void parse_o(char* opt) +{ + ps_out_t* new; + // POSIX: "-o is blank- or comma-separated list" (FIXME) + char *comma, *equal; + while (1) { + comma = strchr(opt, ','); + equal = strchr(opt, '='); + if (comma && (!equal || equal > comma)) { + *comma = '\0'; + *new_out_t() = *find_out_spec(opt); + *comma = ','; + opt = comma + 1; + continue; + } + break; + } + new = new_out_t(); + if (equal) + *equal = '\0'; + *new = *find_out_spec(opt); + if (equal) { + *equal = '='; + new->header = equal + 1; + // POSIX: the field widths shall be ... at least as wide as + // the header text (default or overridden value). + // If the header text is null, such as -o user=, + // the field width shall be at least as wide as the + // default header text + if (new->header[0]) { + new->width = strlen(new->header); + print_header = 1; + } + } else + print_header = 1; +} + +static void post_process(void) +{ + int i; + int width = 0; + for (i = 0; i < out_cnt; i++) { + ps_flags |= out[i].ps_flags; + if (out[i].header[0]) { + print_header = 1; + } + width += out[i].width + 1; /* "FIELD " */ + } + buffer = xmalloc(width + 1); /* for trailing \0 */ +} + +static void format_header(void) +{ + int i; + ps_out_t* op; + char *p = buffer; + if (!print_header) + return; + i = 0; + if (out_cnt) { + while (1) { + op = &out[i]; + if (++i == out_cnt) /* do not pad last field */ + break; + p += sprintf(p, "%-*s ", op->width, op->header); + } + strcpy(p, op->header); + } + printf("%.*s\n", terminal_width, buffer); +} + +static void format_process(const procps_status_t *ps) +{ + int i, len; + char *p = buffer; + i = 0; + if (out_cnt) while (1) { + out[i].f(p, out[i].width, ps); + // POSIX: Any field need not be meaningful in all + // implementations. In such a case a hyphen ( '-' ) + // should be output in place of the field value. + if (!*p) { + *p++ = '-'; + *p = '\0'; + } + len = strlen(p); + p += len; + len = out[i].width - len + 1; + if (++i == out_cnt) /* do not pad last field */ + break; + while (len--) + *p++ = ' '; + *p = '\0'; + } + printf("%.*s\n", terminal_width, buffer); +} + +/* Cannot be const: parse_o() will choke */ +static char default_o[] = "pid,user" /* TODO: ,vsz,stat */ ",args"; + int ps_main(int argc, char **argv) { + procps_status_t *p; + llist_t* opt_o = NULL; + + // POSIX: + // -a Write information for all processes associated with terminals + // Implementations may omit session leaders from this list + // -A Write information for all processes + // -d Write information for all processes, except session leaders + // -e Write information for all processes (equivalent to -A.) + // -f Generate a full listing + // -l Generate a long listing + // -o col1,col2,col3=header + // Select which columns to distplay + /* We allow (and ignore) most of the above. FIXME */ + opt_complementary = "o::"; + getopt32(argc, argv, "o:aAdefl", &opt_o); + if (opt_o) { + opt_o = rev_llist(opt_o); + do { + parse_o(opt_o->data); + opt_o = opt_o->link; + } while (opt_o); + } else + parse_o(default_o); + post_process(); + + terminal_width = INT_MAX; + if (isatty(1)) { + get_terminal_width_height(1, &terminal_width, NULL); + terminal_width--; + } + format_header(); + + p = NULL; + while ((p = procps_scan(p, ps_flags))) { + format_process(p); + } + + return EXIT_SUCCESS; +} + + +#else /* !ENABLE_DESKTOP */ + + +int ps_main(int argc, char **argv) +{ procps_status_t *p = NULL; int i, len; SKIP_SELINUX(const) int use_selinux = 0; @@ -111,3 +384,5 @@ clear_username_cache(); return EXIT_SUCCESS; } + +#endif From vda at busybox.net Sat Nov 4 16:46:36 2006 From: vda at busybox.net (vda at busybox.net) Date: Sat, 4 Nov 2006 16:46:36 -0800 (PST) Subject: svn commit: trunk/busybox/include Message-ID: <20061105004636.5FF5248607@busybox.net> Author: vda Date: 2006-11-04 16:46:35 -0800 (Sat, 04 Nov 2006) New Revision: 16512 Log: usage.h: stop using ancient USAGE_xxx (we have USE_xx now) Modified: trunk/busybox/include/usage.h Changeset: Modified: trunk/busybox/include/usage.h =================================================================== --- trunk/busybox/include/usage.h 2006-11-05 00:45:47 UTC (rev 16511) +++ trunk/busybox/include/usage.h 2006-11-05 00:46:35 UTC (rev 16512) @@ -2268,16 +2268,6 @@ "$ patch -p1 < example.diff\n" \ "$ patch -p0 -i example.diff" -#if ENABLE_FEATURE_PIDOF_SINGLE -#define USAGE_FEATURE_PIDOF_SINGLE(a) a -#else -#define USAGE_FEATURE_PIDOF_SINGLE(a) -#endif -#if ENABLE_FEATURE_PIDOF_OMIT -#define USAGE_FEATURE_PIDOF_OMIT(a) a -#else -#define USAGE_FEATURE_PIDOF_OMIT(a) -#endif #if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT) #define USAGE_PIDOF "Options:" #else @@ -2291,14 +2281,14 @@ "Lists the PIDs of all processes with names that match the\n" \ "names on the command line.\n" \ USAGE_PIDOF \ - USAGE_FEATURE_PIDOF_SINGLE("\n\t-s\t\tdisplay only a single PID") \ - USAGE_FEATURE_PIDOF_OMIT("\n\t-o\t\tomit given pid.") \ - USAGE_FEATURE_PIDOF_OMIT("\n\t\t\tUse %PPID to omit the parent pid of pidof itself") + USE_FEATURE_PIDOF_SINGLE("\n\t-s\t\tdisplay only a single PID") \ + USE_FEATURE_PIDOF_OMIT("\n\t-o\t\tomit given pid.") \ + USE_FEATURE_PIDOF_OMIT("\n\t\t\tUse %PPID to omit the parent pid of pidof itself") #define pidof_example_usage \ "$ pidof init\n" \ "1\n" \ - USAGE_FEATURE_PIDOF_OMIT("$ pidof /bin/sh\n20351 5973 5950\n") \ - USAGE_FEATURE_PIDOF_OMIT("$ pidof /bin/sh -o %PPID\n20351 5950") + USE_FEATURE_PIDOF_OMIT("$ pidof /bin/sh\n20351 5973 5950\n") \ + USE_FEATURE_PIDOF_OMIT("$ pidof /bin/sh -o %PPID\n20351 5950") #ifndef CONFIG_FEATURE_FANCY_PING #define ping_trivial_usage "host" @@ -2394,11 +2384,6 @@ #else #define USAGE_PS "\nOptions:" #endif -#if ENABLE_FEATURE_PS_WIDE -#define USAGE_PS_WIDE(a) a -#else -#define USAGE_PS_WIDE(a) -#endif #define ps_trivial_usage \ "" @@ -2406,7 +2391,7 @@ "Report process status\n" \ USAGE_PS \ USE_SELINUX("\n\t-c\tshow SE Linux context") \ - USAGE_PS_WIDE("\n\tw\twide output") + USE_FEATURE_PS_WIDE("\n\tw\twide output") #endif /* ENABLE_DESKTOP */ @@ -2693,18 +2678,12 @@ USE_FEATURE_FANCY_SLEEP("$ sleep 1d 3h 22m 8s\n" \ "[98528 second delay results]\n") -#if ENABLE_FEATURE_SORT_BIG -# define USAGE_SORT_BIG(a) a -#else -# define USAGE_SORT_BIG(a) -#endif - #define sort_trivial_usage \ - "[-nru" USAGE_SORT_BIG("gMcszbdfimSTokt] [-o outfile] [-k start[.offset][opts][,end[.offset][opts]] [-t char") "] [FILE]..." + "[-nru" USE_FEATURE_SORT_BIG("gMcszbdfimSTokt] [-o outfile] [-k start[.offset][opts][,end[.offset][opts]] [-t char") "] [FILE]..." #define sort_full_usage \ "Sorts lines of text in the specified files\n\n" \ "Options:\n" \ - USAGE_SORT_BIG( \ + USE_FEATURE_SORT_BIG( \ "\t-b\tignore leading blanks\n" \ "\t-c\tcheck whether input is sorted\n" \ "\t-d\tdictionary order (blank or alphanumeric only)\n" \ @@ -2715,16 +2694,16 @@ "\t-M\tsort month\n" \ ) \ "\t-n\tsort numbers\n" \ - USAGE_SORT_BIG( \ + USE_FEATURE_SORT_BIG( \ "\t-o\toutput to file\n" \ "\t-k\tsort by key\n" \ "\t-t\tuse key separator other than whitespace\n" \ ) \ "\t-r\treverse sort order\n" \ - USAGE_SORT_BIG("\t-s\tstable (don't sort ties alphabetically)\n") \ + USE_FEATURE_SORT_BIG("\t-s\tstable (don't sort ties alphabetically)\n") \ "\t-u\tsuppress duplicate lines" \ - USAGE_SORT_BIG("\n\t-z\tinput terminated by nulls, not newlines\n") \ - USAGE_SORT_BIG("\t-mST\tignored for GNU compatibility") \ + USE_FEATURE_SORT_BIG("\n\t-z\tinput terminated by nulls, not newlines\n") \ + USE_FEATURE_SORT_BIG("\t-mST\tignored for GNU compatibility") \ "" #define sort_example_usage \ "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n" \ @@ -2734,7 +2713,7 @@ "d\n" \ "e\n" \ "f\n" \ - USAGE_SORT_BIG( \ + USE_FEATURE_SORT_BIG( \ "$ echo -e \"c 3\\nb 2\\nd 2\" | $SORT -k 2,2n -k 1,1r\n" \ "d 2\n" \ "b 2\n" \ From vda at busybox.net Sat Nov 4 16:47:38 2006 From: vda at busybox.net (vda at busybox.net) Date: Sat, 4 Nov 2006 16:47:38 -0800 (PST) Subject: svn commit: trunk/busybox/procps Message-ID: <20061105004738.ADD0548609@busybox.net> Author: vda Date: 2006-11-04 16:47:38 -0800 (Sat, 04 Nov 2006) New Revision: 16513 Log: ps: add documentation on POSIX ps Added: trunk/busybox/procps/ps.posix Modified: trunk/busybox/procps/ps.c Changeset: Modified: trunk/busybox/procps/ps.c =================================================================== --- trunk/busybox/procps/ps.c 2006-11-05 00:46:35 UTC (rev 16512) +++ trunk/busybox/procps/ps.c 2006-11-05 00:47:38 UTC (rev 16513) @@ -385,4 +385,4 @@ return EXIT_SUCCESS; } -#endif +#endif /* ENABLE_DESKTOP */ Added: trunk/busybox/procps/ps.posix =================================================================== --- trunk/busybox/procps/ps.posix 2006-11-05 00:46:35 UTC (rev 16512) +++ trunk/busybox/procps/ps.posix 2006-11-05 00:47:38 UTC (rev 16513) @@ -0,0 +1,175 @@ +This is what POSIX 2003 says about ps: + +By default, ps shall select all processes with the same effective user +ID as the current user and the same controlling terminal as the invoker + +ps [-aA][-defl][-G grouplist][-o format]...[-p proclist][-t termlist] +[-U userlist][-g grouplist][-n namelist][-u userlist] + +-a Write information for all processes associated with terminals. + Implementations may omit session leaders from this list. + +-A Write information for all processes. + +-d Write information for all processes, except session leaders. + +-e Write information for all processes. (Equivalent to -A.) + +-f Generate a full listing. (See the STDOUT section for the con- + tents of a full listing.) + +-g grouplist + Write information for processes whose session leaders are given + in grouplist. The application shall ensure that the grouplist is + a single argument in the form of a or comma-separated + list. + +-G grouplist + Write information for processes whose real group ID numbers are + given in grouplist. The application shall ensure that the grou- + plist is a single argument in the form of a or comma- + separated list. + +-l Generate a long listing. (See STDOUT for the contents of a long + listing.) + +-n namelist + Specify the name of an alternative system namelist file in place + of the default. The name of the default file and the format of a + namelist file are unspecified. + +-o format + Write information according to the format specification given in + format. Multiple -o options can be specified; the format speci- + fication shall be interpreted as the -separated concate- + nation of all the format option-arguments. + +-p proclist + Write information for processes whose process ID numbers are + given in proclist. The application shall ensure that the pro- + clist is a single argument in the form of a or comma- + separated list. + +-t termlist + Write information for processes associated with terminals given + in termlist. The application shall ensure that the termlist is a + single argument in the form of a or comma-separated + list. Terminal identifiers shall be given in an implementation- + defined format. On XSI-conformant systems, they shall be + given in one of two forms: the device's filename (for example, + tty04) or, if the device's filename starts with tty, just the + identifier following the characters tty (for example, "04" ). + +-u userlist + Write information for processes whose user ID numbers or login + names are given in userlist. The application shall ensure that + the userlist is a single argument in the form of a or + comma-separated list. In the listing, the numerical user ID + shall be written unless the -f option is used, in which case the + login name shall be written. + +-U userlist + Write information for processes whose real user ID numbers or + login names are given in userlist. The application shall ensure + that the userlist is a single argument in the form of a + or comma-separated list. + +With the exception of -o format, all of the options shown are used to +select processes. If any are specified, the default list shall be +ignored and ps shall select the processes represented by the inclusive +OR of all the selection-criteria options. + +The -o option allows the output format to be specified under user con- +trol. + +The application shall ensure that the format specification is a list of +names presented as a single argument, or comma-separated. Each +variable has a default header. The default header can be overridden by +appending an equals sign and the new text of the header. The rest of +the characters in the argument shall be used as the header text. The +fields specified shall be written in the order specified on the command +line, and should be arranged in columns in the output. The field widths +shall be selected by the system to be at least as wide as the header +text (default or overridden value). If the header text is null, such as +-o user=, the field width shall be at least as wide as the default +header text. If all header text fields are null, no header line shall +be written. + +ruser The real user ID of the process. This shall be the textual user + ID, if it can be obtained and the field width permits, or a dec- + imal representation otherwise. + +user The effective user ID of the process. This shall be the textual + user ID, if it can be obtained and the field width permits, or a + decimal representation otherwise. + +rgroup The real group ID of the process. This shall be the textual + group ID, if it can be obtained and the field width permits, or + a decimal representation otherwise. + +group The effective group ID of the process. This shall be the textual + group ID, if it can be obtained and the field width permits, or + a decimal representation otherwise. + +pid The decimal value of the process ID. + +ppid The decimal value of the parent process ID. + +pgid The decimal value of the process group ID. + +pcpu The ratio of CPU time used recently to CPU time available in the + same period, expressed as a percentage. The meaning of + "recently" in this context is unspecified. The CPU time avail- + able is determined in an unspecified manner. + +vsz The size of the process in (virtual) memory in 1024 byte units + as a decimal integer. + +nice The decimal value of the nice value of the process; see nice() . + +etime In the POSIX locale, the elapsed time since the process was + started, in the form: [[dd-]hh:]mm:ss + +time In the POSIX locale, the cumulative CPU time of the process in + the form: [dd-]hh:mm:ss + +tty The name of the controlling terminal of the process (if any) in + the same format used by the who utility. + +comm The name of the command being executed ( argv[0] value) as a + string. + +args The command with all its arguments as a string. The implementa- + tion may truncate this value to the field width; it is implemen- + tation-defined whether any further truncation occurs. It is + unspecified whether the string represented is a version of the + argument list as it was passed to the command when it started, + or is a version of the arguments as they may have been modified + by the application. Applications cannot depend on being able to + modify their argument list and having that modification be + reflected in the output of ps. + +Any field need not be meaningful in all implementations. In such a case +a hyphen ( '-' ) should be output in place of the field value. + +Only comm and args shall be allowed to contain s; all others +shall not. + +The following table specifies the default header to be used in the +POSIX locale corresponding to each format specifier. + + Format Specifier Default Header Format Specifier Default Header + args COMMAND ppid PPID + comm COMMAND rgroup RGROUP + etime ELAPSED ruser RUSER + group GROUP time TIME + nice NI tty TT + pcpu %CPU user USER + pgid PGID vsz VSZ + pid PID + +There is no special quoting mechanism for header text. The header text +is the rest of the argument. If multiple header changes are needed, +multiple -o options can be used, such as: + + ps -o "user=User Name" -o pid=Process\ ID From vda at busybox.net Sun Nov 5 10:05:11 2006 From: vda at busybox.net (vda at busybox.net) Date: Sun, 5 Nov 2006 10:05:11 -0800 (PST) Subject: svn commit: trunk/busybox: archival e2fsprogs include libbb miscuti etc... Message-ID: <20061105180511.BA68E48663@busybox.net> Author: vda Date: 2006-11-05 10:05:09 -0800 (Sun, 05 Nov 2006) New Revision: 16515 Log: rename: compare_string_array -> index_in_str_array introduce index_in_substr_array and use it in iproute2 Modified: trunk/busybox/archival/dpkg.c trunk/busybox/e2fsprogs/fsck.c trunk/busybox/include/libbb.h trunk/busybox/libbb/compare_string_array.c trunk/busybox/miscutils/devfsd.c trunk/busybox/networking/ip.c trunk/busybox/networking/libiproute/ipaddress.c trunk/busybox/networking/libiproute/iproute.c trunk/busybox/networking/libiproute/utils.c trunk/busybox/util-linux/mount.c Changeset: Modified: trunk/busybox/archival/dpkg.c =================================================================== --- trunk/busybox/archival/dpkg.c 2006-11-05 11:21:34 UTC (rev 16514) +++ trunk/busybox/archival/dpkg.c 2006-11-05 18:05:09 UTC (rev 16515) @@ -644,7 +644,7 @@ goto fill_package_struct_cleanup; /* Oh no, the dreaded goto statement ! */ } - field_num = compare_string_array(field_names, field_name); + field_num = index_in_str_array(field_names, field_name); switch (field_num) { case 0: /* Package */ new_node->name = search_name_hashtable(field_value); Modified: trunk/busybox/e2fsprogs/fsck.c =================================================================== --- trunk/busybox/e2fsprogs/fsck.c 2006-11-05 11:21:34 UTC (rev 16514) +++ trunk/busybox/e2fsprogs/fsck.c 2006-11-05 18:05:09 UTC (rev 16515) @@ -996,11 +996,11 @@ if (!fs_match(fs, &fs_type_compiled)) return 1; /* Are we ignoring this type? */ - if(compare_string_array(ignored_types, fs->type) >= 0) + if (index_in_str_array(ignored_types, fs->type) >= 0) return 1; /* Do we really really want to check this fs? */ - wanted = compare_string_array(really_wanted, fs->type) >= 0; + wanted = index_in_str_array(really_wanted, fs->type) >= 0; /* See if the program is available. */ s = find_fsck(fs->type); Modified: trunk/busybox/include/libbb.h =================================================================== --- trunk/busybox/include/libbb.h 2006-11-05 11:21:34 UTC (rev 16514) +++ trunk/busybox/include/libbb.h 2006-11-05 18:05:09 UTC (rev 16515) @@ -477,7 +477,8 @@ extern int correct_password(const struct passwd *pw); extern char *pw_encrypt(const char *clear, const char *salt); extern int obscure(const char *old, const char *newval, const struct passwd *pwdp); -extern int compare_string_array(const char * const string_array[], const char *key); +extern int index_in_str_array(const char * const string_array[], const char *key); +extern int index_in_substr_array(const char * const string_array[], const char *key); extern void print_login_issue(const char *issue_file, const char *tty); extern void print_login_prompt(void); #ifdef BB_NOMMU Modified: trunk/busybox/libbb/compare_string_array.c =================================================================== --- trunk/busybox/libbb/compare_string_array.c 2006-11-05 11:21:34 UTC (rev 16514) +++ trunk/busybox/libbb/compare_string_array.c 2006-11-05 18:05:09 UTC (rev 16515) @@ -3,11 +3,11 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include #include "libbb.h" -/* returns the array number of the string */ -int compare_string_array(const char * const string_array[], const char *key) +/* returns the array index of the string */ +/* (index of first match is returned, or -1) */ +int index_in_str_array(const char * const string_array[], const char *key) { int i; @@ -16,6 +16,22 @@ return i; } } - return -i; + return -1; } +/* returns the array index of the string, even if it matches only a beginning */ +/* (index of first match is returned, or -1) */ +int index_in_substr_array(const char * const string_array[], const char *key) +{ + int i; + int len = strlen(key); + if (!len) + return -1; + + for (i = 0; string_array[i] != 0; i++) { + if (strncmp(string_array[i], key, len) == 0) { + return i; + } + } + return -1; +} Modified: trunk/busybox/miscutils/devfsd.c =================================================================== --- trunk/busybox/miscutils/devfsd.c 2006-11-05 11:21:34 UTC (rev 16514) +++ trunk/busybox/miscutils/devfsd.c 2006-11-05 18:05:09 UTC (rev 16515) @@ -627,7 +627,7 @@ when, name, what, p[0], p[1], p[2], p[3], p[4], p[5], p[6]); - i = compare_string_array(options, when ); + i = index_in_str_array(options, when ); /*"CLEAR_CONFIG"*/ if( i == 0) @@ -673,7 +673,7 @@ goto process_config_line_err; } - i = compare_string_array(options, what ); + i = index_in_str_array(options, what ); switch(i) { @@ -1313,8 +1313,8 @@ /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */ hostname[STRING_LENGTH - 1] = '\0'; - /* compare_string_array returns i>=0 */ - i=compare_string_array(field_names, variable); + /* index_in_str_array returns i>=0 */ + i=index_in_str_array(field_names, variable); if ( i > 6 || i < 0 || (i > 1 && gv_info == NULL)) return (NULL); Modified: trunk/busybox/networking/ip.c =================================================================== --- trunk/busybox/networking/ip.c 2006-11-05 11:21:34 UTC (rev 16514) +++ trunk/busybox/networking/ip.c 2006-11-05 18:05:09 UTC (rev 16515) @@ -12,20 +12,11 @@ * Rani Assaf 980929: resolve addresses */ -#include -#include -#include -#include -#include -#include -#include -#include +#include "busybox.h" #include "libiproute/utils.h" #include "libiproute/ip_common.h" -#include "busybox.h" - int ip_main(int argc, char **argv) { int ret = EXIT_FAILURE; @@ -57,5 +48,5 @@ if (ret) { bb_show_usage(); } - return(EXIT_SUCCESS); + return EXIT_SUCCESS; } Modified: trunk/busybox/networking/libiproute/ipaddress.c =================================================================== --- trunk/busybox/networking/libiproute/ipaddress.c 2006-11-05 11:21:34 UTC (rev 16514) +++ trunk/busybox/networking/libiproute/ipaddress.c 2006-11-05 18:05:09 UTC (rev 16515) @@ -441,7 +441,7 @@ } while (argc > 0) { - const int option_num = compare_string_array(option, *argv); + const int option_num = index_in_str_array(option, *argv); switch (option_num) { case 0: /* to */ NEXT_ARG(); @@ -653,7 +653,7 @@ req.ifa.ifa_family = preferred_family; while (argc > 0) { - const int option_num = compare_string_array(option, *argv); + const int option_num = index_in_str_array(option, *argv); switch (option_num) { case 0: /* peer */ case 1: /* remote */ @@ -800,25 +800,24 @@ int do_ipaddr(int argc, char **argv) { static const char *const commands[] = { - "add", "del", "delete", "list", "show", "lst", "flush", 0 + "add", "delete", "list", "show", "lst", "flush", 0 }; int command_num = 2; if (*argv) { - command_num = compare_string_array(commands, *argv); + command_num = index_in_substr_array(commands, *argv); } switch (command_num) { case 0: /* add */ return ipaddr_modify(R