(Discord chat excerpt from 2023/06/14 at 14:19) - tux-linux ok, this is nonsense ---- FILE ---- build@inkbox /tmp> ./kill_process /proc/./cmdline /proc/../cmdline /proc/fb/cmdline /proc/fs/cmdline /proc/bus/cmdline /proc/dma/cmdline /proc/irq/cmdline /proc/net/cmdline /proc/sys/cmdline /proc/tty/cmdline /proc/xen/cmdline /proc/acpi/cmdline /proc/keys/cmdline /proc/kmsg/cmdline /proc/misc/cmdline /proc/mtrr/cmdline /proc/stat/cmdline /proc/iomem/cmdline /proc/kcore/cmdline /proc/locks/cmdline /proc/swaps/cmdline /proc/crypto/cmdline /proc/driver/cmdline /proc/mounts/cmdline /proc/uptime/cmdline /proc/vmstat/cmdline /proc/cgroups/cmdline /proc/cmdline/cmdline /proc/cpuinfo/cmdline /proc/devices/cmdline /proc/ioports/cmdline /proc/loadavg/cmdline /proc/meminfo/cmdline /proc/modules/cmdline /proc/sysvipc/cmdline /proc/version/cmdline /proc/consoles/cmdline /proc/kallsyms/cmdline /proc/pressure/cmdline /proc/slabinfo/cmdline /proc/softirqs/cmdline /proc/zoneinfo/cmdline /proc/buddyinfo/cmdline /proc/diskstats/cmdline /proc/key-users/cmdline /proc/schedstat/cmdline /proc/interrupts/cmdline /proc/kpagecount/cmdline /proc/kpageflags/cmdline /proc/partitions/cmdline /proc/timer_list/cmdline /proc/execdomains/cmdline /proc/filesystems/cmdline /proc/kpagecgroup/cmdline /proc/sched_debug/cmdline /proc/vmallocinfo/cmdline /proc/pagetypeinfo/cmdline /proc/dynamic_debug/cmdline /proc/sysrq-trigger/cmdline /proc/self/cmdline /proc/thread-self/cmdline /proc/1/cmdline got there opened file closed file /proc/2/cmdline got there opened file closed file /proc/3/cmdline got there opened file closed file /proc/4/cmdline got there opened file closed file /proc/6/cmdline got there opened file closed file /proc/8/cmdline got there opened file closed file /proc/9/cmdline got there opened file closed file /proc/10/cmdline got there opened file closed file /proc/11/cmdline got there opened file closed file /proc/12/cmdline got there opened file closed file /proc/13/cmdline got there opened file closed file /proc/15/cmdline got there opened file closed file /proc/16/cmdline got there opened file closed file /proc/17/cmdline got there opened file closed file /proc/18/cmdline got there opened file closed file /proc/20/cmdline got there opened file closed file /proc/21/cmdline got there opened file closed file /proc/22/cmdline got there opened file closed file /proc/23/cmdline got there opened file closed file /proc/25/cmdline got there opened file closed file /proc/26/cmdline got there opened file closed file /proc/27/cmdline got there opened file closed file /proc/28/cmdline got there opened file closed file /proc/30/cmdline got there opened file closed file /proc/31/cmdline got there opened file closed file /proc/32/cmdline got there opened file closed file /proc/33/cmdline got there opened file closed file /proc/35/cmdline got there opened file closed file /proc/36/cmdline got there opened file closed file /proc/37/cmdline got there opened file closed file /proc/38/cmdline got there opened file closed file /proc/40/cmdline got there opened file closed file /proc/41/cmdline got there opened file closed file /proc/42/cmdline got there opened file closed file /proc/43/cmdline got there opened file closed file /proc/45/cmdline got there opened file closed file /proc/46/cmdline got there opened file closed file /proc/47/cmdline got there opened file closed file /proc/48/cmdline got there opened file closed file /proc/50/cmdline got there opened file closed file /proc/51/cmdline got there opened file closed file /proc/52/cmdline got there opened file closed file /proc/53/cmdline got there opened file closed file /proc/55/cmdline got there opened file closed file /proc/56/cmdline got there opened file closed file /proc/57/cmdline got there opened file closed file /proc/58/cmdline got there opened file closed file /proc/60/cmdline got there opened file closed file /proc/61/cmdline got there opened file closed file /proc/62/cmdline got there opened file closed file /proc/63/cmdline got there opened file closed file /proc/65/cmdline got there opened file closed file /proc/66/cmdline got there opened file closed file /proc/67/cmdline got there opened file closed file /proc/68/cmdline got there opened file closed file /proc/70/cmdline got there opened file closed file /proc/83/cmdline got there opened file closed file /proc/84/cmdline got there opened file closed file /proc/85/cmdline got there opened file closed file /proc/87/cmdline got there opened file closed file /proc/88/cmdline got there opened file closed file /proc/89/cmdline got there opened file closed file /proc/90/cmdline got there opened file closed file /proc/91/cmdline got there opened file closed file /proc/92/cmdline got there opened file closed file /proc/112/cmdline got there opened file closed file /proc/113/cmdline got there opened file closed file /proc/114/cmdline got there opened file closed file /proc/115/cmdline got there opened file closed file /proc/116/cmdline got there opened file closed file /proc/117/cmdline got there opened file closed file /proc/119/cmdline got there opened file closed file /proc/128/cmdline got there opened file closed file /proc/129/cmdline got there opened file closed file /proc/130/cmdline got there opened file closed file /proc/131/cmdline got there opened file closed file /proc/132/cmdline got there opened file closed file /proc/133/cmdline got there opened file closed file /proc/134/cmdline got there opened file closed file /proc/143/cmdline got there opened file closed file /proc/146/cmdline got there opened file closed file /proc/147/cmdline got there opened file closed file /proc/169/cmdline got there opened file closed file /proc/179/cmdline got there opened file closed file /proc/184/cmdline got there opened file closed file /proc/211/cmdline got there opened file closed file /proc/213/cmdline got there opened file closed file /proc/214/cmdline got there opened file closed file /proc/215/cmdline got there opened file closed file /proc/216/cmdline got there opened file closed file /proc/218/cmdline got there opened file closed file /proc/219/cmdline got there opened file closed file /proc/223/cmdline got there opened file closed file /proc/226/cmdline got there opened file closed file /proc/232/cmdline got there opened file closed file /proc/259/cmdline got there opened file closed file /proc/260/cmdline got there opened file closed file /proc/261/cmdline got there opened file closed file /proc/264/cmdline got there opened file closed file /proc/274/cmdline got there opened file closed file /proc/298/cmdline got there opened file closed file /proc/328/cmdline got there opened file closed file /proc/394/cmdline got there opened file closed file /proc/537/cmdline got there opened file closed file /proc/541/cmdline got there opened file closed file /proc/542/cmdline got there opened file closed file /proc/544/cmdline got there opened file closed file /proc/548/cmdline got there opened file closed file /proc/551/cmdline got there opened file closed file /proc/553/cmdline got there opened file closed file /proc/569/cmdline got there opened file closed file /proc/42390/cmdline got there opened file closed file /proc/42438/cmdline got there opened file closed file /proc/84263/cmdline got there opened file closed file /proc/84264/cmdline got there opened file closed file /proc/84897/cmdline got there opened file closed file /proc/84898/cmdline got there opened file closed file /proc/180944/cmdline got there opened file closed file /proc/182263/cmdline got there opened file closed file /proc/182266/cmdline got there opened file closed file /proc/184757/cmdline got there opened file closed file /proc/187168/cmdline got there opened file closed file /proc/187321/cmdline got there opened file closed file /proc/187367/cmdline got there opened file closed file /proc/187913/cmdline got there opened file closed file /proc/188058/cmdline got there opened file closed file /proc/188214/cmdline got there opened file closed file /proc/188217/cmdline got there opened file closed file /proc/206702/cmdline got there opened file closed file /proc/208002/cmdline got there opened file closed file /proc/208046/cmdline got there opened file closed file /proc/208077/cmdline got there opened file closed file /proc/208079/cmdline got there opened file closed file /proc/208118/cmdline got there opened file closed file /proc/208120/cmdline got there opened file closed file /proc/208133/cmdline got there opened file closed file /proc/208134/cmdline got there opened file closed file /proc/208792/cmdline got there opened file closed file /proc/472435/cmdline got there opened file closed file /proc/472810/cmdline got there opened file closed file /proc/472833/cmdline got there opened file closed file /proc/473143/cmdline got there opened file closed file /proc/473149/cmdline got there opened file closed file /proc/473150/cmdline got there opened file closed file /proc/508213/cmdline got there opened file closed file /proc/509731/cmdline got there opened file closed file /proc/510896/cmdline got there opened file closed file /proc/511678/cmdline got there opened file closed file /proc/512531/cmdline got there opened file closed file -1 build@inkbox /tmp> armv7l-linux-musleabihf-gcc kill_process.c -o kill_process -g -static build@inkbox /tmp> ./kill_process /proc/./cmdline /proc/../cmdline /proc/fb/cmdline /proc/fs/cmdline /proc/bus/cmdline /proc/dma/cmdline /proc/irq/cmdline /proc/net/cmdline /proc/sys/cmdline /proc/tty/cmdline /proc/xen/cmdline /proc/acpi/cmdline /proc/keys/cmdline /proc/kmsg/cmdline /proc/misc/cmdline /proc/mtrr/cmdline /proc/stat/cmdline /proc/iomem/cmdline /proc/kcore/cmdline /proc/locks/cmdline /proc/swaps/cmdline /proc/crypto/cmdline /proc/driver/cmdline /proc/mounts/cmdline /proc/uptime/cmdline /proc/vmstat/cmdline /proc/cgroups/cmdline /proc/cmdline/cmdline /proc/cpuinfo/cmdline /proc/devices/cmdline /proc/ioports/cmdline /proc/loadavg/cmdline /proc/meminfo/cmdline /proc/modules/cmdline /proc/sysvipc/cmdline /proc/version/cmdline /proc/consoles/cmdline /proc/kallsyms/cmdline /proc/pressure/cmdline /proc/slabinfo/cmdline /proc/softirqs/cmdline /proc/zoneinfo/cmdline /proc/buddyinfo/cmdline /proc/diskstats/cmdline /proc/key-users/cmdline /proc/schedstat/cmdline /proc/interrupts/cmdline /proc/kpagecount/cmdline /proc/kpageflags/cmdline /proc/partitions/cmdline /proc/timer_list/cmdline /proc/execdomains/cmdline /proc/filesystems/cmdline /proc/kpagecgroup/cmdline /proc/sched_debug/cmdline /proc/vmallocinfo/cmdline /proc/pagetypeinfo/cmdline /proc/dynamic_debug/cmdline /proc/sysrq-trigger/cmdline /proc/self/cmdline /proc/thread-self/cmdline /proc/1/cmdline got there opened file qemu: uncaught target signal 11 (Segmentation fault) - core dumped fish: “./kill_process” terminated by signal SIGSEGV (Address boundary error) build@inkbox /tmp [SIGSEGV]> ---- FILE ---- - tux-linux The first kill_process was compiled with native gcc (x86_64), and it works. The second kill_process, which still has the same code, was compiled with musl toolchain and run via binfmt on x86_64. It also segfaults on native arm. Using a glibc instead of a musl toolchain doesn't change anything. ```if(access(cmdline_file, F_OK) != 0)``` it seems this function doesn't work on arm for some reason in this specific case for reference ---- FILE: kill_process.c ---- #include #include #include #include #include #include #include #include #include #include "kill_process.h" void kill_process(const char * name, int signal) { int pid = get_pid_by_name(name); printf("%d\n", pid); if(pid != -1) { kill(pid, signal); } } int is_regular_file(const char *path) { struct stat path_stat; stat(path, &path_stat); return S_ISREG(path_stat.st_mode); } int get_pid_by_name(const char * name) { struct dirent * entry = NULL; DIR * dp = NULL; const char proc[] = "/proc/"; dp = opendir(proc); while((entry = readdir(dp))) { // cmdline is more accurate, sometimes status may be buggy char cmdline_file[1024] = { 0 }; sprintf(cmdline_file, "%s%s/cmdline", proc, entry->d_name); printf("%s\n", cmdline_file); if(strcmp(cmdline_file, "/proc/./cmdline") == 0 || strcmp(cmdline_file, "/proc/self/cmdline") == 0 || strcmp(cmdline_file, "/proc/thread-self/cmdline") == 0) { continue; } if(access(cmdline_file, F_OK) != 0) { continue; } printf("got there\n"); // https://www.geeksforgeeks.org/how-to-append-a-character-to-a-string-in-c/ FILE* ptr; char ch; char cmdline[4096] = { 0 }; // Opening file in reading mode ptr = fopen(cmdline_file, "r"); printf("opened file\n"); // Printing what is written in file // character by character using loop. do { ch = fgetc(ptr); strncat(cmdline, &ch, 1); // Checking if character is not EOF. // If it is EOF stop reading. } while (ch != EOF); // Closing the file fclose(ptr); printf("closed file\n"); // https://stackoverflow.com/questions/2340281/check-if-a-string-contains-a-string-in-c if(strstr(cmdline, name)) { // After closing directory, it's impossible to call entry->d_name int return_pid = atoi(entry->d_name); closedir(dp); return return_pid; } } // If we get here, we haven't found any PID closedir(dp); return -1; } char * read_file(const char * file_path, bool strip_newline) { // Ensure that specified file exists, then try to read it if(access(file_path, F_OK) != 0) { return NULL; } FILE * fp = fopen(file_path , "rb"); if(!fp) { return NULL; } fseek(fp, 0L, SEEK_END); long bytes = ftell(fp); rewind(fp); // Allocate memory for entire content char * buffer = NULL; buffer = calloc(sizeof(*buffer), (size_t) bytes + 1U); if(!buffer) { goto cleanup; } // Copy the file into the buffer if(fread(buffer, sizeof(*buffer), (size_t) bytes, fp) < (size_t) bytes || ferror(fp) != 0) { // Short read or error, meep! free(buffer); buffer = NULL; goto cleanup; } // If requested, remove trailing newline if(strip_newline && bytes > 0 && buffer[bytes - 1] == '\n') { buffer[bytes - 1] = '\0'; } cleanup: fclose(fp); return buffer; // May be NULL (indicates failure) } int main() { kill_process("busybox", SIGKILL); } ---- FILE: kill_process.c ---- ---- FILE: kill_process.h ---- int get_pid_by_name(const char * name); int is_regular_file(const char * path); void kill_process(const char * name, int signal); char * read_file(const char * file_path, bool strip_newline); ---- FILE: kill_process.h ---- - tux-linux I give up