diff mbox

[ovs-dev,4/7] process: Retrieve process status.

Message ID 1491073335-5178-5-git-send-email-bhanuprakash.bodireddy@intel.com
State Changes Requested
Headers show

Commit Message

Bodireddy, Bhanuprakash April 1, 2017, 7:02 p.m. UTC
Implement function to retrieve the process status. This will be used by
Keepalive monitoring thread for detecting false alarms.

Signed-off-by: Bhanuprakash Bodireddy <bhanuprakash.bodireddy@intel.com>
---
 lib/process.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/process.h | 10 ++++++++++
 2 files changed, 70 insertions(+)

Comments

Aaron Conole April 3, 2017, 1 a.m. UTC | #1
Bhanuprakash Bodireddy <bhanuprakash.bodireddy@intel.com> writes:

> Implement function to retrieve the process status. This will be used by
> Keepalive monitoring thread for detecting false alarms.
>
> Signed-off-by: Bhanuprakash Bodireddy <bhanuprakash.bodireddy@intel.com>
> ---
>  lib/process.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  lib/process.h | 10 ++++++++++
>  2 files changed, 70 insertions(+)
>
> diff --git a/lib/process.c b/lib/process.c
> index e9d0ba9..e0601dd 100644
> --- a/lib/process.c
> +++ b/lib/process.c
> @@ -50,6 +50,20 @@ struct process {
>      int status;
>  };
>  
> +struct pstate2Num {
> +    char *tidState;
> +    int num;
> +};
> +
> +const struct pstate2Num pstate_map[] = {
> +    { "S", STOPPED_STATE },
> +    { "R", ACTIVE_STATE },
> +    { "t", TRACED_STATE },
> +    { "Z", DEFUNC_STATE },
> +    { "D", UNINTERRUPTIBLE_SLEEP_STATE },
> +    { "NULL", UNUSED_STATE },
> +};
> +
>  /* Pipe used to signal child termination. */
>  static int fds[2];
>  
> @@ -390,6 +404,52 @@ process_run(void)
>  #endif
>  }
>  
> +int
> +get_process_status(int tid, int *pstate)
> +{
> +#ifndef _WIN32

The following is Linux specific.  Please use an '#if LINUX' - there are
examples in the code for this.

> +    static char process_name[20];
> +    FILE *stream;
> +    char line[75];
> +    char Name[15], value[5], status[20];
> +    int i, ln;
> +
> +    snprintf(process_name, sizeof(process_name),
> +             "/proc/%d/status", tid);
> +    stream = fopen(process_name, "r");
> +    if (stream == NULL) {
> +        VLOG_WARN_ONCE("%s: open failed: %s", process_name,
> +            ovs_strerror(errno));
> +        return errno;
> +    }
> +
> +    ln=0;
> +    while (fgets(line, sizeof line, stream)) {
> +        if (!ovs_scan(line,
> +                      "%6s %2s %14s\n",
> +                       Name, value, status)) {
> +            VLOG_WARN_ONCE("%s: could not parse line %d: %s",
> +                    process_name, ln, line);
> +            continue;
> +        }
> +        if (!strcmp(Name, "State:")) {
> +            for (i=0; pstate_map[i].tidState != NULL; i++) {
> +                if (strcmp(pstate_map[i].tidState, value) == 0) {
> +                    VLOG_INFO("The state is %s, status is %d\n",
> +                        pstate_map[i].tidState, pstate_map[i].num);
> +                    *pstate = pstate_map[i].num;
> +                    break;
> +                }
> +            }
> +            break;
> +        }
> +        ln++;
> +   }
> +   return 0;
> +#else
> +   return ENOSYS;
> +#endif
> +}
>  
>  /* Causes the next call to poll_block() to wake up when process 'p' has
>   * exited. */
> diff --git a/lib/process.h b/lib/process.h
> index 3feac7e..8a5513e 100644
> --- a/lib/process.h
> +++ b/lib/process.h
> @@ -20,6 +20,15 @@
>  #include <stdbool.h>
>  #include <sys/types.h>
>  
> +enum process_states {
> +    UNUSED_STATE,
> +    STOPPED_STATE,
> +    ACTIVE_STATE,
> +    TRACED_STATE,
> +    DEFUNC_STATE,
> +    UNINTERRUPTIBLE_SLEEP_STATE
> +};
> +
>  struct process;
>  
>  /* Starting and monitoring subprocesses.
> @@ -38,6 +47,7 @@ bool process_exited(struct process *);
>  int process_status(const struct process *);
>  void process_run(void);
>  void process_wait(struct process *);
> +int get_process_status(int, int *);
>  
>  /* These functions are thread-safe. */
>  char *process_status_msg(int);
Bodireddy, Bhanuprakash April 3, 2017, 1 p.m. UTC | #2
>-----Original Message-----
>From: Aaron Conole [mailto:aconole@redhat.com]
>Sent: Monday, April 3, 2017 2:00 AM
>To: Bodireddy, Bhanuprakash <bhanuprakash.bodireddy@intel.com>
>Cc: dev@openvswitch.org
>Subject: Re: [ovs-dev] [PATCH 4/7] process: Retrieve process status.
>
>Bhanuprakash Bodireddy <bhanuprakash.bodireddy@intel.com> writes:
>
>> Implement function to retrieve the process status. This will be used
>> by Keepalive monitoring thread for detecting false alarms.
>>
>> Signed-off-by: Bhanuprakash Bodireddy
>> <bhanuprakash.bodireddy@intel.com>
>> ---
>>  lib/process.c | 60
>>
>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>  lib/process.h | 10 ++++++++++
>>  2 files changed, 70 insertions(+)
>>
>> diff --git a/lib/process.c b/lib/process.c index e9d0ba9..e0601dd
>> 100644
>> --- a/lib/process.c
>> +++ b/lib/process.c
>> @@ -50,6 +50,20 @@ struct process {
>>      int status;
>>  };
>>
>> +struct pstate2Num {
>> +    char *tidState;
>> +    int num;
>> +};
>> +
>> +const struct pstate2Num pstate_map[] = {
>> +    { "S", STOPPED_STATE },
>> +    { "R", ACTIVE_STATE },
>> +    { "t", TRACED_STATE },
>> +    { "Z", DEFUNC_STATE },
>> +    { "D", UNINTERRUPTIBLE_SLEEP_STATE },
>> +    { "NULL", UNUSED_STATE },
>> +};
>> +
>>  /* Pipe used to signal child termination. */  static int fds[2];
>>
>> @@ -390,6 +404,52 @@ process_run(void)  #endif  }
>>
>> +int
>> +get_process_status(int tid, int *pstate) { #ifndef _WIN32
>
>The following is Linux specific.  Please use an '#if LINUX' - there are examples
>in the code for this.

That's right. I would do this in v2.

- Bhanuprakash
diff mbox

Patch

diff --git a/lib/process.c b/lib/process.c
index e9d0ba9..e0601dd 100644
--- a/lib/process.c
+++ b/lib/process.c
@@ -50,6 +50,20 @@  struct process {
     int status;
 };
 
+struct pstate2Num {
+    char *tidState;
+    int num;
+};
+
+const struct pstate2Num pstate_map[] = {
+    { "S", STOPPED_STATE },
+    { "R", ACTIVE_STATE },
+    { "t", TRACED_STATE },
+    { "Z", DEFUNC_STATE },
+    { "D", UNINTERRUPTIBLE_SLEEP_STATE },
+    { "NULL", UNUSED_STATE },
+};
+
 /* Pipe used to signal child termination. */
 static int fds[2];
 
@@ -390,6 +404,52 @@  process_run(void)
 #endif
 }
 
+int
+get_process_status(int tid, int *pstate)
+{
+#ifndef _WIN32
+    static char process_name[20];
+    FILE *stream;
+    char line[75];
+    char Name[15], value[5], status[20];
+    int i, ln;
+
+    snprintf(process_name, sizeof(process_name),
+             "/proc/%d/status", tid);
+    stream = fopen(process_name, "r");
+    if (stream == NULL) {
+        VLOG_WARN_ONCE("%s: open failed: %s", process_name,
+            ovs_strerror(errno));
+        return errno;
+    }
+
+    ln=0;
+    while (fgets(line, sizeof line, stream)) {
+        if (!ovs_scan(line,
+                      "%6s %2s %14s\n",
+                       Name, value, status)) {
+            VLOG_WARN_ONCE("%s: could not parse line %d: %s",
+                    process_name, ln, line);
+            continue;
+        }
+        if (!strcmp(Name, "State:")) {
+            for (i=0; pstate_map[i].tidState != NULL; i++) {
+                if (strcmp(pstate_map[i].tidState, value) == 0) {
+                    VLOG_INFO("The state is %s, status is %d\n",
+                        pstate_map[i].tidState, pstate_map[i].num);
+                    *pstate = pstate_map[i].num;
+                    break;
+                }
+            }
+            break;
+        }
+        ln++;
+   }
+   return 0;
+#else
+   return ENOSYS;
+#endif
+}
 
 /* Causes the next call to poll_block() to wake up when process 'p' has
  * exited. */
diff --git a/lib/process.h b/lib/process.h
index 3feac7e..8a5513e 100644
--- a/lib/process.h
+++ b/lib/process.h
@@ -20,6 +20,15 @@ 
 #include <stdbool.h>
 #include <sys/types.h>
 
+enum process_states {
+    UNUSED_STATE,
+    STOPPED_STATE,
+    ACTIVE_STATE,
+    TRACED_STATE,
+    DEFUNC_STATE,
+    UNINTERRUPTIBLE_SLEEP_STATE
+};
+
 struct process;
 
 /* Starting and monitoring subprocesses.
@@ -38,6 +47,7 @@  bool process_exited(struct process *);
 int process_status(const struct process *);
 void process_run(void);
 void process_wait(struct process *);
+int get_process_status(int, int *);
 
 /* These functions are thread-safe. */
 char *process_status_msg(int);