mirror of
https://github.com/samba-team/samba.git
synced 2025-08-03 04:22:09 +03:00
implemented talloc() as described on samba-technical. This fixes the
lp_string() bug properly. we still need to add lp_talloc_free() calls in all the main event loops, I've only put it in smbd and nmbd thus far.
This commit is contained in:
@ -99,7 +99,7 @@ LIB_OBJ = lib/charcnv.o lib/charset.o lib/debug.o lib/fault.o \
|
|||||||
lib/util_array.o lib/util_str.o lib/util_sid.o \
|
lib/util_array.o lib/util_str.o lib/util_sid.o \
|
||||||
lib/util_unistr.o lib/util_file.o \
|
lib/util_unistr.o lib/util_file.o \
|
||||||
lib/util.o lib/util_sock.o lib/util_sec.o smbd/ssl.o lib/fnmatch.o \
|
lib/util.o lib/util_sock.o lib/util_sec.o smbd/ssl.o lib/fnmatch.o \
|
||||||
tdb/tdb.o
|
tdb/tdb.o lib/talloc.o
|
||||||
|
|
||||||
UBIQX_OBJ = ubiqx/ubi_BinTree.o ubiqx/ubi_Cache.o ubiqx/ubi_SplayTree.o \
|
UBIQX_OBJ = ubiqx/ubi_BinTree.o ubiqx/ubi_Cache.o ubiqx/ubi_SplayTree.o \
|
||||||
ubiqx/ubi_dLinkList.o ubiqx/ubi_sLinkList.o ubiqx/debugparse.o
|
ubiqx/ubi_dLinkList.o ubiqx/ubi_sLinkList.o ubiqx/debugparse.o
|
||||||
|
@ -611,6 +611,7 @@ extern int errno;
|
|||||||
#include "ubi_dLinkList.h"
|
#include "ubi_dLinkList.h"
|
||||||
#include "dlinklist.h"
|
#include "dlinklist.h"
|
||||||
#include "../tdb/tdb.h"
|
#include "../tdb/tdb.h"
|
||||||
|
#include "talloc.h"
|
||||||
#include "interfaces.h"
|
#include "interfaces.h"
|
||||||
|
|
||||||
#ifdef HAVE_FNMATCH
|
#ifdef HAVE_FNMATCH
|
||||||
|
@ -223,6 +223,12 @@ smb_ucs2_t *wsys_getwd(smb_ucs2_t *s);
|
|||||||
int wsys_chown(const smb_ucs2_t *wfname, uid_t uid, gid_t gid);
|
int wsys_chown(const smb_ucs2_t *wfname, uid_t uid, gid_t gid);
|
||||||
int wsys_chroot(const smb_ucs2_t *wfname);
|
int wsys_chroot(const smb_ucs2_t *wfname);
|
||||||
|
|
||||||
|
/*The following definitions come from lib/talloc.c */
|
||||||
|
|
||||||
|
TALLOC_CTX *talloc_init(void);
|
||||||
|
void *talloc(TALLOC_CTX *t, size_t size);
|
||||||
|
void talloc_destroy(TALLOC_CTX *t);
|
||||||
|
|
||||||
/*The following definitions come from lib/time.c */
|
/*The following definitions come from lib/time.c */
|
||||||
|
|
||||||
void GetTimeOfDay(struct timeval *tval);
|
void GetTimeOfDay(struct timeval *tval);
|
||||||
@ -1111,6 +1117,7 @@ void expire_workgroups_and_servers(time_t t);
|
|||||||
|
|
||||||
/*The following definitions come from param/loadparm.c */
|
/*The following definitions come from param/loadparm.c */
|
||||||
|
|
||||||
|
void lp_talloc_free(void);
|
||||||
char *lp_logfile(void);
|
char *lp_logfile(void);
|
||||||
char *lp_smbrun(void);
|
char *lp_smbrun(void);
|
||||||
char *lp_configfile(void);
|
char *lp_configfile(void);
|
||||||
|
32
source/include/talloc.h
Normal file
32
source/include/talloc.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
Unix SMB/Netbios implementation.
|
||||||
|
Version 3.0
|
||||||
|
Samba temporary memory allocation functions
|
||||||
|
Copyright (C) Andrew Tridgell 2000
|
||||||
|
|
||||||
|
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
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct talloc_chunk {
|
||||||
|
struct talloc_chunk *next;
|
||||||
|
void *ptr;
|
||||||
|
size_t alloc_size;
|
||||||
|
size_t total_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
struct talloc_chunk *list;
|
||||||
|
} TALLOC_CTX;
|
||||||
|
|
96
source/lib/talloc.c
Normal file
96
source/lib/talloc.c
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
Unix SMB/Netbios implementation.
|
||||||
|
Version 3.0
|
||||||
|
Samba temporary memory allocation functions
|
||||||
|
Copyright (C) Andrew Tridgell 2000
|
||||||
|
|
||||||
|
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
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* this is a very simple temporary memory allocator. To use it do the following:
|
||||||
|
|
||||||
|
1) when you first want to allocate a pool of meomry use
|
||||||
|
talloc_init() and save the resulting context pointer somewhere
|
||||||
|
|
||||||
|
2) to allocate memory use talloc()
|
||||||
|
|
||||||
|
3) when _all_ of the memory allocated using this context is no longer needed
|
||||||
|
use talloc_destroy()
|
||||||
|
|
||||||
|
talloc does not zero the memory. It guarantees memory of a
|
||||||
|
TALLOC_ALIGN alignment
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "includes.h"
|
||||||
|
|
||||||
|
#define TALLOC_ALIGN 32
|
||||||
|
#define TALLOC_CHUNK_SIZE (0x2000)
|
||||||
|
|
||||||
|
/* initialissa talloc context. */
|
||||||
|
TALLOC_CTX *talloc_init(void)
|
||||||
|
{
|
||||||
|
TALLOC_CTX *t;
|
||||||
|
|
||||||
|
t = (TALLOC_CTX *)malloc(sizeof(*t));
|
||||||
|
if (!t) return NULL;
|
||||||
|
|
||||||
|
t->list = NULL;
|
||||||
|
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* allocate a bit of memory from the specified pool */
|
||||||
|
void *talloc(TALLOC_CTX *t, size_t size)
|
||||||
|
{
|
||||||
|
void *p;
|
||||||
|
|
||||||
|
size = (size + TALLOC_ALIGN) & (~TALLOC_ALIGN-1);
|
||||||
|
|
||||||
|
if (!t->list || (t->list->total_size - t->list->alloc_size) < size) {
|
||||||
|
struct talloc_chunk *c;
|
||||||
|
size_t asize = (size + TALLOC_CHUNK_SIZE) & ~(TALLOC_CHUNK_SIZE-1);
|
||||||
|
|
||||||
|
c = (struct talloc_chunk *)malloc(sizeof(*c));
|
||||||
|
if (!c) return NULL;
|
||||||
|
c->next = t->list;
|
||||||
|
c->ptr = (void *)malloc(asize);
|
||||||
|
if (!c->ptr) {
|
||||||
|
free(c);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
c->alloc_size = 0;
|
||||||
|
c->total_size = asize;
|
||||||
|
t->list = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = t->list->ptr + t->list->alloc_size;
|
||||||
|
t->list->alloc_size += size;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* destroy a whole pool */
|
||||||
|
void talloc_destroy(TALLOC_CTX *t)
|
||||||
|
{
|
||||||
|
struct talloc_chunk *c;
|
||||||
|
|
||||||
|
while (t->list) {
|
||||||
|
c = t->list->next;
|
||||||
|
free(t->list->ptr);
|
||||||
|
free(t->list);
|
||||||
|
t->list = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(t);
|
||||||
|
}
|
@ -475,6 +475,9 @@ static void process(void)
|
|||||||
|
|
||||||
/* check for new network interfaces */
|
/* check for new network interfaces */
|
||||||
reload_interfaces(t);
|
reload_interfaces(t);
|
||||||
|
|
||||||
|
/* free up temp memory */
|
||||||
|
lp_talloc_free();
|
||||||
}
|
}
|
||||||
} /* process */
|
} /* process */
|
||||||
|
|
||||||
|
@ -1092,55 +1092,43 @@ static void init_locals(void)
|
|||||||
|
|
||||||
#define NUMBER_OF_STATIC_STRING_BUFS 20
|
#define NUMBER_OF_STATIC_STRING_BUFS 20
|
||||||
|
|
||||||
|
static TALLOC_CTX *lp_talloc;
|
||||||
|
|
||||||
/******************************************************************* a
|
/******************************************************************* a
|
||||||
convenience routine to grab string parameters into a rotating buffer,
|
free up temporary memory - called from the main loop
|
||||||
|
********************************************************************/
|
||||||
|
void lp_talloc_free(void)
|
||||||
|
{
|
||||||
|
if (!lp_talloc) return;
|
||||||
|
talloc_destroy(lp_talloc);
|
||||||
|
lp_talloc = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************
|
||||||
|
convenience routine to grab string parameters into temporary memory
|
||||||
and run standard_sub_basic on them. The buffers can be written to by
|
and run standard_sub_basic on them. The buffers can be written to by
|
||||||
callers without affecting the source string.
|
callers without affecting the source string.
|
||||||
********************************************************************/
|
********************************************************************/
|
||||||
static char *lp_string(const char *s)
|
static char *lp_string(const char *s)
|
||||||
{
|
{
|
||||||
static char *bufs[NUMBER_OF_STATIC_STRING_BUFS];
|
size_t len = s?strlen(s):0;
|
||||||
static size_t buflen[NUMBER_OF_STATIC_STRING_BUFS];
|
char *ret;
|
||||||
static int next = -1;
|
|
||||||
char *ret;
|
|
||||||
int i;
|
|
||||||
size_t len = s?strlen(s):0;
|
|
||||||
|
|
||||||
if (next == -1) {
|
if (!lp_talloc) lp_talloc = talloc_init();
|
||||||
/* initialisation */
|
|
||||||
for (i=0;i<NUMBER_OF_STATIC_STRING_BUFS;i++) {
|
ret = (char *)talloc(lp_talloc, len + 100); /* leave room for substitution */
|
||||||
bufs[i] = NULL;
|
|
||||||
buflen[i] = 0;
|
|
||||||
}
|
|
||||||
next = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = MAX(len+100,sizeof(pstring)); /* the +100 is for some
|
if (!ret) return NULL;
|
||||||
substitution room */
|
|
||||||
|
|
||||||
if (buflen[next] != len) {
|
if (!s)
|
||||||
buflen[next] = len;
|
*ret = 0;
|
||||||
if (bufs[next])
|
else
|
||||||
free(bufs[next]);
|
StrnCpy(ret,s,len);
|
||||||
bufs[next] = (char *)malloc(len);
|
|
||||||
if (!bufs[next]) {
|
|
||||||
DEBUG(0,("out of memory in lp_string()"));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = &bufs[next][0];
|
trim_string(ret, "\"", "\"");
|
||||||
next = (next+1)%NUMBER_OF_STATIC_STRING_BUFS;
|
|
||||||
|
|
||||||
if (!s)
|
standard_sub_basic(ret);
|
||||||
*ret = 0;
|
return(ret);
|
||||||
else
|
|
||||||
StrnCpy(ret,s,len-1);
|
|
||||||
|
|
||||||
trim_string(ret, "\"", "\"");
|
|
||||||
|
|
||||||
standard_sub_basic(ret);
|
|
||||||
return(ret);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -2601,7 +2589,7 @@ BOOL lp_load(char *pszFname,BOOL global_only, BOOL save_defaults, BOOL add_ipc)
|
|||||||
{
|
{
|
||||||
pstring n2;
|
pstring n2;
|
||||||
BOOL bRetval;
|
BOOL bRetval;
|
||||||
|
|
||||||
add_to_file_list(pszFname);
|
add_to_file_list(pszFname);
|
||||||
|
|
||||||
bRetval = False;
|
bRetval = False;
|
||||||
|
@ -98,7 +98,7 @@ END {
|
|||||||
gotstart = 1;
|
gotstart = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( $0 ~ /^TDB_CONTEXT|^TDB_DATA|^smb_ucs2_t/ ) {
|
if( $0 ~ /^TDB_CONTEXT|^TDB_DATA|^smb_ucs2_t|^TALLOC_CTX/ ) {
|
||||||
gotstart = 1;
|
gotstart = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1018,6 +1018,9 @@ void smbd_process(void)
|
|||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
|
/* free up temporary memory */
|
||||||
|
lp_talloc_free();
|
||||||
|
|
||||||
while(!receive_message_or_smb(InBuffer,BUFFER_SIZE,select_timeout,&got_smb))
|
while(!receive_message_or_smb(InBuffer,BUFFER_SIZE,select_timeout,&got_smb))
|
||||||
{
|
{
|
||||||
if(!timeout_processing( deadtime, &select_timeout, &last_timeout_processing_time))
|
if(!timeout_processing( deadtime, &select_timeout, &last_timeout_processing_time))
|
||||||
|
Reference in New Issue
Block a user