Copyright (C) Jeremy Allison 2001-2002
Copyright (C) Simo Sorce 2001
Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
-
+ Copyright (C) James Peach 2006
+
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,
enum protocol_types Protocol = PROTOCOL_COREPLUS;
/* a default finfo structure to ensure all fields are sensible */
-file_info def_finfo = {-1,0,0,0,0,0,0,"",""};
+file_info def_finfo;
/* this is used by the chaining code */
int chain_size = 0;
gfree_case_tables();
gfree_debugsyms();
gfree_charcnv();
- gfree_messsges();
+ gfree_interfaces();
/* release the talloc null_context memory last */
- talloc_nc_free();
+ talloc_disable_null_tracking();
}
const char *my_netbios_names(int i)
Add a gid to an array of gids if it's not already there.
****************************************************************************/
-void add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
+BOOL add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
gid_t **gids, size_t *num_gids)
{
int i;
- for (i=0; i<*num_gids; i++) {
- if ((*gids)[i] == gid)
- return;
+ if ((*num_gids != 0) && (*gids == NULL)) {
+ /*
+ * A former call to this routine has failed to allocate memory
+ */
+ return False;
}
- if (mem_ctx != NULL) {
- *gids = TALLOC_REALLOC_ARRAY(mem_ctx, *gids, gid_t, *num_gids+1);
- } else {
- *gids = SMB_REALLOC_ARRAY(*gids, gid_t, *num_gids+1);
+ for (i=0; i<*num_gids; i++) {
+ if ((*gids)[i] == gid) {
+ return True;
+ }
}
+ *gids = TALLOC_REALLOC_ARRAY(mem_ctx, *gids, gid_t, *num_gids+1);
if (*gids == NULL) {
- return;
+ *num_gids = 0;
+ return False;
}
(*gids)[*num_gids] = gid;
*num_gids += 1;
+ return True;
}
/****************************************************************************
if (DEBUGLEVEL < 50)
bcc = MIN(bcc, 512);
- dump_data(10, smb_buf(buf), bcc);
+ dump_data(10, (uint8 *)smb_buf(buf), bcc);
}
/*******************************************************************
- Set the length and marker of an smb packet.
+ Set the length and marker of an encrypted smb packet.
********************************************************************/
-void smb_setlen(char *buf,int len)
+void smb_set_enclen(char *buf,int len,uint16 enc_ctx_num)
{
_smb_setlen(buf,len);
SCVAL(buf,4,0xFF);
- SCVAL(buf,5,'S');
- SCVAL(buf,6,'M');
- SCVAL(buf,7,'B');
+ SCVAL(buf,5,'E');
+ SSVAL(buf,6,enc_ctx_num);
+}
+
+/*******************************************************************
+ Set the length and marker of an smb packet.
+********************************************************************/
+
+void smb_setlen(const char *frombuf, char *buf, int len)
+{
+ _smb_setlen(buf,len);
+
+ if (frombuf) {
+ if (buf != frombuf) {
+ memcpy(buf+4, frombuf+4, 4);
+ }
+ } else {
+ SCVAL(buf,4,0xFF);
+ SCVAL(buf,5,'S');
+ SCVAL(buf,6,'M');
+ SCVAL(buf,7,'B');
+ }
}
/*******************************************************************
Setup the word count and byte count for a smb message.
********************************************************************/
-int set_message(char *buf,int num_words,int num_bytes,BOOL zero)
+int set_message(const char *frombuf, char *buf,int num_words,int num_bytes,BOOL zero)
{
- if (zero)
+ if (zero && (num_words || num_bytes)) {
memset(buf + smb_size,'\0',num_words*2 + num_bytes);
+ }
SCVAL(buf,smb_wct,num_words);
SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
- smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
+ smb_setlen(frombuf, buf,smb_size + num_words*2 + num_bytes - 4);
return (smb_size + num_words*2 + num_bytes);
}
Setup only the byte count for a smb message.
********************************************************************/
-int set_message_bcc(char *buf,int num_bytes)
+int set_message_bcc(const char *frombuf, char *buf,int num_bytes)
{
int num_words = CVAL(buf,smb_wct);
SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes);
- smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4);
+ smb_setlen(frombuf, buf,smb_size + num_words*2 + num_bytes - 4);
return (smb_size + num_words*2 + num_bytes);
}
message as a marker.
********************************************************************/
-int set_message_end(void *outbuf,void *end_ptr)
+int set_message_end(const char *frombuf, void *outbuf,void *end_ptr)
{
- return set_message_bcc((char *)outbuf,PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
+ return set_message_bcc(frombuf,
+ (char *)outbuf,
+ PTR_DIFF(end_ptr,smb_buf((char *)outbuf)));
}
/*******************************************************************
/* remove any double slashes */
all_string_sub(s, "\\\\", "\\", 0);
+ /* Remove leading .\\ characters */
+ if(strncmp(s, ".\\", 2) == 0) {
+ trim_string(s, ".\\", NULL);
+ if(*s == 0)
+ pstrcpy(s,".\\");
+ }
+
while ((p = strstr_m(s,"\\..\\")) != NULL) {
pstring s1;
}
trim_string(s,NULL,"\\..");
-
all_string_sub(s, "\\.\\", "\\", 0);
}
}
trim_string(s,NULL,"/..");
+ all_string_sub(s, "/./", "/", 0);
+}
+
+void clean_name(char *s)
+{
+ dos_clean_name(s);
+ unix_clean_name(s);
}
/*******************************************************************
size_t num_to_read_thistime;
size_t num_written = 0;
- if ((buf = SMB_MALLOC(TRANSFER_BUF_SIZE)) == NULL)
+ if ((buf = SMB_MALLOC_ARRAY(char, TRANSFER_BUF_SIZE)) == NULL)
return -1;
while (total < n) {
void *malloc_(size_t size)
{
+ if (size == 0) {
+ return NULL;
+ }
#undef malloc
return malloc(size);
#define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
static void *calloc_(size_t count, size_t size)
{
+ if (size == 0 || count == 0) {
+ return NULL;
+ }
#undef calloc
return calloc(count, size);
#define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
return NULL;
}
+ if (el_size == 0 || count == 0) {
+ return NULL;
+ }
#if defined(PARANOID_MALLOC_CHECKER)
return malloc_(el_size*count);
#else
#endif
}
+/****************************************************************************
+ Type-safe memalign
+****************************************************************************/
+
+void *memalign_array(size_t el_size, size_t align, unsigned int count)
+{
+ if (count >= MAX_ALLOC_SIZE/el_size) {
+ return NULL;
+ }
+
+ return sys_memalign(align, el_size*count);
+}
+
/****************************************************************************
Type-safe calloc.
****************************************************************************/
if (nmemb >= MAX_ALLOC_SIZE/size) {
return NULL;
}
+ if (size == 0 || nmemb == 0) {
+ return NULL;
+ }
#if defined(PARANOID_MALLOC_CHECKER)
return calloc_(nmemb, size);
#else
****************************************************************************/
void add_to_large_array(TALLOC_CTX *mem_ctx, size_t element_size,
- void *element, void **array, uint32 *num_elements,
+ void *element, void *_array, uint32 *num_elements,
ssize_t *array_size)
{
+ void **array = (void **)_array;
+
if (*array_size < 0) {
return;
}
goto error;
}
- if (mem_ctx != NULL) {
- *array = TALLOC(mem_ctx, element_size * (*array_size));
- } else {
- *array = SMB_MALLOC(element_size * (*array_size));
- }
-
+ *array = TALLOC(mem_ctx, element_size * (*array_size));
if (*array == NULL) {
goto error;
}
goto error;
}
- if (mem_ctx != NULL) {
- *array = TALLOC_REALLOC(mem_ctx, *array,
- element_size * (*array_size));
- } else {
- *array = SMB_REALLOC(*array,
- element_size * (*array_size));
- }
+ *array = TALLOC_REALLOC(mem_ctx, *array,
+ element_size * (*array_size));
if (*array == NULL) {
goto error;
Check if a process exists. Does this work on all unixes?
****************************************************************************/
-BOOL process_exists(const struct process_id pid)
+BOOL process_exists(const struct server_id pid)
{
- if (!procid_is_local(&pid)) {
- /* This *SEVERELY* needs fixing. */
+ if (procid_is_me(&pid)) {
return True;
}
- /* Doing kill with a non-positive pid causes messages to be
- * sent to places we don't want. */
- SMB_ASSERT(pid.pid > 0);
- return(kill(pid.pid,0) == 0 || errno != ESRCH);
+ if (procid_is_local(&pid)) {
+ return (kill(pid.pid,0) == 0 || errno != ESRCH);
+ }
+
+#ifdef CLUSTER_SUPPORT
+ return ctdbd_process_exists(messaging_ctdbd_connection(), pid.vnn,
+ pid.pid);
+#else
+ return False;
+#endif
}
BOOL process_exists_by_pid(pid_t pid)
{
- return process_exists(pid_to_procid(pid));
+ /* Doing kill with a non-positive pid causes messages to be
+ * sent to places we don't want. */
+ SMB_ASSERT(pid > 0);
+ return(kill(pid,0) == 0 || errno != ESRCH);
}
/*******************************************************************
(unsigned long long)sys_getpid(), why));
log_stack_trace();
- /* only smbd needs to decrement the smbd counter in connections.tdb */
- decrement_smbd_process_count();
-
cmd = lp_panic_action();
if (cmd && *cmd) {
DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
exit shortly after calling it.
********************************************************************/
+#ifdef HAVE_LIBUNWIND_H
+#include <libunwind.h>
+#endif
+
+#ifdef HAVE_EXECINFO_H
+#include <execinfo.h>
+#endif
+
#ifdef HAVE_LIBEXC_H
#include <libexc.h>
#endif
void log_stack_trace(void)
{
-#ifdef HAVE_BACKTRACE_SYMBOLS
+#ifdef HAVE_LIBUNWIND
+ /* Try to use libunwind before any other technique since on ia64
+ * libunwind correctly walks the stack in more circumstances than
+ * backtrace.
+ */
+ unw_cursor_t cursor;
+ unw_context_t uc;
+ unsigned i = 0;
+
+ char procname[256];
+ unw_word_t ip, sp, off;
+
+ procname[sizeof(procname) - 1] = '\0';
+
+ if (unw_getcontext(&uc) != 0) {
+ goto libunwind_failed;
+ }
+
+ if (unw_init_local(&cursor, &uc) != 0) {
+ goto libunwind_failed;
+ }
+
+ DEBUG(0, ("BACKTRACE:\n"));
+
+ do {
+ ip = sp = 0;
+ unw_get_reg(&cursor, UNW_REG_IP, &ip);
+ unw_get_reg(&cursor, UNW_REG_SP, &sp);
+
+ switch (unw_get_proc_name(&cursor,
+ procname, sizeof(procname) - 1, &off) ) {
+ case 0:
+ /* Name found. */
+ case -UNW_ENOMEM:
+ /* Name truncated. */
+ DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
+ i, procname, (long long)off,
+ (long long)ip, (long long) sp));
+ break;
+ default:
+ /* case -UNW_ENOINFO: */
+ /* case -UNW_EUNSPEC: */
+ /* No symbol name found. */
+ DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
+ i, "<unknown symbol>",
+ (long long)ip, (long long) sp));
+ }
+ ++i;
+ } while (unw_step(&cursor) > 0);
+
+ return;
+
+libunwind_failed:
+ DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
+
+#elif HAVE_BACKTRACE_SYMBOLS
void *backtrace_stack[BACKTRACE_STACK_SIZE];
size_t backtrace_size;
char **backtrace_strings;
* libexc(3) for details. Apparantly trace_back_stack leaks memory, but
* since we are about to abort anyway, it hardly matters.
*/
- {
#define NAMESIZE 32 /* Arbitrary */
- __uint64_t addrs[BACKTRACE_STACK_SIZE];
- char * names[BACKTRACE_STACK_SIZE];
- char namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
+ __uint64_t addrs[BACKTRACE_STACK_SIZE];
+ char * names[BACKTRACE_STACK_SIZE];
+ char namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
- int i;
- int levels;
+ int i;
+ int levels;
- ZERO_ARRAY(addrs);
- ZERO_ARRAY(names);
- ZERO_ARRAY(namebuf);
+ ZERO_ARRAY(addrs);
+ ZERO_ARRAY(names);
+ ZERO_ARRAY(namebuf);
- /* We need to be root so we can open our /proc entry to walk
- * our stack. It also helps when we want to dump core.
- */
- become_root();
+ /* We need to be root so we can open our /proc entry to walk
+ * our stack. It also helps when we want to dump core.
+ */
+ become_root();
- for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
- names[i] = namebuf + (i * NAMESIZE);
- }
+ for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
+ names[i] = namebuf + (i * NAMESIZE);
+ }
- levels = trace_back_stack(0, addrs, names,
- BACKTRACE_STACK_SIZE, NAMESIZE - 1);
+ levels = trace_back_stack(0, addrs, names,
+ BACKTRACE_STACK_SIZE, NAMESIZE - 1);
- DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
- for (i = 0; i < levels; i++) {
- DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
- }
- }
+ DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
+ for (i = 0; i < levels; i++) {
+ DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
+ }
#undef NAMESIZE
+
#else
DEBUG(0, ("unable to produce a stack trace on this platform\n"));
#endif
SMB_STRUCT_FLOCK lock;
int ret;
- DEBUG(8,("fcntl_lock %d %d %.0f %.0f %d\n",fd,op,(double)offset,(double)count,type));
+ DEBUG(8,("fcntl_lock fd=%d op=%d offset=%.0f count=%.0f type=%d\n",
+ fd,op,(double)offset,(double)count,type));
lock.l_type = type;
lock.l_whence = SEEK_SET;
ret = sys_fcntl_ptr(fd,op,&lock);
if (ret == -1) {
+ int sav = errno;
DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
(double)offset,(double)count,op,type,strerror(errno)));
+ errno = sav;
return False;
}
SMB_STRUCT_FLOCK lock;
int ret;
- DEBUG(8,("fcntl_getlock %d %.0f %.0f %d\n",fd,(double)*poffset,(double)*pcount,*ptype));
+ DEBUG(8,("fcntl_getlock fd=%d offset=%.0f count=%.0f type=%d\n",
+ fd,(double)*poffset,(double)*pcount,*ptype));
lock.l_type = *ptype;
lock.l_whence = SEEK_SET;
ret = sys_fcntl_ptr(fd,SMB_F_GETLK,&lock);
if (ret == -1) {
+ int sav = errno;
DEBUG(3,("fcntl_getlock: lock request failed at offset %.0f count %.0f type %d (%s)\n",
(double)*poffset,(double)*pcount,*ptype,strerror(errno)));
+ errno = sav;
return False;
}
/* check for loopback */
+ if (strequal(servername, "127.0.0.1"))
+ return True;
+
if (strequal(servername, "localhost"))
return True;
/*******************************************************************
we distinguish between 2K and XP by the "Native Lan Manager" string
WinXP => "Windows 2002 5.1"
+ WinXP 64bit => "Windows XP 5.2"
Win2k => "Windows 2000 5.0"
NT4 => "Windows NT 4.0"
Win9x => "Windows 4.0"
********************************************************************/
void ra_lanman_string( const char *native_lanman )
-{
+{
if ( strcmp( native_lanman, "Windows 2002 5.1" ) == 0 )
set_remote_arch( RA_WINXP );
+ else if ( strcmp( native_lanman, "Windows XP 5.2" ) == 0 )
+ set_remote_arch( RA_WINXP );
else if ( strcmp( native_lanman, "Windows Server 2003 5.2" ) == 0 )
set_remote_arch( RA_WIN2K3 );
}
case RA_WIN2K3:
fstrcpy(remote_arch, "Win2K3");
break;
+ case RA_VISTA:
+ fstrcpy(remote_arch, "Vista");
+ break;
case RA_SAMBA:
fstrcpy(remote_arch,"Samba");
break;
DEBUG(level,("%c", isprint(buf[i])?buf[i]:'.'));
}
-void dump_data(int level, const char *buf1,int len)
+void dump_data(int level, const unsigned char *buf,int len)
{
- const unsigned char *buf = (const unsigned char *)buf1;
int i=0;
if (len<=0) return;
DEBUG(11, ("%s", msg));
if (data != NULL && len > 0)
{
- dump_data(11, (const char *)data, len);
+ dump_data(11, data, len);
}
#endif
}
void *smb_xmalloc_array(size_t size, unsigned int count)
{
void *p;
- if (size == 0)
- smb_panic("smb_xmalloc_array: called with zero size.\n");
+ if (size == 0) {
+ smb_panic("smb_xmalloc_array: called with zero size");
+ }
if (count >= MAX_ALLOC_SIZE/size) {
- smb_panic("smb_xmalloc: alloc size too large.\n");
+ smb_panic("smb_xmalloc_array: alloc size too large");
}
if ((p = SMB_MALLOC(size*count)) == NULL) {
DEBUG(0, ("smb_xmalloc_array failed to allocate %lu * %lu bytes\n",
(unsigned long)size, (unsigned long)count));
- smb_panic("smb_xmalloc_array: malloc fail.\n");
+ smb_panic("smb_xmalloc_array: malloc failed");
}
return p;
}
#undef strdup
#endif
#endif
+
+#ifndef HAVE_STRDUP
+#define strdup rep_strdup
+#endif
+
char *s1 = strdup(s);
#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef strdup
+#undef strdup
+#endif
#define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
#endif
- if (!s1)
- smb_panic("smb_xstrdup: malloc fail\n");
+ if (!s1) {
+ smb_panic("smb_xstrdup: malloc failed");
+ }
return s1;
}
#undef strndup
#endif
#endif
+
+#if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
+#undef HAVE_STRNDUP
+#define strndup rep_strndup
+#endif
+
char *s1 = strndup(s, n);
#if defined(PARANOID_MALLOC_CHECKER)
+#ifdef strndup
+#undef strndup
+#endif
#define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
#endif
- if (!s1)
- smb_panic("smb_xstrndup: malloc fail\n");
+ if (!s1) {
+ smb_panic("smb_xstrndup: malloc failed");
+ }
return s1;
}
VA_COPY(ap2, ap);
n = vasprintf(ptr, format, ap2);
- if (n == -1 || ! *ptr)
+ if (n == -1 || ! *ptr) {
smb_panic("smb_xvasprintf: out of memory");
+ }
return n;
}
return dirpath;
}
+BOOL parent_dirname_talloc(TALLOC_CTX *mem_ctx, const char *dir,
+ char **parent, const char **name)
+{
+ char *p;
+ ptrdiff_t len;
+
+ p = strrchr_m(dir, '/'); /* Find final '/', if any */
+
+ if (p == NULL) {
+ if (!(*parent = talloc_strdup(mem_ctx, "."))) {
+ return False;
+ }
+ if (name) {
+ *name = "";
+ }
+ return True;
+ }
+
+ len = p-dir;
+
+ if (!(*parent = TALLOC_ARRAY(mem_ctx, char, len+1))) {
+ return False;
+ }
+ memcpy(*parent, dir, len);
+ (*parent)[len] = '\0';
+
+ if (name) {
+ *name = p+1;
+ }
+ return True;
+}
/*******************************************************************
Determine if a pattern contains any Microsoft wildcard characters.
}
/**********************************************************************
- Converts a name to a fully qalified domain name.
+ Converts a name to a fully qualified domain name.
+ Returns True if lookup succeeded, False if not (then fqdn is set to name)
***********************************************************************/
-void name_to_fqdn(fstring fqdn, const char *name)
+BOOL name_to_fqdn(fstring fqdn, const char *name)
{
struct hostent *hp = sys_gethostbyname(name);
+
if ( hp && hp->h_name && *hp->h_name ) {
- DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, hp->h_name));
- fstrcpy(fqdn,hp->h_name);
+ char *full = NULL;
+
+ /* find out if the fqdn is returned as an alias
+ * to cope with /etc/hosts files where the first
+ * name is not the fqdn but the short name */
+ if (hp->h_aliases && (! strchr_m(hp->h_name, '.'))) {
+ int i;
+ for (i = 0; hp->h_aliases[i]; i++) {
+ if (strchr_m(hp->h_aliases[i], '.')) {
+ full = hp->h_aliases[i];
+ break;
+ }
+ }
+ }
+ if (full && (StrCaseCmp(full, "localhost.localdomain") == 0)) {
+ DEBUG(1, ("WARNING: your /etc/hosts file may be broken!\n"));
+ DEBUGADD(1, (" Specifing the machine hostname for address 127.0.0.1 may lead\n"));
+ DEBUGADD(1, (" to Kerberos authentication problems as localhost.localdomain\n"));
+ DEBUGADD(1, (" may end up being used instead of the real machine FQDN.\n"));
+ full = hp->h_name;
+ }
+
+ if (!full) {
+ full = hp->h_name;
+ }
+
+ DEBUG(10,("name_to_fqdn: lookup for %s -> %s.\n", name, full));
+ fstrcpy(fqdn, full);
+ return True;
} else {
DEBUG(10,("name_to_fqdn: lookup for %s failed.\n", name));
fstrcpy(fqdn, name);
+ return False;
}
}
{
void *result;
- if (ptr == NULL)
- return NULL;
-
result = talloc_check_name(ptr, name);
if (result != NULL)
return result;
DEBUG(0, ("Talloc type mismatch, expected %s, got %s\n",
name, talloc_get_name(ptr)));
- smb_panic("aborting");
+ smb_panic("talloc type mismatch");
/* Keep the compiler happy */
return NULL;
}
return (uint32)-1;
}
-pid_t procid_to_pid(const struct process_id *proc)
+pid_t procid_to_pid(const struct server_id *proc)
{
return proc->pid;
}
-struct process_id pid_to_procid(pid_t pid)
+static uint32 my_vnn = NONCLUSTER_VNN;
+
+void set_my_vnn(uint32 vnn)
+{
+ DEBUG(10, ("vnn pid %d = %u\n", (int)sys_getpid(), (unsigned int)vnn));
+ my_vnn = vnn;
+}
+
+uint32 get_my_vnn(void)
+{
+ return my_vnn;
+}
+
+struct server_id pid_to_procid(pid_t pid)
{
- struct process_id result;
+ struct server_id result;
result.pid = pid;
+#ifdef CLUSTER_SUPPORT
+ result.vnn = my_vnn;
+#endif
return result;
}
-struct process_id procid_self(void)
+struct server_id procid_self(void)
{
return pid_to_procid(sys_getpid());
}
-BOOL procid_equal(const struct process_id *p1, const struct process_id *p2)
+struct server_id server_id_self(void)
{
- return (p1->pid == p2->pid);
+ return procid_self();
}
-BOOL procid_is_me(const struct process_id *pid)
+BOOL procid_equal(const struct server_id *p1, const struct server_id *p2)
{
- return (pid->pid == sys_getpid());
+ if (p1->pid != p2->pid)
+ return False;
+#ifdef CLUSTER_SUPPORT
+ if (p1->vnn != p2->vnn)
+ return False;
+#endif
+ return True;
+}
+
+BOOL cluster_id_equal(const struct server_id *id1,
+ const struct server_id *id2)
+{
+ return procid_equal(id1, id2);
}
-struct process_id interpret_pid(const char *pid_string)
+BOOL procid_is_me(const struct server_id *pid)
{
+ if (pid->pid != sys_getpid())
+ return False;
+#ifdef CLUSTER_SUPPORT
+ if (pid->vnn != my_vnn)
+ return False;
+#endif
+ return True;
+}
+
+struct server_id interpret_pid(const char *pid_string)
+{
+#ifdef CLUSTER_SUPPORT
+ unsigned int vnn, pid;
+ struct server_id result;
+ if (sscanf(pid_string, "%u:%u", &vnn, &pid) == 2) {
+ result.vnn = vnn;
+ result.pid = pid;
+ }
+ else if (sscanf(pid_string, "%u", &pid) == 1) {
+ result.vnn = NONCLUSTER_VNN;
+ result.pid = pid;
+ }
+ else {
+ result.vnn = NONCLUSTER_VNN;
+ result.pid = -1;
+ }
+ return result;
+#else
return pid_to_procid(atoi(pid_string));
+#endif
}
-char *procid_str_static(const struct process_id *pid)
+char *procid_str_static(const struct server_id *pid)
{
static fstring str;
- fstr_sprintf(str, "%d", pid->pid);
+#ifdef CLUSTER_SUPPORT
+ if (pid->vnn == NONCLUSTER_VNN) {
+ fstr_sprintf(str, "%d", (int)pid->pid);
+ }
+ else {
+ fstr_sprintf(str, "%u:%d", (unsigned)pid->vnn, (int)pid->pid);
+ }
+#else
+ fstr_sprintf(str, "%d", (int)pid->pid);
+#endif
return str;
}
-char *procid_str(TALLOC_CTX *mem_ctx, const struct process_id *pid)
+char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid)
{
return talloc_strdup(mem_ctx, procid_str_static(pid));
}
-BOOL procid_valid(const struct process_id *pid)
+BOOL procid_valid(const struct server_id *pid)
{
return (pid->pid != -1);
}
-BOOL procid_is_local(const struct process_id *pid)
+BOOL procid_is_local(const struct server_id *pid)
{
+#ifdef CLUSTER_SUPPORT
+ return pid->vnn == my_vnn;
+#else
return True;
+#endif
+}
+
+int this_is_smp(void)
+{
+#if defined(HAVE_SYSCONF)
+
+#if defined(SYSCONF_SC_NPROC_ONLN)
+ return (sysconf(_SC_NPROC_ONLN) > 1) ? 1 : 0;
+#elif defined(SYSCONF_SC_NPROCESSORS_ONLN)
+ return (sysconf(_SC_NPROCESSORS_ONLN) > 1) ? 1 : 0;
+#else
+ return 0;
+#endif
+
+#else
+ return 0;
+#endif
+}
+
+/****************************************************************
+ Check if an offset into a buffer is safe.
+ If this returns True it's safe to indirect into the byte at
+ pointer ptr+off.
+****************************************************************/
+
+BOOL is_offset_safe(const char *buf_base, size_t buf_len, char *ptr, size_t off)
+{
+ const char *end_base = buf_base + buf_len;
+ char *end_ptr = ptr + off;
+
+ if (!buf_base || !ptr) {
+ return False;
+ }
+
+ if (end_base < buf_base || end_ptr < ptr) {
+ return False; /* wrap. */
+ }
+
+ if (end_ptr < end_base) {
+ return True;
+ }
+ return False;
+}
+
+/****************************************************************
+ Return a safe pointer into a buffer, or NULL.
+****************************************************************/
+
+char *get_safe_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
+{
+ return is_offset_safe(buf_base, buf_len, ptr, off) ?
+ ptr + off : NULL;
+}
+
+/****************************************************************
+ Return a safe pointer into a string within a buffer, or NULL.
+****************************************************************/
+
+char *get_safe_str_ptr(const char *buf_base, size_t buf_len, char *ptr, size_t off)
+{
+ if (!is_offset_safe(buf_base, buf_len, ptr, off)) {
+ return NULL;
+ }
+ /* Check if a valid string exists at this offset. */
+ if (skip_string(buf_base,buf_len, ptr + off) == NULL) {
+ return NULL;
+ }
+ return ptr + off;
+}
+
+/****************************************************************
+ Return an SVAL at a pointer, or failval if beyond the end.
+****************************************************************/
+
+int get_safe_SVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
+{
+ /*
+ * Note we use off+1 here, not off+2 as SVAL accesses ptr[0] and ptr[1],
+ * NOT ptr[2].
+ */
+ if (!is_offset_safe(buf_base, buf_len, ptr, off+1)) {
+ return failval;
+ }
+ return SVAL(ptr,off);
+}
+
+/****************************************************************
+ Return an IVAL at a pointer, or failval if beyond the end.
+****************************************************************/
+
+int get_safe_IVAL(const char *buf_base, size_t buf_len, char *ptr, size_t off, int failval)
+{
+ /*
+ * Note we use off+3 here, not off+4 as IVAL accesses
+ * ptr[0] ptr[1] ptr[2] ptr[3] NOT ptr[4].
+ */
+ if (!is_offset_safe(buf_base, buf_len, ptr, off+3)) {
+ return failval;
+ }
+ return IVAL(ptr,off);
+}
+
+/****************************************************************
+ talloc wrapper functions that guarentee a null pointer return
+ if size == 0.
+****************************************************************/
+
+#ifndef MAX_TALLOC_SIZE
+#define MAX_TALLOC_SIZE 0x10000000
+#endif
+
+/*
+ * talloc and zero memory.
+ * - returns NULL if size is zero.
+ */
+
+void *_talloc_zero_zeronull(const void *ctx, size_t size, const char *name)
+{
+ void *p;
+
+ if (size == 0) {
+ return NULL;
+ }
+
+ p = talloc_named_const(ctx, size, name);
+
+ if (p) {
+ memset(p, '\0', size);
+ }
+
+ return p;
+}
+
+/*
+ * memdup with a talloc.
+ * - returns NULL if size is zero.
+ */
+
+void *_talloc_memdup_zeronull(const void *t, const void *p, size_t size, const char *name)
+{
+ void *newp;
+
+ if (size == 0) {
+ return NULL;
+ }
+
+ newp = talloc_named_const(t, size, name);
+ if (newp) {
+ memcpy(newp, p, size);
+ }
+
+ return newp;
+}
+
+/*
+ * alloc an array, checking for integer overflow in the array size.
+ * - returns NULL if count or el_size are zero.
+ */
+
+void *_talloc_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name)
+{
+ if (count >= MAX_TALLOC_SIZE/el_size) {
+ return NULL;
+ }
+
+ if (el_size == 0 || count == 0) {
+ return NULL;
+ }
+
+ return talloc_named_const(ctx, el_size * count, name);
+}
+
+/*
+ * alloc an zero array, checking for integer overflow in the array size
+ * - returns NULL if count or el_size are zero.
+ */
+
+void *_talloc_zero_array_zeronull(const void *ctx, size_t el_size, unsigned count, const char *name)
+{
+ if (count >= MAX_TALLOC_SIZE/el_size) {
+ return NULL;
+ }
+
+ if (el_size == 0 || count == 0) {
+ return NULL;
+ }
+
+ return _talloc_zero(ctx, el_size * count, name);
+}
+
+/*
+ * Talloc wrapper that returns NULL if size == 0.
+ */
+void *talloc_zeronull(const void *context, size_t size, const char *name)
+{
+ if (size == 0) {
+ return NULL;
+ }
+ return talloc_named_const(context, size, name);
}