Add NT quota support. Patch from Stefan (metze) Metzemacher
[nivanova/samba-autobuild/.git] / source3 / lib / util.c
index 1f300a28157a660e4a648239a64981cc064b88c1..4452de8b4bc8114cd4f4ffa0c6bdaed0dc0d93e4 100644 (file)
@@ -937,6 +937,19 @@ void *Realloc(void *p,size_t size)
        return(ret);
 }
 
+void *Realloc_zero(void *ptr, size_t size)
+{
+       void *tptr = NULL;
+               
+       tptr = Realloc(ptr, size);
+       if(tptr == NULL)
+               return NULL;
+
+       memset((char *)tptr,'\0',size);
+
+       return tptr;
+}
+
 /****************************************************************************
  Free memory, checks for NULL.
  Use directly SAFE_FREE()
@@ -1012,6 +1025,7 @@ BOOL get_mydomname(fstring my_domname)
 {
        pstring hostname;
        char *p;
+       struct hostent *hp;
 
        *hostname = 0;
        /* get my host name */
@@ -1023,24 +1037,38 @@ BOOL get_mydomname(fstring my_domname)
        /* Ensure null termination. */
        hostname[sizeof(hostname)-1] = '\0';
 
+               
        p = strchr_m(hostname, '.');
 
-       if (!p)
-               return False;
+       if (p) {
+               p++;
+               
+               if (my_domname)
+                       fstrcpy(my_domname, p);
+       }
 
-       p++;
+       if (!(hp = sys_gethostbyname(hostname))) {
+               return False;
+       }
        
-       if (my_domname)
-               fstrcpy(my_domname, p);
+       p = strchr_m(hp->h_name, '.');
 
-       return True;
+       if (p) {
+               p++;
+               
+               if (my_domname)
+                       fstrcpy(my_domname, p);
+               return True;
+       }
+
+       return False;
 }
 
 /****************************************************************************
  Interpret a protocol description string, with a default.
 ****************************************************************************/
 
-int interpret_protocol(char *str,int def)
+int interpret_protocol(const char *str,int def)
 {
        if (strequal(str,"NT1"))
                return(PROTOCOL_NT1);
@@ -1400,20 +1428,26 @@ void smb_panic(const char *why)
 {
        char *cmd;
        int result;
+       size_t i;
+#ifdef HAVE_BACKTRACE_SYMBOLS
+       void *backtrace_stack[BACKTRACE_STACK_SIZE];
+       size_t backtrace_size;
+       char **backtrace_strings;
+#endif
 
 #ifdef DEVELOPER
        {
                extern char *global_clobber_region_function;
                extern unsigned int global_clobber_region_line;
-               
+
                if (global_clobber_region_function) {
                        DEBUG(0,("smb_panic: clobber_region() last called from [%s(%u)]\n",
-                                global_clobber_region_function,
-                                global_clobber_region_line));
+                                        global_clobber_region_function,
+                                        global_clobber_region_line));
                } 
        }
 #endif
-       
+
        cmd = lp_panic_action();
        if (cmd && *cmd) {
                DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
@@ -1421,21 +1455,38 @@ void smb_panic(const char *why)
 
                if (result == -1)
                        DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
-                                 strerror(errno)));
+                                         strerror(errno)));
                else
                        DEBUG(0, ("smb_panic(): action returned status %d\n",
-                                 WEXITSTATUS(result)));
+                                         WEXITSTATUS(result)));
        }
        DEBUG(0,("PANIC: %s\n", why));
+
+#ifdef HAVE_BACKTRACE_SYMBOLS
+       /* get the backtrace (stack frames) */
+       backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
+       backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
+
+       DEBUG(0, ("BACKTRACE: %d stack frames:\n", backtrace_size));
+       
+       if (backtrace_strings) {
+               for (i = 0; i < backtrace_size; i++)
+                       DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
+
+               SAFE_FREE(backtrace_strings);
+       }
+
+#endif
+
        dbgflush();
        abort();
 }
 
 /*******************************************************************
- A readdir wrapper which just returns the file name.
-********************************************************************/
 A readdir wrapper which just returns the file name.
+ ********************************************************************/
 
-char *readdirname(DIR *p)
+const char *readdirname(DIR *p)
 {
        SMB_STRUCT_DIRENT *ptr;
        char *dname;
@@ -1819,50 +1870,6 @@ enum remote_arch_types get_remote_arch(void)
        return ra_type;
 }
 
-
-void out_ascii(FILE *f, unsigned char *buf,int len)
-{
-       int i;
-       for (i=0;i<len;i++)
-               fprintf(f, "%c", isprint(buf[i])?buf[i]:'.');
-}
-
-void out_data(FILE *f,char *buf1,int len, int per_line)
-{
-       unsigned char *buf = (unsigned char *)buf1;
-       int i=0;
-       if (len<=0) {
-               return;
-       }
-
-       fprintf(f, "[%03X] ",i);
-       for (i=0;i<len;) {
-               fprintf(f, "%02X ",(int)buf[i]);
-               i++;
-               if (i%(per_line/2) == 0) fprintf(f, " ");
-               if (i%per_line == 0) {      
-                       out_ascii(f,&buf[i-per_line  ],per_line/2); fprintf(f, " ");
-                       out_ascii(f,&buf[i-per_line/2],per_line/2); fprintf(f, "\n");
-                       if (i<len) fprintf(f, "[%03X] ",i);
-               }
-       }
-       if ((i%per_line) != 0) {
-               int n;
-
-               n = per_line - (i%per_line);
-               fprintf(f, " ");
-               if (n>(per_line/2)) fprintf(f, " ");
-               while (n--) {
-                       fprintf(f, "   ");
-               }
-               n = MIN(per_line/2,i%per_line);
-               out_ascii(f,&buf[i-(i%per_line)],n); fprintf(f, " ");
-               n = (i%per_line) - n;
-               if (n>0) out_ascii(f,&buf[i-n],n); 
-               fprintf(f, "\n");    
-       }
-}
-
 void print_asc(int level, const unsigned char *buf,int len)
 {
        int i;
@@ -2089,8 +2096,10 @@ void *smb_xmalloc(size_t size)
        void *p;
        if (size == 0)
                smb_panic("smb_xmalloc: called with zero size.\n");
-       if ((p = malloc(size)) == NULL)
+       if ((p = malloc(size)) == NULL) {
+               DEBUG(0, ("smb_xmalloc() failed to allocate %lu bytes\n", (unsigned long)size));
                smb_panic("smb_xmalloc: malloc fail.\n");
+       }
        return p;
 }