Author: manu
Date: Wed Oct  4 08:48:05 2017
New Revision: 324258
URL: https://svnweb.freebsd.org/changeset/base/324258

Log:
  mountd: Convert mountlist to SLIST
  
  Use SLIST from sys/queue.h instead of homebrew linked list for mountlist.
  
  Reviewed by:  bapt, rmacklem
  MFC after:    1 week
  Sponsored by: Gandi.net
  Differential Revision:        https://reviews.freebsd.org/D12504

Modified:
  head/usr.sbin/mountd/mountd.c

Modified: head/usr.sbin/mountd/mountd.c
==============================================================================
--- head/usr.sbin/mountd/mountd.c       Wed Oct  4 08:43:56 2017        
(r324257)
+++ head/usr.sbin/mountd/mountd.c       Wed Oct  4 08:48:05 2017        
(r324258)
@@ -92,9 +92,10 @@ __FBSDID("$FreeBSD$");
  * Structures for keeping the mount list and export list
  */
 struct mountlist {
-       struct mountlist *ml_next;
        char    ml_host[MNTNAMLEN+1];
        char    ml_dirp[MNTPATHLEN+1];
+
+       SLIST_ENTRY(mountlist)  next;
 };
 
 struct dirlist {
@@ -225,7 +226,7 @@ static int  xdr_mlist(XDR *, caddr_t);
 static void    terminate(int);
 
 static SLIST_HEAD(, exportlist) exphead = SLIST_HEAD_INITIALIZER(exphead);
-static struct mountlist *mlhead;
+static SLIST_HEAD(, mountlist) mlhead = SLIST_HEAD_INITIALIZER(mlhead);
 static struct grouplist *grphead;
 static char *exnames_default[2] = { _PATH_EXPORTS, NULL };
 static char **exnames;
@@ -447,7 +448,6 @@ main(int argc, char **argv)
        argc -= optind;
        argv += optind;
        grphead = (struct grouplist *)NULL;
-       mlhead = (struct mountlist *)NULL;
        if (argc > 0)
                exnames = argv;
        else
@@ -1254,8 +1254,7 @@ xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
        int false = 0;
        char *strp;
 
-       mlp = mlhead;
-       while (mlp) {
+       SLIST_FOREACH(mlp, &mlhead, next) {
                if (!xdr_bool(xdrsp, &true))
                        return (0);
                strp = &mlp->ml_host[0];
@@ -1264,7 +1263,6 @@ xdr_mlist(XDR *xdrsp, caddr_t cp __unused)
                strp = &mlp->ml_dirp[0];
                if (!xdr_string(xdrsp, &strp, MNTPATHLEN))
                        return (0);
-               mlp = mlp->ml_next;
        }
        if (!xdr_bool(xdrsp, &false))
                return (0);
@@ -2946,7 +2944,7 @@ parsecred(char *namelist, struct xucred *cr)
 static void
 get_mountlist(void)
 {
-       struct mountlist *mlp, **mlpp;
+       struct mountlist *mlp;
        char *host, *dirp, *cp;
        char str[STRSIZ];
        FILE *mlfile;
@@ -2959,7 +2957,6 @@ get_mountlist(void)
                        return;
                }
        }
-       mlpp = &mlhead;
        while (fgets(str, STRSIZ, mlfile) != NULL) {
                cp = str;
                host = strsep(&cp, " \t\n");
@@ -2973,9 +2970,8 @@ get_mountlist(void)
                mlp->ml_host[MNTNAMLEN] = '\0';
                strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
                mlp->ml_dirp[MNTPATHLEN] = '\0';
-               mlp->ml_next = (struct mountlist *)NULL;
-               *mlpp = mlp;
-               mlpp = &mlp->ml_next;
+
+               SLIST_INSERT_HEAD(&mlhead, mlp, next);
        }
        fclose(mlfile);
 }
@@ -2983,23 +2979,16 @@ get_mountlist(void)
 static void
 del_mlist(char *hostp, char *dirp)
 {
-       struct mountlist *mlp, **mlpp;
-       struct mountlist *mlp2;
+       struct mountlist *mlp, *mlp2;
        FILE *mlfile;
        int fnd = 0;
 
-       mlpp = &mlhead;
-       mlp = mlhead;
-       while (mlp) {
+       SLIST_FOREACH_SAFE(mlp, &mlhead, next, mlp2) {
                if (!strcmp(mlp->ml_host, hostp) &&
                    (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
                        fnd = 1;
-                       mlp2 = mlp;
-                       *mlpp = mlp = mlp->ml_next;
-                       free((caddr_t)mlp2);
-               } else {
-                       mlpp = &mlp->ml_next;
-                       mlp = mlp->ml_next;
+                       SLIST_REMOVE(&mlhead, mlp, mountlist, next);
+                       free((caddr_t)mlp);
                }
        }
        if (fnd) {
@@ -3007,10 +2996,8 @@ del_mlist(char *hostp, char *dirp)
                        syslog(LOG_ERR,"can't update %s", _PATH_RMOUNTLIST);
                        return;
                }
-               mlp = mlhead;
-               while (mlp) {
+               SLIST_FOREACH(mlp, &mlhead, next) {
                        fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
-                       mlp = mlp->ml_next;
                }
                fclose(mlfile);
        }
@@ -3019,17 +3006,14 @@ del_mlist(char *hostp, char *dirp)
 static void
 add_mlist(char *hostp, char *dirp)
 {
-       struct mountlist *mlp, **mlpp;
+       struct mountlist *mlp;
        FILE *mlfile;
 
-       mlpp = &mlhead;
-       mlp = mlhead;
-       while (mlp) {
+       SLIST_FOREACH(mlp, &mlhead, next) {
                if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
                        return;
-               mlpp = &mlp->ml_next;
-               mlp = mlp->ml_next;
        }
+
        mlp = (struct mountlist *)malloc(sizeof (*mlp));
        if (mlp == (struct mountlist *)NULL)
                out_of_mem();
@@ -3037,8 +3021,7 @@ add_mlist(char *hostp, char *dirp)
        mlp->ml_host[MNTNAMLEN] = '\0';
        strncpy(mlp->ml_dirp, dirp, MNTPATHLEN);
        mlp->ml_dirp[MNTPATHLEN] = '\0';
-       mlp->ml_next = (struct mountlist *)NULL;
-       *mlpp = mlp;
+       SLIST_INSERT_HEAD(&mlhead, mlp, next);
        if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
                syslog(LOG_ERR, "can't update %s", _PATH_RMOUNTLIST);
                return;
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to