Commit 8bbf64eb authored by Andrey Vagin's avatar Andrey Vagin Committed by Pavel Emelyanov

posix-timer: make parser a bit more readable

Cc: Pavel Tikhomirov <snorcht@gmail.com>
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 0f960261
...@@ -1161,27 +1161,14 @@ err: ...@@ -1161,27 +1161,14 @@ err:
int parse_posix_timers(pid_t pid, struct proc_posix_timers_stat *args) int parse_posix_timers(pid_t pid, struct proc_posix_timers_stat *args)
{ {
int i;
int ret = 0; int ret = 0;
int get = 0;
int pid_t; int pid_t;
FILE * file; FILE * file;
char * line1 = NULL;
char * line2 = NULL;
char * line3 = NULL;
char * line4 = NULL;
size_t len1 = 0;
size_t len2 = 0;
size_t len3 = 0;
size_t len4 = 0;
char * siginfo;
char siginfo_tmp[20];
char sigpid[7]; char sigpid[7];
char tidpid[4]; char tidpid[4];
char str_name[10];
struct proc_posix_timer *timer = NULL; struct proc_posix_timer *timer = NULL;
INIT_LIST_HEAD(&args->timers); INIT_LIST_HEAD(&args->timers);
...@@ -1194,39 +1181,24 @@ int parse_posix_timers(pid_t pid, struct proc_posix_timers_stat *args) ...@@ -1194,39 +1181,24 @@ int parse_posix_timers(pid_t pid, struct proc_posix_timers_stat *args)
} }
while (1) { while (1) {
get = getline(&line1, &len1, file);
if (get == -1)
goto end_posix;
get = getline(&line2, &len2, file);
if (get == -1)
goto end_posix;
get = getline(&line3, &len3, file);
if (get == -1)
goto end_posix;
get = getline(&line4, &len4, file);
if (get == -1)
goto end_posix;
timer = xzalloc(sizeof(struct proc_posix_timer)); timer = xzalloc(sizeof(struct proc_posix_timer));
if (timer == NULL)
goto err;
ret = sscanf(line1, "%s %ld", str_name, &timer->spt.it_id); ret = fscanf(file, "ID: %ld\n"
if (ret != 2 || str_name[0] != 'I') "signal: %d/%p\n"
goto parse_err_posix; "notify: %6[a-z]/%3[a-z].%d\n"
ret = sscanf(line2, "%s %d%s", str_name, &timer->spt.si_signo, siginfo_tmp); "ClockID: %d\n",
if (ret != 3 || str_name[0] != 's') &timer->spt.it_id,
goto parse_err_posix; &timer->spt.si_signo, &timer->spt.sival_ptr,
siginfo=&siginfo_tmp[1]; sigpid, tidpid, &pid_t,
ret = sscanf(siginfo, "%p", &timer->spt.sival_ptr); &timer->spt.clock_id);
if (ret != 1) if (ret != 7) {
goto parse_err_posix; ret = 0;
for (i = 0; i<len3; i++) { if (feof(file))
if (line3[i] == '/' || line3[i] == '.') { goto out;
line3[i] = ' '; goto err;
}
} }
ret = sscanf(line3, "%s %s %s %d", str_name, sigpid, tidpid, &pid_t);
if (ret != 4 || str_name[0] != 'n')
goto parse_err_posix;
if ( tidpid[0] == 't') { if ( tidpid[0] == 't') {
timer->spt.it_sigev_notify = SIGEV_THREAD_ID; timer->spt.it_sigev_notify = SIGEV_THREAD_ID;
...@@ -1244,14 +1216,12 @@ int parse_posix_timers(pid_t pid, struct proc_posix_timers_stat *args) ...@@ -1244,14 +1216,12 @@ int parse_posix_timers(pid_t pid, struct proc_posix_timers_stat *args)
} }
} }
ret = sscanf(line4, "%s %d", str_name, &timer->spt.clock_id);
if (ret != 2 || str_name[0] != 'C')
goto parse_err_posix;
list_add(&timer->list, &args->timers); list_add(&timer->list, &args->timers);
timer = NULL; timer = NULL;
args->timer_n++; args->timer_n++;
} }
parse_err_posix: err:
xfree(timer);
while (!list_empty(&args->timers)) { while (!list_empty(&args->timers)) {
timer = list_first_entry(&args->timers, struct proc_posix_timer, list); timer = list_first_entry(&args->timers, struct proc_posix_timer, list);
list_del(&timer->list); list_del(&timer->list);
...@@ -1259,20 +1229,7 @@ parse_err_posix: ...@@ -1259,20 +1229,7 @@ parse_err_posix:
} }
pr_perror("Parse error in posix timers proc file!"); pr_perror("Parse error in posix timers proc file!");
ret = -1; ret = -1;
end_posix: out:
if (ferror(file)) {
ret = -1;
pr_perror("getline");
}
if (line1)
free(line1);
if (line2)
free(line2);
if (line3)
free(line3);
if (file != NULL)
fclose(file); fclose(file);
return ret; return ret;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment