/** * Lookup which process owns the destination port sepcified * in dpkt->dp. */ void lookup_proc(char *buffer, size_t s, struct dispkt *dpkt) { char *net_file; DIR *proc_dir; FILE *fp; struct dirent *de; unsigned long inode = 0, pid; char linebuf[256], *endptr; if (!buffer || !dpkt) return; *buffer = '\0'; if (dpkt->ip_proto == IPPROTO_TCP) { if (dpkt->family == AF_INET6) net_file = "/proc/net/tcp6"; else net_file = "/proc/net/tcp"; } else { if (dpkt->family == AF_INET6) net_file = "/proc/net/udp6"; else net_file = "/proc/net/udp"; } /* Open the file from /proc/net and try to find the inode */ if (!(fp = fopen(net_file, "r"))) return; while (fgets(linebuf, sizeof(linebuf) - 1, fp)) { inode = get_inode_for_dest(linebuf, dpkt); if (inode > 0) break; } fclose(fp); if (!inode) return; /* Now that we have the inode, we can probe for the process */ if (!(proc_dir = opendir("/proc"))) return; while (!*buffer && (de = readdir(proc_dir))) { /* Make sure we actually have a base-10 numeric string */ pid = strtoul(de->d_name, &endptr, 10); if (*endptr != '\0') continue; process_pid(buffer, s, pid, inode); } closedir(proc_dir); }
void luaopen_lev_process(lua_State *L) { luaL_newmetatable(L, "lev.process"); luaL_register(L, NULL, methods); lua_setfield(L, -1, "__index"); lua_createtable(L, 0, ARRAY_SIZE(functions) + PROPERTY_COUNT - 1); luaL_register(L, NULL, functions); /* properties */ process_pid(L); lua_setfield(L, -2, "pid"); process_title(L); lua_setfield(L, -2, "title"); lua_setfield(L, -2, "process"); }