int _proc_check_cmdline_bypid(int pid);
void _prepare_listen_sock(void);
int _delete_sock_path(int pid, uid_t uid);
+int _close_all_fds(void);
#endif /* __COMMON_H__ */
return 0;
}
+int _close_all_fds(void)
+{
+ DIR *dp;
+ struct dirent dentry;
+ struct dirent *result = NULL;
+ int fd;
+ int max_fd;
+
+ dp = opendir("/proc/self/fd");
+ if (dp == NULL) {
+ /* fallback */
+ max_fd = sysconf(_SC_OPEN_MAX);
+ for (fd = 3; fd < max_fd; fd++)
+ close(fd);
+
+ return 0;
+ }
+
+ while (readdir_r(dp, &dentry, &result) == 0 && result) {
+ if (!isdigit(dentry.d_name[0]))
+ continue;
+
+ fd = atoi(dentry.d_name);
+ if (fd < 3)
+ continue;
+
+ if (fd == dirfd(dp))
+ continue;
+
+ close(fd);
+ }
+ closedir(dp);
+
+ return 0;
+}
+
{
char sock_path[PATH_MAX];
int pid;
- int max_fd;
- int iter_fd;
if (__prepare_fork(kb, appinfo->debug_appid) < 0)
return -1;
_signal_unblock_sigchld();
_signal_fini();
- max_fd = sysconf(_SC_OPEN_MAX);
- for (iter_fd = 3; iter_fd <= max_fd; iter_fd++)
- close(iter_fd);
-
- snprintf(sock_path, sizeof(sock_path), "%s/apps/%d/%d",
- SOCKET_PATH, getuid(), getpid());
- unlink(sock_path);
+ _close_all_fds();
+ _delete_sock_path(getpid(), getuid());
PERF("prepare exec - fisrt done");
_D("lock up test log(no error): prepare exec - first done");