Skip to content

Commit

Permalink
ps: drop dependency on Android's libprocessgroup.
Browse files Browse the repository at this point in the history
This dependency keeps getting hairier and hairier (json!) and the
current system for policy scheduling is actually quite different from
the original anyway.
  • Loading branch information
enh-google authored and landley committed Aug 29, 2024
1 parent db76625 commit ab9caa5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
14 changes: 0 additions & 14 deletions lib/portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,6 @@ static inline int stub_out_log_write(int pri, const char *tag, const char *msg)

#endif

// libprocessgroup is an Android platform library not included in the NDK.
#if defined(__BIONIC__)
#if __has_include(<processgroup/sched_policy.h>)
#include <processgroup/sched_policy.h>
#define GOT_IT
#endif
#endif
#ifdef GOT_IT
#undef GOT_IT
#else
static inline int get_sched_policy(int tid, void *policy) {return 0;}
static inline char *get_sched_policy_name(int policy) {return "unknown";}
#endif

#ifndef SYSLOG_NAMES
typedef struct {char *c_name; int c_val;} CODE;
extern CODE prioritynames[], facilitynames[];
Expand Down
33 changes: 28 additions & 5 deletions toys/posix/ps.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ struct procpid {
long long slot[SLOT_count]; // data (see enum above)
unsigned short offset[6]; // offset of fields in str[] (skip CMD, always 0)
char state;
char pcy[3]; // Android scheduling policy
char str[]; // CMD, TTY, WCHAN, LABEL, COMM, ARGS, NAME
};

Expand Down Expand Up @@ -632,7 +633,7 @@ static char *string_field(struct procpid *tb, struct ofields *field)
out = out+strlen(out)-3-abs(field->len);
if (out<buf) out = buf;

} else if (which==PS_PCY) sprintf(out, "%.2s", get_sched_policy_name(ll));
} else if (which==PS_PCY) sprintf(out, "%.2s", tb->pcy);
else if (CFG_TOYBOX_DEBUG) error_exit("bad which %d", which);

return out;
Expand Down Expand Up @@ -721,6 +722,7 @@ static int get_ps(struct dirtree *new)
struct procpid *tb = (void *)toybuf;
long long *slot = tb->slot;
char *name, *s, *buf = tb->str, *end = 0;
FILE *fp;
struct sysinfo si;
int i, j, fd;
off_t len;
Expand Down Expand Up @@ -852,8 +854,30 @@ static int get_ps(struct dirtree *new)
}

// Do we need Android scheduling policy?
if (TT.bits&_PS_PCY)
get_sched_policy(slot[SLOT_tid], (void *)&slot[SLOT_pcy]);
if (TT.bits&_PS_PCY) {
// Find the cpuset line in "/proc/$pid/cgroup", extract the final field,
// and translate it to one of Android's traditional 2-char names.
// TODO: if other Linux systems start using cgroups, conditionalize this.
sprintf(buf, "/proc/%lld/cgroup", slot[SLOT_tid]);
if ((fp = fopen(buf, "re"))) {
char *s, *line;
while ((line = xgetline(fp))) {
if ((s = strstr(line, ":cpuset:/"))) {
s += strlen(":cpuset:/");
if (!*s || !strcmp(s, "foreground")) strcpy(tb->pcy, "fg");
else if (!strcmp(s, "system-background")) strcpy(tb->pcy, " ");
else if (!strcmp(s, "background")) strcpy(tb->pcy, "bg");
else if (!strcmp(s, "top-app")) strcpy(tb->pcy, "ta");
else if (!strcmp(s, "restricted")) strcpy(tb->pcy, "rs");
else if (!strcmp(s, "foreground-window")) strcpy(tb->pcy, "wi");
else if (!strcmp(s, "camera-daemon")) strcpy(tb->pcy, "cd");
else strcpy(tb->pcy, "?");
}
free(line);
}
fclose(fp);
} else strcpy(tb->pcy, "-");
}

// Done using buf[] (tb->str) as scratch space, now read string data,
// saving consective null terminated strings. (Save starting offsets into
Expand Down Expand Up @@ -929,10 +953,9 @@ static int get_ps(struct dirtree *new)

// Couldn't find it, try all the tty drivers.
if (i == 3) {
FILE *fp = fopen("/proc/tty/drivers", "r");
int tty_major = 0, maj = dev_major(rdev), min = dev_minor(rdev);

if (fp) {
if ((fp = fopen("/proc/tty/drivers", "r"))) {
while (fscanf(fp, "%*s %256s %d %*s %*s", buf, &tty_major) == 2) {
// TODO: we could parse the minor range too.
if (tty_major == maj) {
Expand Down

0 comments on commit ab9caa5

Please sign in to comment.