s3: Use the status from cli_raw_ioctl in torture_ioctl_test
[samba.git] / source3 / nmbd / nmbd_serverlistdb.c
index b4c6e2902dee692d8a6cb538c22cd4377d9064da..f3fba51f4a828999c079280a5751a7638f6c24f3 100644 (file)
@@ -7,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
 */
 
 #include "includes.h"
 
-extern int ClientNMB;
-
 int updatecount = 0;
 
 /*******************************************************************
@@ -39,15 +36,7 @@ void remove_all_servers(struct work_record *work)
        for (servrec = work->serverlist; servrec; servrec = nexts) {
                DEBUG(7,("remove_all_servers: Removing server %s\n",servrec->serv.name));
                nexts = servrec->next;
-
-               if (servrec->prev)
-                       servrec->prev->next = servrec->next;
-               if (servrec->next)
-                       servrec->next->prev = servrec->prev;
-
-               if (work->serverlist == servrec)
-                       work->serverlist = servrec->next;
-
+               DLIST_REMOVE(work->serverlist, servrec);
                ZERO_STRUCTP(servrec);
                SAFE_FREE(servrec);
        }
@@ -62,21 +51,7 @@ void remove_all_servers(struct work_record *work)
 static void add_server_to_workgroup(struct work_record *work,
                              struct server_record *servrec)
 {
-       struct server_record *servrec2;
-
-       if (!work->serverlist) {
-               work->serverlist = servrec;
-               servrec->prev = NULL;
-               servrec->next = NULL;
-               return;
-       }
-
-       for (servrec2 = work->serverlist; servrec2->next; servrec2 = servrec2->next)
-               ;
-
-       servrec2->next = servrec;
-       servrec->next = NULL;
-       servrec->prev = servrec2;
+       DLIST_ADD_END(work->serverlist, servrec, struct server_record *);
        work->subnet->work_changed = True;
 }
 
@@ -102,14 +77,7 @@ struct server_record *find_server_in_workgroup(struct work_record *work, const c
 
 void remove_server_from_workgroup(struct work_record *work, struct server_record *servrec)
 {
-       if (servrec->prev)
-               servrec->prev->next = servrec->next;
-       if (servrec->next)
-               servrec->next->prev = servrec->prev;
-
-       if (work->serverlist == servrec) 
-               work->serverlist = servrec->next; 
-
+       DLIST_REMOVE(work->serverlist, servrec);
        ZERO_STRUCTP(servrec);
        SAFE_FREE(servrec);
        work->subnet->work_changed = True;
@@ -131,13 +99,13 @@ struct server_record *create_server_on_workgroup(struct work_record *work,
                return (NULL);
        }
   
-       if((servrec = find_server_in_workgroup(work, name)) != NULL) {
+       if(find_server_in_workgroup(work, name) != NULL) {
                DEBUG(0,("create_server_on_workgroup: Server %s already exists on \
 workgroup %s. This is a bug.\n", name, work->work_group));
                return NULL;
        }
   
-       if((servrec = (struct server_record *)malloc(sizeof(*servrec))) == NULL) {
+       if((servrec = SMB_MALLOC_P(struct server_record)) == NULL) {
                DEBUG(0,("create_server_entry_on_workgroup: malloc fail !\n"));
                return NULL;
        }
@@ -216,7 +184,7 @@ static uint32 write_this_server_name( struct subnet_record *subrec,
        struct work_record *iwork;
 
        /* Go through all the subnets we have already seen. */
-       for (ssub = FIRST_SUBNET; ssub != subrec; ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub)) {
+       for (ssub = FIRST_SUBNET; ssub && (ssub != subrec); ssub = NEXT_SUBNET_INCLUDING_UNICAST(ssub)) {
                for(iwork = ssub->workgrouplist; iwork; iwork = iwork->next) {
                        if(find_server_in_workgroup( iwork, servrec->serv.name) != NULL) {
                                /*
@@ -291,18 +259,20 @@ void write_browse_list_entry(XFILE *fp, const char *name, uint32 rec_type,
        x_fprintf(fp, "\"%s\"\n", description);
 }
 
-void write_browse_list(time_t t, BOOL force_write)
-{   
+void write_browse_list(time_t t, bool force_write)
+{
        struct subnet_record *subrec;
        struct work_record *work;
        struct server_record *servrec;
-       pstring fname,fnamenew;
+       char *fname;
+       char *fnamenew;
        uint32 stype;
        int i;
        XFILE *fp;
-       BOOL list_changed = force_write;
+       bool list_changed = force_write;
        static time_t lasttime = 0;
-    
+       TALLOC_CTX *ctx = talloc_tos();
+
        /* Always dump if we're being told to by a signal. */
        if(force_write == False) {
                if (!lasttime)
@@ -314,7 +284,7 @@ void write_browse_list(time_t t, BOOL force_write)
        lasttime = t;
 
        dump_workgroups(force_write);
+
        for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) {
                if(subrec->work_changed) {
                        list_changed = True;
@@ -326,22 +296,25 @@ void write_browse_list(time_t t, BOOL force_write)
                return;
 
        updatecount++;
-    
-       pstrcpy(fname,lp_lockdir());
-       trim_string(fname,NULL,"/");
-       pstrcat(fname,"/");
-       pstrcat(fname,SERVER_LIST);
-       pstrcpy(fnamenew,fname);
-       pstrcat(fnamenew,".");
+
+       fname = cache_path(SERVER_LIST);
+       if (!fname) {
+               return;
+       }
+       fnamenew = talloc_asprintf(ctx, "%s.",
+                               fname);
+       if (!fnamenew) {
+               return;
+       }
+
        fp = x_fopen(fnamenew,O_WRONLY|O_CREAT|O_TRUNC, 0644);
+
        if (!fp) {
                DEBUG(0,("write_browse_list: Can't open file %s. Error was %s\n",
                        fnamenew,strerror(errno)));
                return;
-       } 
-  
+       }
+
        /*
         * Write out a record for our workgroup. Use the record from the first
         * subnet.
@@ -358,7 +331,7 @@ void write_browse_list(time_t t, BOOL force_write)
                SV_TYPE_DOMAIN_ENUM|SV_TYPE_NT|SV_TYPE_LOCAL_LIST_ONLY,
                work->local_master_browser_name, work->work_group);
 
-       /* 
+       /*
         * We need to do something special for our own names.
         * This is due to the fact that we may be a local master browser on
         * one of our broadcast subnets, and a domain master on the unicast
@@ -381,7 +354,7 @@ void write_browse_list(time_t t, BOOL force_write)
                write_browse_list_entry(fp, my_netbios_names(i), stype,
                        string_truncate(lp_serverstring(), MAX_SERVER_STRING_LENGTH), lp_workgroup());
        }
-      
+
        for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec)) { 
                subrec->work_changed = False;
 
@@ -402,7 +375,7 @@ void write_browse_list(time_t t, BOOL force_write)
 
                                /* We have already written our names here. */
                                if(is_myname(servrec->serv.name))
-                                       continue; 
+                                       continue;
 
                                serv_type = write_this_server_name(subrec, work, servrec);
                                if(serv_type) {
@@ -412,8 +385,8 @@ void write_browse_list(time_t t, BOOL force_write)
                                }
                        }
                }
-       } 
-  
+       }
+
        x_fclose(fp);
        unlink(fname);
        chmod(fnamenew,0644);