bos: convert struct bnode_proc to use opr
[openafs.git] / src / bozo / bnode.c
index 1d080ab..a806cec 100644 (file)
 #include <afs/audit.h>
 #include <afs/afsutil.h>
 #include <afs/fileutil.h>
+#include <opr/queue.h>
 
 #include "bnode.h"
+#include "bnode_internal.h"
 #include "bosprototypes.h"
 
 #ifndef WCOREDUMP
@@ -34,8 +36,8 @@
 int bnode_waiting = 0;
 static PROCESS bproc_pid;      /* pid of waker-upper */
 static struct bnode *allBnodes = 0;    /* list of all bnodes */
-static struct bnode_proc *allProcs = 0;        /* list of all processes for which we're waiting */
-static struct bnode_type *allTypes = 0;        /* list of registered type handlers */
+static struct opr_queue allProcs;      /**< List of all processes for which we're waiting */
+static struct opr_queue allTypes;      /**< List of all registered type handlers */
 
 static struct bnode_stats {
     int weirdPids;
@@ -110,7 +112,6 @@ SaveCore(struct bnode *abnode, struct bnode_proc
     if (code) {
         DIR *logdir;
         struct dirent *file;
-        size_t length;
         unsigned long pid;
        const char *coredir = AFSDIR_LOGS_DIR;
 
@@ -125,13 +126,11 @@ SaveCore(struct bnode *abnode, struct bnode_proc
                 continue;
             pid = atol(file->d_name + 5);
             if (pid == aproc->pid) {
-                length = strlen(coredir) + strlen(file->d_name) + 2;
-                corefile = malloc(length);
+                asprintf(&corefile, "%s/%s", coredir, file->d_name);
                 if (corefile == NULL) {
                     closedir(logdir);
                     return;
                 }
-                snprintf(corefile, length, "%s/%s", coredir, file->d_name);
                 code = 0;
                 break;
             }
@@ -152,7 +151,7 @@ SaveCore(struct bnode *abnode, struct bnode_proc
            TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
     strcpy(tbuffer, FileName);
 #endif
-    code = renamefile(corefile, tbuffer);
+    code = rk_rename(corefile, tbuffer);
     free(corefile);
 }
 
@@ -322,29 +321,32 @@ bnode_FindInstance(char *aname)
 static struct bnode_type *
 FindType(char *aname)
 {
-    struct bnode_type *tt;
+    struct opr_queue *cursor;
+
+    for (opr_queue_Scan(&allTypes, cursor)) {
+       struct bnode_type *tt = opr_queue_Entry(cursor, struct bnode_type, q);
 
-    for (tt = allTypes; tt; tt = tt->next) {
        if (!strcmp(tt->name, aname))
            return tt;
     }
-    return (struct bnode_type *)0;
+    return NULL;
 }
 
 int
 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
 {
-    struct bnode_type *tt;
+    struct opr_queue *cursor;
+    struct bnode_type *tt = NULL;
 
-    for (tt = allTypes; tt; tt = tt->next) {
+    for (opr_queue_Scan(&allTypes, cursor), tt = NULL) {
+       tt = opr_queue_Entry(cursor, struct bnode_type, q);
        if (!strcmp(tt->name, atype))
            break;
     }
     if (!tt) {
-       tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
-       memset(tt, 0, sizeof(struct bnode_type));
-       tt->next = allTypes;
-       allTypes = tt;
+       tt = calloc(1, sizeof(struct bnode_type));
+        opr_queue_Init(&tt->q);
+       opr_queue_Prepend(&allTypes, &tt->q);
        tt->name = atype;
     }
     tt->ops = aprocs;
@@ -515,23 +517,6 @@ bnode_InitBnode(struct bnode *abnode, struct bnode_ops *abnodeops,
     return 0;
 }
 
-static int
-DeleteProc(struct bnode_proc *abproc)
-{
-    struct bnode_proc **pb, *tb;
-    struct bnode_proc *nb;
-
-    for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
-       nb = tb->next;
-       if (tb == abproc) {
-           *pb = nb;
-           free(tb);
-           return 0;
-       }
-    }
-    return BZNOENT;
-}
-
 /* bnode lwp executes this code repeatedly */
 static void *
 bproc(void *unused)
@@ -539,6 +524,7 @@ bproc(void *unused)
     afs_int32 code;
     struct bnode *tb;
     afs_int32 temp;
+    struct opr_queue *cursor;
     struct bnode_proc *tp;
     struct bnode *nb;
     int options;               /* must not be register */
@@ -601,9 +587,12 @@ bproc(void *unused)
                if (code == 0 || code == -1)
                    break;      /* all done */
                /* otherwise code has a process id, which we now search for */
-               for (tp = allProcs; tp; tp = tp->next)
+               for (tp = NULL, opr_queue_Scan(&allProcs, cursor), tp = NULL) {
+                   tp = opr_queue_Entry(cursor, struct bnode_proc, q);
+
                    if (tp->pid == code)
                        break;
+               }
                if (tp) {
                    /* found the pid */
                    tb = tp->bnode;
@@ -688,7 +677,8 @@ bproc(void *unused)
                    BOP_PROCEXIT(tb, tp);
                    bnode_Check(tb);
                    bnode_Release(tb);  /* bnode delete can happen here */
-                   DeleteProc(tp);
+                   opr_queue_Remove(&tp->q);
+                   free(tp);
                } else
                    bnode_stats.weirdPids++;
            }
@@ -852,6 +842,8 @@ bnode_Init(void)
     if (initDone)
        return 0;
     initDone = 1;
+    opr_queue_Init(&allTypes);
+    opr_queue_Init(&allProcs);
     memset(&bnode_stats, 0, sizeof(bnode_stats));
     LWP_InitializeProcessSupport(1, &junk);    /* just in case */
     IOMGR_Initialize();
@@ -915,8 +907,7 @@ bnode_ParseLine(char *aline, struct bnode_token **alist)
            if (inToken) {
                inToken = 0;    /* end of this token */
                *tptr++ = 0;
-               ttok =
-                   (struct bnode_token *)malloc(sizeof(struct bnode_token));
+               ttok = malloc(sizeof(struct bnode_token));
                ttok->next = (struct bnode_token *)0;
                ttok->key = strdup(tbuffer);
                if (last) {
@@ -962,9 +953,8 @@ bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
     code = bnode_ParseLine(aexecString, &tlist);       /* try parsing first */
     if (code)
        return code;
-    tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
-    memset(tp, 0, sizeof(struct bnode_proc));
-    tp->next = allProcs;
+    tp = calloc(1, sizeof(struct bnode_proc));
+    opr_queue_Init(&tp->q);
     tp->bnode = abnode;
     tp->comLine = aexecString;
     tp->coreName = coreName;   /* may be null */
@@ -989,7 +979,7 @@ bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
     bozo_Log("%s started pid %ld: %s\n", abnode->name, cpid, aexecString);
 
     bnode_FreeTokens(tlist);
-    allProcs = tp;
+    opr_queue_Prepend(&allProcs, &tp->q);
     *aproc = tp;
     tp->pid = cpid;
     tp->flags = BPROC_STARTED;