+
+#define PROGNAME_WIDTH 20
+#define PROGNAME_WIDTH_STR "20"
+/* PROGNAME_WIDTH chars: 12345678901234567890 */
+#define PROGNAME_BANNER "PID/Program name "
+
+struct prg_node {
+ struct prg_node *next;
+ long inode;
+ char name[PROGNAME_WIDTH];
+};
+
+#define PRG_HASH_SIZE 211
+
+
+struct globals {
+ const char *net_conn_line;
+ smallint flags;
+#if ENABLE_FEATURE_NETSTAT_PRG
+ smallint prg_cache_loaded;
+ struct prg_node *prg_hash[PRG_HASH_SIZE];
+#endif
+};
+#define G (*ptr_to_globals)
+#define flags (G.flags )
+#define net_conn_line (G.net_conn_line )
+#define prg_hash (G.prg_hash )
+#define prg_cache_loaded (G.prg_cache_loaded)
+#define INIT_G() do { \
+ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+ flags = NETSTAT_CONNECTED | NETSTAT_ALLPROTO; \
+ net_conn_line = PRINT_NET_CONN; \
+} while (0)
+
+
+#if ENABLE_FEATURE_NETSTAT_PRG
+
+/* Deliberately truncating long to unsigned *int* */
+#define PRG_HASHIT(x) ((unsigned)(x) % PRG_HASH_SIZE)
+
+#define print_progname_banner() do { \
+ if (option_mask32 & OPT_prg) printf(PROGNAME_BANNER); \
+} while (0)
+
+static void prg_cache_add(long inode, char *name)
+{
+ unsigned hi = PRG_HASHIT(inode);
+ struct prg_node **pnp, *pn;
+
+ prg_cache_loaded = 2;
+ for (pnp = prg_hash + hi; (pn = *pnp) != NULL; pnp = &pn->next) {
+ if (pn->inode == inode) {
+ /* Some warning should be appropriate here
+ as we got multiple processes for one i-node */
+ return;
+ }
+ }
+ *pnp = xzalloc(sizeof(struct prg_node));
+ pn = *pnp;
+ pn->inode = inode;
+ safe_strncpy(pn->name, name, PROGNAME_WIDTH);
+}
+
+static const char *prg_cache_get(long inode)
+{
+ unsigned hi = PRG_HASHIT(inode);
+ struct prg_node *pn;
+
+ for (pn = prg_hash[hi]; pn; pn = pn->next)
+ if (pn->inode == inode)
+ return pn->name;
+ return "-";
+}
+
+#if ENABLE_FEATURE_CLEAN_UP
+static void prg_cache_clear(void)
+{
+ struct prg_node **pnp, *pn;
+
+ for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
+ while ((pn = *pnp) != NULL) {
+ *pnp = pn->next;
+ free(pn);
+ }
+ }
+}
+#else
+#define prg_cache_clear() ((void)0)
+#endif
+
+static long extract_socket_inode(const char *lname)
+{
+ long inode = -1;
+
+ if (strncmp(lname, "socket:[", sizeof("socket:[")-1) == 0) {
+ /* "socket:[12345]", extract the "12345" as inode */
+ inode = bb_strtol(lname + sizeof("socket:[")-1, (char**)&lname, 0);
+ if (*lname != ']')
+ inode = -1;
+ } else if (strncmp(lname, "[0000]:", sizeof("[0000]:")-1) == 0) {
+ /* "[0000]:12345", extract the "12345" as inode */
+ inode = bb_strtol(lname + sizeof("[0000]:")-1, NULL, 0);
+ if (errno) /* not NUL terminated? */
+ inode = -1;
+ }
+
+#if 0 /* bb_strtol returns all-ones bit pattern on ERANGE anyway */
+ if (errno == ERANGE)
+ inode = -1;
+#endif
+ return inode;
+}
+
+static int FAST_FUNC file_act(const char *fileName,
+ struct stat *statbuf UNUSED_PARAM,
+ void *userData,
+ int depth UNUSED_PARAM)
+{
+ char *linkname;
+ long inode;
+
+ linkname = xmalloc_readlink(fileName);
+ if (linkname != NULL) {
+ inode = extract_socket_inode(linkname);
+ free(linkname);
+ if (inode >= 0)
+ prg_cache_add(inode, (char *)userData);
+ }
+ return TRUE;
+}
+
+static int FAST_FUNC dir_act(const char *fileName,
+ struct stat *statbuf UNUSED_PARAM,
+ void *userData UNUSED_PARAM,
+ int depth)
+{
+ const char *shortName;
+ char *p, *q;
+ char cmdline_buf[512];
+ int i;
+
+ if (depth == 0) /* "/proc" itself */
+ return TRUE; /* continue looking one level below /proc */
+
+ shortName = fileName + sizeof("/proc/")-1; /* point after "/proc/" */
+ if (!isdigit(shortName[0])) /* skip /proc entries whic aren't processes */
+ return SKIP;
+
+ p = concat_path_file(fileName, "cmdline"); /* "/proc/PID/cmdline" */
+ i = open_read_close(p, cmdline_buf, sizeof(cmdline_buf) - 1);
+ free(p);
+ if (i < 0)
+ return FALSE;
+ cmdline_buf[i] = '\0';
+ q = concat_path_file(shortName, bb_basename(cmdline_buf)); /* "PID/argv0" */
+
+ /* go through all files in /proc/PID/fd */
+ p = concat_path_file(fileName, "fd");
+ i = recursive_action(p, ACTION_RECURSE | ACTION_QUIET,
+ file_act, NULL, (void *)q, 0);
+
+ free(p);
+ free(q);
+
+ if (!i)
+ return FALSE; /* signal permissions error to caller */
+
+ return SKIP; /* caller should not recurse further into this dir. */
+}
+
+static void prg_cache_load(void)
+{
+ int load_ok;
+
+ prg_cache_loaded = 1;
+ load_ok = recursive_action("/proc", ACTION_RECURSE | ACTION_QUIET,
+ NULL, dir_act, NULL, 0);
+ if (load_ok)
+ return;
+
+ if (prg_cache_loaded == 1)
+ bb_error_msg("can't scan /proc - are you root?");
+ else
+ bb_error_msg("showing only processes with your user ID");
+}
+
+#else
+
+#define prg_cache_clear() ((void)0)
+#define print_progname_banner() ((void)0)
+
+#endif //ENABLE_FEATURE_NETSTAT_PRG