Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

external-toybox: Commit

external/toybox


Commit MetaInfo

Revision371dfd41efca697e72832cdf5db61ffd4c7e7a98 (tree)
Time2018-02-11 01:34:42
AuthorRob Landley <rob@land...>
CommiterRob Landley

Log Message

Rename struct strawberry -> struct ofields, and carveup -> procpid.

The first contains display fields selectable by the -o argument,
the second contains the /proc/$PID data for one process.

Change Summary

Incremental Difference

--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -286,21 +286,20 @@ GLOBALS(
286286 void (*show_process)(void *tb);
287287 )
288288
289-/* Linked list of fields selected for display, in order, with :len and =title */
289+// Linked list of -o fields selected for display, in order, with :len and =title
290290
291-struct strawberry {
292- struct strawberry *next, *prev;
291+struct ofields {
292+ struct ofields *next, *prev;
293293 short which, len, reverse;
294294 char *title;
295- char forever[];
296295 };
297296
298297 /* The function get_ps() reads all the data about one process, saving it in
299- * toybox as a struct carveup. Simple ps calls then pass toybuf directly to
300- * show_ps(), but features like sorting instead append a copy to a linked list
298+ * toybox as a struct procpid. Simple ps calls then pass toybuf directly to
299+ * show_ps(), but features like sorting append a copy to a linked list
301300 * for further processing once all processes have been read.
302301 *
303- * struct carveup contains a slot[] array of 64 bit values, with the following
302+ * struct procpid contains a slot[] array of 64 bit values, with the following
304303 * data at each position in the array. Most is read from /proc/$PID/stat (see
305304 * https://kernel.org/doc/Documentation/filesystems/proc.txt table 1-4) but
306305 * we we replace several fields with don't use with other data. */
@@ -346,7 +345,7 @@ enum {
346345 starting position of each string after the first (which is always 0). */
347346
348347 // Data layout in toybuf
349-struct carveup {
348+struct procpid {
350349 long long slot[SLOT_count]; // data (see enum above)
351350 unsigned short offset[6]; // offset of fields in str[] (skip CMD, always 0)
352351 char state;
@@ -364,7 +363,7 @@ struct carveup {
364363 * the display code reclaims unused padding from later fields to try to
365364 * get the overflow back).
366365 *
367- * slot: which slot[] out of carveup. Negative means it's a string field.
366+ * slot: which slot[] out of procpid. Negative means it's a string field.
368367 * Setting bit |64 requests extra display/sort processing.
369368 *
370369 * The TAGGED_ARRAY plumbing produces an enum of indexes, the "tag" is the
@@ -391,7 +390,7 @@ struct typography {
391390 {"RTPRIO", 6, SLOT_rtprio}, {"SCH", 3, SLOT_policy}, {"CPU", 3, SLOT_taskcpu},
392391 {"TID", 5, SLOT_tid}, {"TCNT", 4, SLOT_tcount}, {"BIT", 3, SLOT_bits},
393392
394- // String fields (-1 is carveup->str, rest are str+offset[1-slot])
393+ // String fields (-1 is procpid->str, rest are str+offset[1-slot])
395394 {"TTY", -8, -2}, {"WCHAN", -6, -3}, {"LABEL", -30, -4}, {"COMM", -27, -5},
396395 {"NAME", -27, -7}, {"COMMAND", -27, -5}, {"CMDLINE", -27, -6},
397396 {"ARGS", -27, -6}, {"CMD", -15, -1},
@@ -464,7 +463,7 @@ static int ps_match_process(long long *slot)
464463 }
465464
466465 // Convert field to string representation
467-static char *string_field(struct carveup *tb, struct strawberry *field)
466+static char *string_field(struct procpid *tb, struct ofields *field)
468467 {
469468 char *buf = toybuf+sizeof(toybuf)-260, *out = buf, *s;
470469 int which = field->which, sl = typos[which].slot;
@@ -593,8 +592,8 @@ static char *string_field(struct carveup *tb, struct strawberry *field)
593592 // Display process data that get_ps() read from /proc, formatting with TT.fields
594593 static void show_ps(void *p)
595594 {
596- struct carveup *tb = p;
597- struct strawberry *field;
595+ struct procpid *tb = p;
596+ struct ofields *field;
598597 int pad, len, width = TT.width, abslen, sign, olen, extra = 0;
599598
600599 // Loop through fields to display
@@ -654,7 +653,7 @@ static void show_ps(void *p)
654653 }
655654
656655 // dirtree callback: read data about process to display, store, or discard it.
657-// Fills toybuf with struct carveup and either DIRTREE_SAVEs a copy to ->extra
656+// Fills toybuf with struct procpid and either DIRTREE_SAVEs a copy to ->extra
658657 // (in -k mode) or calls show_ps on toybuf (no malloc/copy/free there).
659658 static int get_ps(struct dirtree *new)
660659 {
@@ -662,12 +661,12 @@ static int get_ps(struct dirtree *new)
662661 char *name; // Path under /proc/$PID directory
663662 long long bits; // Only fetch extra data if an -o field is displaying it
664663 } fetch[] = {
665- // sources for carveup->offset[] data
664+ // sources for procpid->offset[] data
666665 {"fd/", _PS_TTY}, {"wchan", _PS_WCHAN}, {"attr/current", _PS_LABEL},
667666 {"exe", _PS_COMMAND|_PS_COMM}, {"cmdline", _PS_CMDLINE|_PS_ARGS|_PS_NAME},
668667 {"", _PS_NAME}
669668 };
670- struct carveup *tb = (void *)toybuf;
669+ struct procpid *tb = (void *)toybuf;
671670 long long *slot = tb->slot;
672671 char *name, *s, *buf = tb->str, *end = 0;
673672 int i, j, fd;
@@ -681,7 +680,7 @@ static int get_ps(struct dirtree *new)
681680 memset(slot, 0, sizeof(tb->slot));
682681 tb->slot[SLOT_tid] = *slot = atol(new->name);
683682 if (TT.threadparent && TT.threadparent->extra)
684- if (*slot == *(((struct carveup *)TT.threadparent->extra)->slot)) return 0;
683+ if (*slot == *(((struct procpid *)TT.threadparent->extra)->slot)) return 0;
685684 fd = dirtree_parentfd(new);
686685
687686 len = 2048;
@@ -804,7 +803,7 @@ static int get_ps(struct dirtree *new)
804803
805804 // For exe we readlink instead of read contents
806805 if (j==3 || j==5) {
807- struct carveup *ptb = 0;
806+ struct procpid *ptb = 0;
808807 int k;
809808
810809 // Thread doesn't have exe or argv[0], so use parent's
@@ -925,7 +924,7 @@ static int get_ps(struct dirtree *new)
925924 static int get_threads(struct dirtree *new)
926925 {
927926 struct dirtree *dt;
928- struct carveup *tb;
927+ struct procpid *tb;
929928 unsigned pid, kcount;
930929
931930 if (!new->parent) return get_ps(new);
@@ -975,7 +974,7 @@ static int get_threads(struct dirtree *new)
975974
976975 static char *parse_ko(void *data, char *type, int length)
977976 {
978- struct strawberry *field;
977+ struct ofields *field;
979978 char *width, *title, *end, *s;
980979 int i, j, k;
981980
@@ -995,10 +994,11 @@ static char *parse_ko(void *data, char *type, int length)
995994 if (!title) length = width-type;
996995 } else width = 0;
997996
998- // Allocate structure, copy title
999- field = xzalloc(sizeof(struct strawberry)+(length+1)*!!title);
997+ // Allocate structure plus extra space to append a copy of title data
998+ // (this way it's same lifetime, freeing struct automatically frees title)
999+ field = xzalloc(sizeof(struct ofields)+(length+1)*!!title);
10001000 if (title) {
1001- memcpy(field->title = field->forever, title, length);
1001+ memcpy(field->title = (char *)(field+1), title, length);
10021002 field->title[field->len = length] = 0;
10031003 }
10041004
@@ -1036,15 +1036,15 @@ static char *parse_ko(void *data, char *type, int length)
10361036 return 0;
10371037 }
10381038
1039-static long long get_headers(struct strawberry *fields, char *buf, int blen)
1039+static long long get_headers(struct ofields *field, char *buf, int blen)
10401040 {
10411041 long long bits = 0;
10421042 int len = 0;
10431043
1044- for (; fields; fields = fields->next) {
1045- len += snprintf(buf+len, blen-len, " %*s"+!bits, fields->len,
1046- fields->title);
1047- bits |= 1LL<<fields->which;
1044+ for (; field; field = field->next) {
1045+ len += snprintf(buf+len, blen-len, " %*s"+!bits, field->len,
1046+ field->title);
1047+ bits |= 1LL<<field->which;
10481048 }
10491049
10501050 return bits;
@@ -1131,8 +1131,8 @@ static char *parse_rest(void *data, char *str, int len)
11311131 // sort for -k
11321132 static int ksort(void *aa, void *bb)
11331133 {
1134- struct strawberry *field;
1135- struct carveup *ta = *(struct carveup **)aa, *tb = *(struct carveup **)bb;
1134+ struct ofields *field;
1135+ struct procpid *ta = *(struct procpid **)aa, *tb = *(struct procpid **)bb;
11361136 int ret = 0, slot;
11371137
11381138 for (field = TT.kfields; field && !ret; field = field->next) {
@@ -1156,7 +1156,7 @@ static int ksort(void *aa, void *bb)
11561156 return ret;
11571157 }
11581158
1159-static struct carveup **collate_leaves(struct carveup **tb, struct dirtree *dt)
1159+static struct procpid **collate_leaves(struct procpid **tb, struct dirtree *dt)
11601160 {
11611161 while (dt) {
11621162 struct dirtree *next = dt->next;
@@ -1170,9 +1170,9 @@ static struct carveup **collate_leaves(struct carveup **tb, struct dirtree *dt)
11701170 return tb;
11711171 }
11721172
1173-static struct carveup **collate(int count, struct dirtree *dt)
1173+static struct procpid **collate(int count, struct dirtree *dt)
11741174 {
1175- struct carveup **tbsort = xmalloc(count*sizeof(struct carveup *));
1175+ struct procpid **tbsort = xmalloc(count*sizeof(struct procpid *));
11761176
11771177 collate_leaves(tbsort, dt);
11781178
@@ -1244,20 +1244,20 @@ void ps_main(void)
12441244 default_ko(toybuf, &TT.fields, "bad -o", TT.ps.o);
12451245
12461246 if (TT.ps.O) {
1247- if (TT.fields) TT.fields = ((struct strawberry *)TT.fields)->prev;
1247+ if (TT.fields) TT.fields = ((struct ofields *)TT.fields)->prev;
12481248 comma_args(TT.ps.O, &TT.fields, "bad -O", parse_ko);
1249- if (TT.fields) TT.fields = ((struct strawberry *)TT.fields)->next;
1249+ if (TT.fields) TT.fields = ((struct ofields *)TT.fields)->next;
12501250 }
12511251 dlist_terminate(TT.fields);
12521252
12531253 // -f and -n change the meaning of some fields
12541254 if (toys.optflags&(FLAG_f|FLAG_n)) {
1255- struct strawberry *ever;
1255+ struct ofields *field;
12561256
1257- for (ever = TT.fields; ever; ever = ever->next) {
1258- if ((toys.optflags&FLAG_n) && ever->which>=PS_UID
1259- && ever->which<=PS_RGROUP && (typos[ever->which].slot&64))
1260- ever->which--;
1257+ for (field = TT.fields; field; field = field->next) {
1258+ if ((toys.optflags&FLAG_n) && field->which>=PS_UID
1259+ && field->which<=PS_RGROUP && (typos[field->which].slot&64))
1260+ field->which--;
12611261 }
12621262 }
12631263
@@ -1272,11 +1272,11 @@ void ps_main(void)
12721272 ? get_threads : get_ps);
12731273
12741274 if ((dt != DIRTREE_ABORTVAL) && toys.optflags&(FLAG_k|FLAG_M)) {
1275- struct carveup **tbsort = collate(TT.kcount, dt);
1275+ struct procpid **tbsort = collate(TT.kcount, dt);
12761276
12771277 if (toys.optflags&FLAG_M) {
12781278 for (i = 0; i<TT.kcount; i++) {
1279- struct strawberry *field;
1279+ struct ofields *field;
12801280
12811281 for (field = TT.fields; field; field = field->next) {
12821282 int len = strlen(string_field(tbsort[i], field));
@@ -1291,7 +1291,7 @@ void ps_main(void)
12911291 }
12921292
12931293 if (toys.optflags&FLAG_k)
1294- qsort(tbsort, TT.kcount, sizeof(struct carveup *), (void *)ksort);
1294+ qsort(tbsort, TT.kcount, sizeof(struct procpid *), (void *)ksort);
12951295 for (i = 0; i<TT.kcount; i++) {
12961296 show_ps(tbsort[i]);
12971297 free(tbsort[i]);
@@ -1319,16 +1319,16 @@ void ps_main(void)
13191319 // select which of the -o fields to sort by
13201320 static void setsort(int pos)
13211321 {
1322- struct strawberry *field, *going2;
1322+ struct ofields *field, *field2;
13231323 int i = 0;
13241324
13251325 if (pos<0) pos = 0;
13261326
13271327 for (field = TT.fields; field; field = field->next) {
13281328 if ((TT.sortpos = i++)<pos && field->next) continue;
1329- going2 = TT.kfields;
1330- going2->which = field->which;
1331- going2->len = field->len;
1329+ field2 = TT.kfields;
1330+ field2->which = field->which;
1331+ field2->len = field->len;
13321332 break;
13331333 }
13341334 }
@@ -1367,7 +1367,7 @@ static void top_common(
13671367 {
13681368 long long timeout = 0, now, stats[16];
13691369 struct proclist {
1370- struct carveup **tb;
1370+ struct procpid **tb;
13711371 int count;
13721372 long long whence;
13731373 } plist[2], *plold, *plnew, old, new, mix;
@@ -1413,11 +1413,11 @@ static void top_common(
14131413 // Collate old and new into "mix", depends on /proc read in pid sort order
14141414 old = *plold;
14151415 new = *plnew;
1416- mix.tb = xmalloc((old.count+new.count)*sizeof(struct carveup));
1416+ mix.tb = xmalloc((old.count+new.count)*sizeof(struct procpid));
14171417 mix.count = 0;
14181418
14191419 while (old.count || new.count) {
1420- struct carveup *otb = old.count ? *old.tb : 0,
1420+ struct procpid *otb = old.count ? *old.tb : 0,
14211421 *ntb = new.count ? *new.tb : 0;
14221422
14231423 // If we just have old for this process, it exited. Discard it.
@@ -1448,7 +1448,7 @@ static void top_common(
14481448 char was, is;
14491449
14501450 if (recalc) {
1451- qsort(mix.tb, mix.count, sizeof(struct carveup *), (void *)ksort);
1451+ qsort(mix.tb, mix.count, sizeof(struct procpid *), (void *)ksort);
14521452 if (!(toys.optflags&FLAG_b)) {
14531453 printf("\033[H\033[J");
14541454 if (toys.signal) {
@@ -1462,16 +1462,16 @@ static void top_common(
14621462 if (recalc && !(toys.optflags&FLAG_q)) {
14631463 // Display "top" header.
14641464 if (*toys.which->name == 't') {
1465- struct strawberry alluc;
1465+ struct ofields field;
14661466 long long ll, up = 0;
14671467 long run[6];
14681468 int j;
14691469
14701470 // Count running, sleeping, stopped, zombie processes.
1471- alluc.which = PS_S;
1471+ field.which = PS_S;
14721472 memset(run, 0, sizeof(run));
14731473 for (i = 0; i<mix.count; i++)
1474- run[1+stridx("RSTZ", *string_field(mix.tb[i], &alluc))]++;
1474+ run[1+stridx("RSTZ", *string_field(mix.tb[i], &field))]++;
14751475 sprintf(toybuf,
14761476 "Tasks: %d total,%4ld running,%4ld sleeping,%4ld stopped,"
14771477 "%4ld zombie", mix.count, run[1], run[2], run[3], run[4]);
@@ -1513,24 +1513,24 @@ static void top_common(
15131513 }
15141514 lines = header_line(lines, 0);
15151515 } else {
1516- struct strawberry *fields;
1517- struct carveup tb;
1516+ struct ofields *field;
1517+ struct procpid tb;
15181518
1519- memset(&tb, 0, sizeof(struct carveup));
1519+ memset(&tb, 0, sizeof(struct procpid));
15201520 pos = stpcpy(toybuf, "Totals:");
1521- for (fields = TT.fields; fields; fields = fields->next) {
1521+ for (field = TT.fields; field; field = field->next) {
15221522 long long ll, bits = 0;
1523- int slot = typos[fields->which].slot&63;
1523+ int slot = typos[field->which].slot&63;
15241524
1525- if (fields->which<PS_C || fields->which>PS_DIO) continue;
1526- ll = 1LL<<fields->which;
1525+ if (field->which<PS_C || field->which>PS_DIO) continue;
1526+ ll = 1LL<<field->which;
15271527 if (bits&ll) continue;
15281528 bits |= ll;
15291529 for (i=0; i<mix.count; i++)
15301530 tb.slot[slot] += mix.tb[i]->slot[slot];
15311531 pos += snprintf(pos, sizeof(toybuf)/2-(pos-toybuf),
1532- " %s: %*s,", typos[fields->which].name,
1533- fields->len, string_field(&tb, fields));
1532+ " %s: %*s,", typos[field->which].name,
1533+ field->len, string_field(&tb, field));
15341534 }
15351535 *--pos = 0;
15361536 lines = header_line(lines, 0);
@@ -1587,7 +1587,7 @@ static void top_common(
15871587 timeout = 0;
15881588 break;
15891589 } else if (toupper(i)=='R')
1590- ((struct strawberry *)TT.kfields)->reverse *= -1;
1590+ ((struct ofields *)TT.kfields)->reverse *= -1;
15911591 else {
15921592 i -= 256;
15931593 if (i == KEY_LEFT) setsort(TT.sortpos-1);
@@ -1658,10 +1658,10 @@ void top_main(void)
16581658 if (!TT.top.s) TT.top.s = TT.top.O ? 3 : 9;
16591659 top_setup(toybuf, "-%CPU,-ETIME,-PID");
16601660 if (TT.top.O) {
1661- struct strawberry *fields = TT.fields;
1661+ struct ofields *field = TT.fields;
16621662
1663- fields = fields->next->next;
1664- comma_args(TT.top.O, &fields, "bad -O", parse_ko);
1663+ field = field->next->next;
1664+ comma_args(TT.top.O, &field, "bad -O", parse_ko);
16651665 }
16661666
16671667 top_common(merge_deltas);
@@ -1705,7 +1705,7 @@ struct regex_list {
17051705 regex_t reg;
17061706 };
17071707
1708-static void do_pgk(struct carveup *tb)
1708+static void do_pgk(struct procpid *tb)
17091709 {
17101710 if (TT.pgrep.signal) {
17111711 if (kill(*tb->slot, TT.pgrep.signal)) {
@@ -1726,7 +1726,7 @@ static void do_pgk(struct carveup *tb)
17261726
17271727 static void match_pgrep(void *p)
17281728 {
1729- struct carveup *tb = p;
1729+ struct procpid *tb = p;
17301730 regmatch_t match;
17311731 struct regex_list *reg;
17321732 char *name = tb->str+tb->offset[4]*!!(toys.optflags&FLAG_f);;
Show on old repository browser