Skip to contentSkip to conversation

process_state: Enhancement of process state detection

PATCH In review Li Wang 3 participants 4 messages Feb 5, 2024 – Feb 7, 2024 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 process_state: Enhancement of process state detection

The functions will be more robust against process names with unusual characters and will correctly read the state character from the /proc/[pid]/stat file. This is a necessary change because the process name, which is a free-form string, can contain spaces and other characters that would otherwise disrupt the simple parsing logic of the original format string.

e.g.

 $ cat /proc/792442/stat
 792442 (Web Content) S 164213 4351 4351 0 -1 4194560 ...
---
 lib/tst_process_state.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/tst_process_state.c +2 -2
index 08a9d0966..c15283c3d 100644
@@ -22,7 +22,7 @@ int tst_process_state_wait(const char *file, const int lineno,
 
 	for (;;) {
 		safe_file_scanf(file, lineno, cleanup_fn, proc_path,
-				"%*i %*s %c", &cur_state);
+				"%*[^)]%*c %c", &cur_state);
 
 		if (state == cur_state)
 			break;
@@ -54,7 +54,7 @@ int tst_process_state_wait2(pid_t pid, const char state)
 			return 1;
 		}
 
-		if (fscanf(f, "%*i %*s %c", &cur_state) != 1) {
+		if (fscanf(f, "%*[^)]%*c %c", &cur_state) != 1) {
 			fclose(f);
 			fprintf(stderr, "Failed to read '%s': %s\n",
 				proc_path, strerror(errno));
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#1 lore raw

Hi Li, Ian,

Quoted 23 lines

The functions will be more robust against process names with unusual characters and will correctly read the state character from the /proc/[pid]/stat file. This is a necessary change because the process name, which is a free-form string, can contain spaces and other characters that would otherwise disrupt the simple parsing logic of the original format string.

e.g.

 $ cat /proc/792442/stat
 792442 (Web Content) S 164213 4351 4351 0 -1 4194560 ...
---
 lib/tst_process_state.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/tst_process_state.c +0 -0
index 08a9d0966..c15283c3d 100644

lib/tst_process_state.c int tst_process_state_wait(const char *file, const int lineno, · line 25

Quoted 5 lines
lib/tst_process_state.c +1 -1
@@ -22,7 +22,7 @@ int tst_process_state_wait(const char *file, const int lineno,
 
 	for (;;) {
 		safe_file_scanf(file, lineno, cleanup_fn, proc_path,
-				"%*i %*s %c", &cur_state);
+				"%*[^)]%*c %c", &cur_state);

Obviously correct, thanks.

But there is also the same issue in lib/tst_thread_state.c,
I guess it applies to that. Li, could you please also fix it before merge?

Also, Andrea, you added tst_thread_state_wait() and TST_THREAD_STATE_WAIT() for futex_waitv0[23] related tests [1], but it's now not used anywhere due Jan's changes [2] [3]. I wonder if it's still useful or whether we should remove it.

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/[email protected]/
[2] https://lore.kernel.org/ltp/6c5b161bc3bcf753cbda92954ca3f47cb268c68f.1663665637.git.jstancek@redhat.com/
[3] https://lore.kernel.org/ltp/6bac7035adc2cfc8ab3800fe1d2d03223ec57ff5.1663662348.git.jstancek@redhat.com/

Quoted 11 lines
lib/tst_process_state.c +1 -1
 		if (state == cur_state)
 			break;
@@ -54,7 +54,7 @@ int tst_process_state_wait2(pid_t pid, const char state)
 			return 1;
 		}
 
-		if (fscanf(f, "%*i %*s %c", &cur_state) != 1) {
+		if (fscanf(f, "%*[^)]%*c %c", &cur_state) != 1) {
 			fclose(f);
 			fprintf(stderr, "Failed to read '%s': %s\n",
 				proc_path, strerror(errno));
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by
#2 lore raw
Quoted 27 lines · Petr Vorel

Hi Li, Ian,

Quoted 23 lines

The functions will be more robust against process names with unusual characters and will correctly read the state character from the /proc/[pid]/stat file. This is a necessary change because the process name, which is a free-form string, can contain spaces and other characters that would otherwise disrupt the simple parsing logic of the original format string.

e.g.

 $ cat /proc/792442/stat
 792442 (Web Content) S 164213 4351 4351 0 -1 4194560 ...
---
 lib/tst_process_state.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/tst_process_state.c +0 -0
index 08a9d0966..c15283c3d 100644
@@ -22,7 +22,7 @@ int tst_process_state_wait(const char *file, const int lineno,
Quoted 8 lines

lib/tst_process_state.c int tst_process_state_wait(const char *file, const int lineno, · line 25

Quoted 4 lines
lib/tst_process_state.c +1 -1
      for (;;) {
              safe_file_scanf(file, lineno, cleanup_fn, proc_path,
-                             "%*i %*s %c", &cur_state);
+                             "%*[^)]%*c %c", &cur_state);

Obviously correct, thanks.

Quoted 7 lines

But there is also the same issue in lib/tst_thread_state.c,
I guess it applies to that. Li, could you please also fix it before merge?

Also, Andrea, you added tst_thread_state_wait() and TST_THREAD_STATE_WAIT() for futex_waitv0[23] related tests [1], but it's now not used anywhere due Jan's changes [2] [3]. I wonder if it's still useful or whether we should remove it.

I'd keep it, it's a small function and someone could find useful for some new test.

Quoted 20 lines

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/[email protected]/
[2] https://lore.kernel.org/ltp/6c5b161bc3bcf753cbda92954ca3f47cb268c68f.1663665637.git.jstancek@redhat.com/
[3] https://lore.kernel.org/ltp/6bac7035adc2cfc8ab3800fe1d2d03223ec57ff5.1663662348.git.jstancek@redhat.com/

Quoted 11 lines
lib/tst_process_state.c +1 -1
              if (state == cur_state)
                      break;
@@ -54,7 +54,7 @@ int tst_process_state_wait2(pid_t pid, const char state)
                      return 1;
              }
 
-             if (fscanf(f, "%*i %*s %c", &cur_state) != 1) {
+             if (fscanf(f, "%*[^)]%*c %c", &cur_state) != 1) {
                      fclose(f);
                      fprintf(stderr, "Failed to read '%s': %s\n",
                              proc_path, strerror(errno));
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw
In reply to Petr Vorel
Quoted 32 lines · Petr Vorel

Hi Li, Ian,

lib/tst_process_state.c int tst_process_state_wait(const char *file, const int

Quoted 23 lines

The functions will be more robust against process names with unusual characters and will correctly read the state character from the /proc/[pid]/stat file. This is a necessary change because the process name, which is a free-form string, can contain spaces and other characters that would otherwise disrupt the simple parsing logic of the original format string.

e.g.

 $ cat /proc/792442/stat
 792442 (Web Content) S 164213 4351 4351 0 -1 4194560 ...
---
 lib/tst_process_state.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/tst_process_state.c +0 -0
index 08a9d0966..c15283c3d 100644
@@ -22,7 +22,7 @@ int tst_process_state_wait(const char *file, const int

lineno,

lib/tst_process_state.c int tst_process_state_wait(const char *file, const int lineno, · line 25

Quoted 4 lines
lib/tst_process_state.c +1 -1
      for (;;) {
              safe_file_scanf(file, lineno, cleanup_fn, proc_path,
-                             "%*i %*s %c", &cur_state);
+                             "%*[^)]%*c %c", &cur_state);
Quoted 7 lines

Obviously correct, thanks.

But there is also the same issue in lib/tst_thread_state.c,
I guess it applies to that. Li, could you please also fix it before merge?

Good point, applied with the tst_thread_state change.

Quoted 8 lines

Also, Andrea, you added tst_thread_state_wait() and TST_THREAD_STATE_WAIT() for
futex_waitv0[23] related tests [1], but it's now not used anywhere due Jan's
changes [2] [3]. I wonder if it's still useful or whether we should remove it.

Agreed with Jan, it is still useful in the future.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp