answer a) the name of the process in which you get above the code.
answer d), to get the related files, pass the pid of the process to this function (we have pid in the question code) -
void print_argv_of_pid(int pid) { printf("%d\n", pid); int mib[3], argmax, nargs, c = 0; size_t size; char *procargs, *sp, *np, *cp; extern int eflg; int show_args = 1; mib[0] = CTL_KERN; mib[1] = KERN_ARGMAX; size = sizeof(argmax); if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) { goto ERROR_A; } procargs = (char *)malloc(argmax); if (procargs == NULL) { goto ERROR_A; } mib[0] = CTL_KERN; mib[1] = KERN_PROCARGS2; mib[2] = pid; size = (size_t)argmax; if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) { goto ERROR_B; } memcpy(&nargs, procargs, sizeof(nargs)); cp = procargs + sizeof(nargs); for (; cp < &procargs[size]; cp++) { if (*cp == '\0') { break; } } if (cp == &procargs[size]) { goto ERROR_B; } for (; cp < &procargs[size]; cp++) { if (*cp != '\0') { break; } } if (cp == &procargs[size]) { goto ERROR_B; } sp = cp; for (np = NULL; c < nargs && cp < &procargs[size]; cp++) { if (*cp == '\0') { c++; if (np != NULL) { *np = ' '; } else { } np = cp; if (!show_args) { break; } } } if (np == NULL || np == sp) { goto ERROR_B; } printf("%s\n", sp); free(procargs); return; ERROR_B: free(procargs); ERROR_A: printf("error"); }
answer b), c) -size and access time -
struct stat st; //pass filepath upto /.app/ to stat function (use 'componentsseparatedby' of nsstring apply on full path which we got in answer d code above) if (stat(filename, &st)) { perror(filename); } else { printf("%s: mtime = %lld.%.9ld\n", filename, (long long)st.st_mtimespec.tv_sec, st.st_mtimespec.tv_nsec); printf("File size: %lld bytes\n", (long long) st.st_size); printf("Last status change: %s", ctime(&st.st_ctime)); printf("Last file access: %s", ctime(&st.st_atime)); printf("Last file modification: %s", ctime(&st.st_mtime)); }
KILL A PROCESS - just pass the pid of the process you want to kill -
int pid_exists(long pid) { int kill_ret; // save some time if it an invalid PID if (pid < 0) { return 0; } // if kill returns success of permission denied we know it a valid PID kill_ret = kill(pid , 0); if ( (0 == kill_ret) || (EPERM == errno) ) { return 1; } // otherwise return 0 for PID not found return 0;
}