]> granicus.if.org Git - procps-ng/commitdiff
library: add sequential access to that pids collection
authorJim Warner <james.warner@comcast.net>
Wed, 26 Aug 2015 05:00:00 +0000 (00:00 -0500)
committerCraig Small <csmall@enc.com.au>
Thu, 27 Aug 2015 12:20:51 +0000 (22:20 +1000)
To ease the transition to the new interface, for other
than that top program, individual read provisions have
been added to the <proc/pids.h> API. This represents a
refinement of a position stated in a post noted below.

Reference(s):
http://www.freelists.org/post/procps/newlib-ps-fix

Signed-off-by: Jim Warner <james.warner@comcast.net>
proc/libprocps.sym
proc/pids.c
proc/pids.h

index b2e1f0c7b2db4ae362e8b3e942574e38fc8ffe6e..6f5156e568763032368b087a92554c0e0bf91104 100644 (file)
@@ -43,6 +43,9 @@ global:
        procps_meminfo_stack_fill;
        procps_meminfo_stack_alloc;
        procps_pids_new;
+       procps_pids_read_next;
+       procps_pids_read_open;
+       procps_pids_read_shut;
        procps_pids_reap;
        procps_pids_ref;
        procps_pids_reset;
index 913bb2b374db3e023a18cb904edeef0c55da7071..ac24ab0d8472dd974c7ec043baf276fd20facec1 100644 (file)
@@ -47,6 +47,8 @@
 #define FILL_ID_MAX  255               // upper limit for pid/uid fills
 #define MEMORY_INCR  128               // amt by which allocations grow
 
+#define READS_BEGUN (info->read)       // a read is in progress
+
 enum pids_item PROCPS_PIDS_logical_end  = PROCPS_PIDS_noop + 1;
 enum pids_item PROCPS_PIDS_physical_end = PROCPS_PIDS_noop + 2;
 
@@ -70,6 +72,8 @@ struct procps_pidsinfo {
     int history_yes;                   // need historical data
     struct history_info *hist;         // pointer to historical support data
     int dirty_stacks;                  // extents need dynamic storage clean
+    struct stacks_extent *read;        // an extent used for active reads
+    proc_t*(*read_something)(PROCTAB*, proc_t*); // readproc/readeither via which
     unsigned pgs2k_shift;              // to convert some proc vaules
     unsigned flags;                    // the old library PROC_FILL flagss
     PROCTAB *PT;                       // the old library essential interface
@@ -942,6 +946,58 @@ PROCPS_EXPORT int procps_pids_new (
 } // end: procps_pids_new
 
 
+PROCPS_EXPORT struct pids_stack *procps_pids_read_next (
+        struct procps_pidsinfo *info)
+{
+    static proc_t task;    // static for initial zeroes + later dynamic free(s)
+
+    if (info == NULL || ! READS_BEGUN)
+        return NULL;
+    if (info->dirty_stacks) {
+        cleanup_stack(info->read->stacks[0]->head, info->maxitems);
+        info->dirty_stacks = 0;
+    }
+    if (NULL == info->read_something(info->PT, &task))
+        return NULL;
+    assign_results(info, info->read->stacks[0], &task);
+    return info->read->stacks[0];
+} // end: procps_pids_read_next
+
+
+PROCPS_EXPORT int procps_pids_read_open (
+        struct procps_pidsinfo *info,
+        enum pids_reap_type which)
+{
+    if (info == NULL || READS_BEGUN)
+        return -EINVAL;
+    if (!info->maxitems && !info->curitems)
+        return -EINVAL;
+    if (which != PROCPS_REAP_TASKS_ONLY && which != PROCPS_REAP_THREADS_TOO)
+        return -EINVAL;
+
+    if (!(info->read = (struct stacks_extent *)procps_pids_stacks_alloc(info, 1)))
+        return -ENOMEM;
+    if (!oldproc_open(info, 0))
+        return -1;
+    info->read_something = which ? readeither : readproc;
+    return 0;
+} // end: procps_pids_read_open
+
+
+PROCPS_EXPORT int procps_pids_read_shut (
+        struct procps_pidsinfo *info)
+{
+    int rc;
+
+    if (info == NULL || ! READS_BEGUN)
+        return -EINVAL;
+    oldproc_close(info);
+    rc = free_extent(info, info->read);
+    info->read = NULL;
+    return rc;
+} // end: procps_pids_read_shut
+
+
 /* procps_pids_reap():
  *
  * Harvest all the available tasks/threads and provide the result
@@ -960,7 +1016,7 @@ PROCPS_EXPORT struct pids_reap *procps_pids_reap (
     struct pids_stacks *ext;
     int n_save = n_alloc;
 
-    if (info == NULL)
+    if (info == NULL || READS_BEGUN)
         return NULL;
     if (!info->maxitems && !info->curitems)
         return NULL;
@@ -1162,7 +1218,7 @@ PROCPS_EXPORT struct pids_counts *procps_pids_stacks_fill (
     unsigned ids[FILL_ID_MAX + 1];
     int i;
 
-    if (info == NULL || these == NULL)
+    if (info == NULL || these == NULL || READS_BEGUN)
         return NULL;
     if (these->stacks == NULL || these->stacks[0] == NULL)
         return NULL;
index e8e21ec3aab17f031a05fdf9bcab2478e4856881..b4d49bbfe468fb51c659ec2b2cb8a22a76d3c77f 100644 (file)
@@ -191,6 +191,16 @@ int procps_pids_new (
     int maxitems,
     enum pids_item *items);
 
+struct pids_stack *procps_pids_read_next (
+    struct procps_pidsinfo *info);
+
+int procps_pids_read_open (
+    struct procps_pidsinfo *info,
+    enum pids_reap_type which);
+
+int procps_pids_read_shut (
+    struct procps_pidsinfo *info);
+
 struct pids_reap *procps_pids_reap (
     struct procps_pidsinfo *info,
     enum pids_reap_type which);