• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
4  */
5 
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <errno.h>
10 
11 #include "tst_safe_file_ops.h"
12 #include "tst_process_state.h"
13 
tst_thread_state_wait(pid_t tid,const char state,unsigned int msec_timeout)14 int tst_thread_state_wait(pid_t tid, const char state,
15 			  unsigned int msec_timeout)
16 {
17 	char proc_path[128], cur_state;
18 	unsigned int msecs = 0;
19 
20 	snprintf(proc_path, sizeof(proc_path), "/proc/self/task/%i/stat", tid);
21 
22 	for (;;) {
23 		SAFE_FILE_SCANF(proc_path, "%*i %*s %c", &cur_state);
24 
25 		if (state == cur_state)
26 			break;
27 
28 		usleep(1000);
29 		msecs += 1;
30 
31 		if (msec_timeout && msecs >= msec_timeout) {
32 			errno = ETIMEDOUT;
33 			return -1;
34 		}
35 	}
36 
37 	return 0;
38 }
39