}
}
+int get_pid_array(int arr[], const int n)
+{
+ DIR *d = opendir("/proc");
+ struct dirent *dirent;
+ int count = 0;
+ pid_t self_pid = getpid();
+
+ if (!d) {
+ LOGW("Cannot open /proc dir (%s)\n", strerror(errno));
+ return 0;
+ }
+
+ while ((dirent = readdir(d)) && (count < n)) {
+ if (dirent->d_type == DT_DIR) {
+ char *tmp;
+ pid_t pid = strtol(dirent->d_name, &tmp, 10);
+ if (*tmp == '\0' && pid != self_pid)
+ arr[count++] = pid;
+ }
+ }
+
+ closedir(d);
+
+ return count;
+}
+
+static pid_t get_first_target_process(void)
+{
+ pid_t pid = -1;
+ int i;
+
+ for (i = 0; i < MAX_TARGET_COUNT; i++) {
+ if (manager.target[i].socket != -1 &&
+ manager.target[i].pid != -1) {
+ pid = manager.target[i].pid;
+ break;
+ }
+ }
+ /* Calling code must guarantee this */
+ assert(pid != -1);
+
+ return pid;
+}
+
// return log length (>0) for normal case
// return negative value for error
int get_system_info(struct system_info_t *sys_info, int* pidarray, int pidcount)
}
if (pidcount > 0)
- if (update_thread_data(pidarray[0]) < 0) {
+ if (update_thread_data(get_first_target_process()) < 0) {
LOGE("Failed to update thread stat data\n");
goto fail_exit;
}
uint32_t cpu_core_count;
};
+int get_pid_array(int pidarr[], const int n);
int get_system_info(struct system_info_t *sys_info, int *pidarray, int pidcount);
int get_device_info(char* buffer, int buffer_len);
}
if (signo == SIGALRM) {
- int pidarr[MAX_TARGET_COUNT];
+ const int max_pid_num = 1024; /* ugly hardcode */
+ int pidarr[max_pid_num];
int pidcount = 0;
struct system_info_t sys_info;
struct msg_data_t *msg;
- for (i = 0; i < MAX_TARGET_COUNT; i++) {
- if (manager.target[i].socket != -1 &&
- manager.target[i].pid != -1)
- pidarr[pidcount++] = manager.target[i].pid;
- }
+ pidcount = get_pid_array(pidarr, max_pid_num);
if (get_system_info(&sys_info, pidarr, pidcount) == -1) {
LOGE("Cannot get system info\n");