2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 2001-2002
6 Copyright (C) Simo Sorce 2001-2011
7 Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
8 Copyright (C) James J Myers 2003
9 Copyright (C) Volker Lendecke 2010
10 Copyright (C) Swen Schillig 2019
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "system/network.h"
29 #include "system/filesys.h"
30 #include "system/locale.h"
31 #include "system/shmem.h"
32 #include "system/passwd.h"
33 #include "system/time.h"
34 #include "system/wait.h"
36 #include "samba_util.h"
37 #include "lib/util/select.h"
48 * @brief Misc utility functions
52 * Convert a string to an unsigned long integer
54 * @param nptr pointer to string which is to be converted
55 * @param endptr [optional] reference to remainder of the string
56 * @param base base of the numbering scheme
57 * @param err error occured during conversion
58 * @flags controlling conversion feature
59 * @result result of the conversion as provided by strtoul
61 * The following flags are supported
62 * SMB_STR_STANDARD # raise error if negative or non-numeric
63 * SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
64 * SMB_STR_FULL_STR_CONV # entire string must be converted
65 * SMB_STR_ALLOW_NO_CONVERSION # allow empty strings or non-numeric
66 * SMB_STR_GLIBC_STANDARD # act exactly as the standard glibc strtoul
68 * The following errors are detected
71 * - string with a leading "-" indicating a negative number
72 * - no conversion due to empty string or not representing a number
75 smb_strtoul(const char *nptr
, char **endptr
, int base
, int *err
, int flags
)
77 unsigned long int val
;
78 int saved_errno
= errno
;
80 char *tmp_endptr
= NULL
;
85 val
= strtoul(nptr
, &tmp_endptr
, base
);
97 if ((flags
& SMB_STR_ALLOW_NO_CONVERSION
) == 0) {
98 /* got an invalid number-string resulting in no conversion */
99 if (nptr
== tmp_endptr
) {
105 if ((flags
& SMB_STR_ALLOW_NEGATIVE
) == 0) {
106 /* did we convert a negative "number" ? */
107 needle
= strchr(nptr
, '-');
108 if (needle
!= NULL
&& needle
< tmp_endptr
) {
114 if ((flags
& SMB_STR_FULL_STR_CONV
) != 0) {
115 /* did we convert the entire string ? */
116 if (tmp_endptr
[0] != '\0') {
128 * Convert a string to an unsigned long long integer
130 * @param nptr pointer to string which is to be converted
131 * @param endptr [optional] reference to remainder of the string
132 * @param base base of the numbering scheme
133 * @param err error occured during conversion
134 * @flags controlling conversion feature
135 * @result result of the conversion as provided by strtoull
137 * The following flags are supported
138 * SMB_STR_STANDARD # raise error if negative or non-numeric
139 * SMB_STR_ALLOW_NEGATIVE # allow strings with a leading "-"
140 * SMB_STR_FULL_STR_CONV # entire string must be converted
141 * SMB_STR_ALLOW_NO_CONVERSION # allow empty strings or non-numeric
142 * SMB_STR_GLIBC_STANDARD # act exactly as the standard glibc strtoul
144 * The following errors are detected
147 * - string with a leading "-" indicating a negative number
148 * - no conversion due to empty string or not representing a number
150 unsigned long long int
151 smb_strtoull(const char *nptr
, char **endptr
, int base
, int *err
, int flags
)
153 unsigned long long int val
;
154 int saved_errno
= errno
;
156 char *tmp_endptr
= NULL
;
161 val
= strtoull(nptr
, &tmp_endptr
, base
);
163 if (endptr
!= NULL
) {
164 *endptr
= tmp_endptr
;
173 if ((flags
& SMB_STR_ALLOW_NO_CONVERSION
) == 0) {
174 /* got an invalid number-string resulting in no conversion */
175 if (nptr
== tmp_endptr
) {
181 if ((flags
& SMB_STR_ALLOW_NEGATIVE
) == 0) {
182 /* did we convert a negative "number" ? */
183 needle
= strchr(nptr
, '-');
184 if (needle
!= NULL
&& needle
< tmp_endptr
) {
190 if ((flags
& SMB_STR_FULL_STR_CONV
) != 0) {
191 /* did we convert the entire string ? */
192 if (tmp_endptr
[0] != '\0') {
204 Find a suitable temporary directory. The result should be copied immediately
205 as it may be overwritten by a subsequent call.
207 _PUBLIC_
const char *tmpdir(void)
210 if ((p
= getenv("TMPDIR")))
217 Create a tmp file, open it and immediately unlink it.
218 If dir is NULL uses tmpdir()
219 Returns the file descriptor or -1 on error.
221 int create_unlink_tmp(const char *dir
)
223 size_t len
= strlen(dir
? dir
: (dir
= tmpdir()));
228 len
= snprintf(fname
, sizeof(fname
), "%s/listenerlock_XXXXXX", dir
);
229 if (len
>= sizeof(fname
)) {
233 mask
= umask(S_IRWXO
| S_IRWXG
);
239 if (unlink(fname
) == -1) {
240 int sys_errno
= errno
;
250 Check if a file exists - call vfs_file_exist for samba files.
252 _PUBLIC_
bool file_exist(const char *fname
)
256 if (stat(fname
, &st
) != 0) {
260 return ((S_ISREG(st
.st_mode
)) || (S_ISFIFO(st
.st_mode
)));
264 Check a files mod time.
267 _PUBLIC_
time_t file_modtime(const char *fname
)
271 if (stat(fname
,&st
) != 0)
278 Check file permissions.
281 _PUBLIC_
bool file_check_permissions(const char *fname
,
295 ret
= stat(fname
, pst
);
297 DEBUG(0, ("stat failed on file '%s': %s\n",
298 fname
, strerror(errno
)));
302 if (pst
->st_uid
!= uid
&& !uid_wrapper_enabled()) {
303 DEBUG(0, ("invalid ownership of file '%s': "
304 "owned by uid %u, should be %u\n",
305 fname
, (unsigned int)pst
->st_uid
,
310 if ((pst
->st_mode
& 0777) != file_perms
) {
311 DEBUG(0, ("invalid permissions on file "
312 "'%s': has 0%o should be 0%o\n", fname
,
313 (unsigned int)(pst
->st_mode
& 0777),
314 (unsigned int)file_perms
));
322 Check if a directory exists.
325 _PUBLIC_
bool directory_exist(const char *dname
)
330 if (stat(dname
,&st
) != 0) {
334 ret
= S_ISDIR(st
.st_mode
);
341 * Try to create the specified directory if it didn't exist.
343 * @retval true if the directory already existed
344 * or was successfully created.
346 _PUBLIC_
bool directory_create_or_exist(const char *dname
,
352 /* Create directory */
353 old_umask
= umask(0);
354 ret
= mkdir(dname
, dir_perms
);
355 if (ret
== -1 && errno
!= EEXIST
) {
356 DBG_WARNING("mkdir failed on directory %s: %s\n",
364 if (ret
!= 0 && errno
== EEXIST
) {
367 ret
= lstat(dname
, &sbuf
);
372 if (!S_ISDIR(sbuf
.st_mode
)) {
381 * @brief Try to create a specified directory if it doesn't exist.
383 * The function creates a directory with the given uid and permissions if it
384 * doesn't exist. If it exists it makes sure the uid and permissions are
385 * correct and it will fail if they are different.
387 * @param[in] dname The directory to create.
389 * @param[in] uid The uid the directory needs to belong too.
391 * @param[in] dir_perms The expected permissions of the directory.
393 * @return True on success, false on error.
395 _PUBLIC_
bool directory_create_or_exist_strict(const char *dname
,
403 ok
= directory_create_or_exist(dname
, dir_perms
);
408 rc
= lstat(dname
, &st
);
410 DEBUG(0, ("lstat failed on created directory %s: %s\n",
411 dname
, strerror(errno
)));
415 /* Check ownership and permission on existing directory */
416 if (!S_ISDIR(st
.st_mode
)) {
417 DEBUG(0, ("directory %s isn't a directory\n",
421 if (st
.st_uid
!= uid
&& !uid_wrapper_enabled()) {
422 DBG_NOTICE("invalid ownership on directory "
426 if ((st
.st_mode
& 0777) != dir_perms
) {
427 DEBUG(0, ("invalid permissions on directory "
428 "'%s': has 0%o should be 0%o\n", dname
,
429 (unsigned int)(st
.st_mode
& 0777), (unsigned int)dir_perms
));
438 Sleep for a specified number of milliseconds.
441 _PUBLIC_
void smb_msleep(unsigned int t
)
443 sys_poll_intr(NULL
, 0, t
);
447 Get my own name, return in talloc'ed storage.
450 _PUBLIC_
char *get_myname(TALLOC_CTX
*ctx
)
453 char hostname
[HOST_NAME_MAX
];
455 /* get my host name */
456 if (gethostname(hostname
, sizeof(hostname
)) == -1) {
457 DEBUG(0,("gethostname failed\n"));
461 /* Ensure null termination. */
462 hostname
[sizeof(hostname
)-1] = '\0';
464 /* split off any parts after an initial . */
465 p
= strchr_m(hostname
, '.');
470 return talloc_strdup(ctx
, hostname
);
474 Check if a process exists. Does this work on all unixes?
477 _PUBLIC_
bool process_exists_by_pid(pid_t pid
)
479 /* Doing kill with a non-positive pid causes messages to be
480 * sent to places we don't want. */
484 return(kill(pid
,0) == 0 || errno
!= ESRCH
);
488 Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
489 is dealt with in posix.c
492 _PUBLIC_
bool fcntl_lock(int fd
, int op
, off_t offset
, off_t count
, int type
)
497 DEBUG(8,("fcntl_lock %d %d %.0f %.0f %d\n",fd
,op
,(double)offset
,(double)count
,type
));
500 lock
.l_whence
= SEEK_SET
;
501 lock
.l_start
= offset
;
505 ret
= fcntl(fd
,op
,&lock
);
507 if (ret
== -1 && errno
!= 0)
508 DEBUG(3,("fcntl_lock: fcntl lock gave errno %d (%s)\n",errno
,strerror(errno
)));
513 (lock
.l_type
!= F_UNLCK
) &&
515 (lock
.l_pid
!= getpid())) {
516 DEBUG(3,("fcntl_lock: fd %d is locked by pid %d\n",fd
,(int)lock
.l_pid
));
520 /* it must be not locked or locked by me */
524 /* a lock set or unset */
526 DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
527 (double)offset
,(double)count
,op
,type
,strerror(errno
)));
531 /* everything went OK */
532 DEBUG(8,("fcntl_lock: Lock call successful\n"));
537 struct debug_channel_level
{
542 static void debugadd_channel_cb(const char *buf
, void *private_data
)
544 struct debug_channel_level
*dcl
=
545 (struct debug_channel_level
*)private_data
;
547 DEBUGADDC(dcl
->channel
, dcl
->level
,("%s", buf
));
550 static void debugadd_cb(const char *buf
, void *private_data
)
552 int *plevel
= (int *)private_data
;
553 DEBUGADD(*plevel
, ("%s", buf
));
556 void print_asc_cb(const uint8_t *buf
, int len
,
557 void (*cb
)(const char *buf
, void *private_data
),
564 for (i
=0; i
<len
; i
++) {
565 s
[0] = isprint(buf
[i
]) ? buf
[i
] : '.';
570 void print_asc(int level
, const uint8_t *buf
,int len
)
572 print_asc_cb(buf
, len
, debugadd_cb
, &level
);
576 * Write dump of binary data to a callback
578 void dump_data_cb(const uint8_t *buf
, int len
,
579 bool omit_zero_bytes
,
580 void (*cb
)(const char *buf
, void *private_data
),
584 bool skipped
= false;
592 if ((omit_zero_bytes
== true) &&
595 all_zero(&buf
[i
], 16))
602 snprintf(tmp
, sizeof(tmp
), "[%04X] ", i
);
603 cb(tmp
, private_data
);
607 snprintf(tmp
, sizeof(tmp
), "%02X ", (int)buf
[i
]);
608 cb(tmp
, private_data
);
611 cb(" ", private_data
);
615 print_asc_cb(&buf
[i
-16], 8, cb
, private_data
);
616 cb(" ", private_data
);
617 print_asc_cb(&buf
[i
-8], 8, cb
, private_data
);
618 cb("\n", private_data
);
620 if ((omit_zero_bytes
== true) &&
622 all_zero(&buf
[i
], 16)) {
624 cb("skipping zero buffer bytes\n",
635 cb(" ", private_data
);
637 cb(" ", private_data
);
640 cb(" ", private_data
);
643 print_asc_cb(&buf
[i
-(i
%16)], n
, cb
, private_data
);
644 cb(" ", private_data
);
647 print_asc_cb(&buf
[i
-n
], n
, cb
, private_data
);
649 cb("\n", private_data
);
655 * Write dump of binary data to the log file.
657 * The data is only written if the log level is at least level.
659 _PUBLIC_
void dump_data(int level
, const uint8_t *buf
, int len
)
661 if (!DEBUGLVL(level
)) {
664 dump_data_cb(buf
, len
, false, debugadd_cb
, &level
);
668 * Write dump of binary data to the log file.
670 * The data is only written if the log level is at least level for
671 * debug class dbgc_class.
673 _PUBLIC_
void dump_data_dbgc(int dbgc_class
, int level
, const uint8_t *buf
, int len
)
675 struct debug_channel_level dcl
= { dbgc_class
, level
};
677 if (!DEBUGLVLC(dbgc_class
, level
)) {
680 dump_data_cb(buf
, len
, false, debugadd_channel_cb
, &dcl
);
684 * Write dump of binary data to the log file.
686 * The data is only written if the log level is at least level.
687 * 16 zero bytes in a row are omitted
689 _PUBLIC_
void dump_data_skip_zeros(int level
, const uint8_t *buf
, int len
)
691 if (!DEBUGLVL(level
)) {
694 dump_data_cb(buf
, len
, true, debugadd_cb
, &level
);
697 static void fprintf_cb(const char *buf
, void *private_data
)
699 FILE *f
= (FILE *)private_data
;
700 fprintf(f
, "%s", buf
);
703 void dump_data_file(const uint8_t *buf
, int len
, bool omit_zero_bytes
,
706 dump_data_cb(buf
, len
, omit_zero_bytes
, fprintf_cb
, f
);
710 malloc that aborts with smb_panic on fail or zero size.
713 _PUBLIC_
void *smb_xmalloc(size_t size
)
717 smb_panic("smb_xmalloc: called with zero size.\n");
718 if ((p
= malloc(size
)) == NULL
)
719 smb_panic("smb_xmalloc: malloc fail.\n");
724 Memdup with smb_panic on fail.
727 _PUBLIC_
void *smb_xmemdup(const void *p
, size_t size
)
730 p2
= smb_xmalloc(size
);
736 strdup that aborts on malloc fail.
739 char *smb_xstrdup(const char *s
)
741 #if defined(PARANOID_MALLOC_CHECKER)
748 #define strdup rep_strdup
751 char *s1
= strdup(s
);
752 #if defined(PARANOID_MALLOC_CHECKER)
756 #define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
759 smb_panic("smb_xstrdup: malloc failed");
766 strndup that aborts on malloc fail.
769 char *smb_xstrndup(const char *s
, size_t n
)
771 #if defined(PARANOID_MALLOC_CHECKER)
777 #if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
779 #define strndup rep_strndup
782 char *s1
= strndup(s
, n
);
783 #if defined(PARANOID_MALLOC_CHECKER)
787 #define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
790 smb_panic("smb_xstrndup: malloc failed");
798 Like strdup but for memory.
801 _PUBLIC_
void *smb_memdup(const void *p
, size_t size
)
814 * Write a password to the log file.
816 * @note Only actually does something if DEBUG_PASSWORD was defined during
819 _PUBLIC_
void dump_data_pw(const char *msg
, const uint8_t * data
, size_t len
)
821 #ifdef DEBUG_PASSWORD
822 DEBUG(11, ("%s", msg
));
823 if (data
!= NULL
&& len
> 0)
825 dump_data(11, data
, len
);
832 * see if a range of memory is all zero. A NULL pointer is considered
835 _PUBLIC_
bool all_zero(const uint8_t *ptr
, size_t size
)
838 if (!ptr
) return true;
839 for (i
=0;i
<size
;i
++) {
840 if (ptr
[i
]) return false;
846 realloc an array, checking for integer overflow in the array size
848 _PUBLIC_
void *realloc_array(void *ptr
, size_t el_size
, unsigned count
, bool free_on_fail
)
850 #define MAX_MALLOC_SIZE 0x7fffffff
852 count
>= MAX_MALLOC_SIZE
/el_size
) {
858 return malloc(el_size
* count
);
860 return realloc(ptr
, el_size
* count
);
863 /****************************************************************************
865 ****************************************************************************/
867 void *malloc_array(size_t el_size
, unsigned int count
)
869 return realloc_array(NULL
, el_size
, count
, false);
872 /****************************************************************************
874 ****************************************************************************/
876 void *memalign_array(size_t el_size
, size_t align
, unsigned int count
)
878 if (el_size
== 0 || count
>= MAX_MALLOC_SIZE
/el_size
) {
882 return memalign(align
, el_size
*count
);
885 /****************************************************************************
887 ****************************************************************************/
889 void *calloc_array(size_t size
, size_t nmemb
)
891 if (nmemb
>= MAX_MALLOC_SIZE
/size
) {
894 if (size
== 0 || nmemb
== 0) {
897 return calloc(nmemb
, size
);
901 Trim the specified elements off the front and back of a string.
903 _PUBLIC_
bool trim_string(char *s
, const char *front
, const char *back
)
910 /* Ignore null or empty strings. */
911 if (!s
|| (s
[0] == '\0')) {
916 front_len
= front
? strlen(front
) : 0;
917 back_len
= back
? strlen(back
) : 0;
920 size_t front_trim
= 0;
922 while (strncmp(s
+front_trim
, front
, front_len
)==0) {
923 front_trim
+= front_len
;
925 if (front_trim
> 0) {
926 /* Must use memmove here as src & dest can
927 * easily overlap. Found by valgrind. JRA. */
928 memmove(s
, s
+front_trim
, (len
-front_trim
)+1);
935 while ((len
>= back_len
) && strncmp(s
+len
-back_len
,back
,back_len
)==0) {
936 s
[len
-back_len
]='\0';
945 Find the number of 'c' chars in a string
947 _PUBLIC_ _PURE_
size_t count_chars(const char *s
, char c
)
952 if (*s
== c
) count
++;
960 * Routine to get hex characters and turn them into a byte array.
961 * the array can be variable length.
962 * - "0xnn" or "0Xnn" is specially catered for.
963 * - The first non-hex-digit character (apart from possibly leading "0x"
964 * finishes the conversion and skips the rest of the input.
965 * - A single hex-digit character at the end of the string is skipped.
967 * valid examples: "0A5D15"; "0x123456"
969 _PUBLIC_
size_t strhex_to_str(char *p
, size_t p_len
, const char *strhex
, size_t strhex_len
)
972 size_t num_chars
= 0;
973 uint8_t lonybble
, hinybble
;
974 const char *hexchars
= "0123456789ABCDEF";
975 char *p1
= NULL
, *p2
= NULL
;
977 /* skip leading 0x prefix */
978 if (strncasecmp(strhex
, "0x", 2) == 0) {
979 i
+= 2; /* skip two chars */
982 for (; i
+1 < strhex_len
&& strhex
[i
] != 0 && strhex
[i
+1] != 0; i
++) {
983 p1
= strchr(hexchars
, toupper((unsigned char)strhex
[i
]));
988 i
++; /* next hex digit */
990 p2
= strchr(hexchars
, toupper((unsigned char)strhex
[i
]));
995 /* get the two nybbles */
996 hinybble
= PTR_DIFF(p1
, hexchars
);
997 lonybble
= PTR_DIFF(p2
, hexchars
);
999 if (num_chars
>= p_len
) {
1003 p
[num_chars
] = (hinybble
<< 4) | lonybble
;
1013 * Parse a hex string and return a data blob.
1015 _PUBLIC_ _PURE_ DATA_BLOB
strhex_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *strhex
)
1017 DATA_BLOB ret_blob
= data_blob_talloc(mem_ctx
, NULL
, strlen(strhex
)/2+1);
1019 ret_blob
.length
= strhex_to_str((char *)ret_blob
.data
, ret_blob
.length
,
1027 * Parse a hex dump and return a data blob. Hex dump is structured as
1028 * is generated from dump_data_cb() elsewhere in this file
1031 _PUBLIC_ _PURE_ DATA_BLOB
hexdump_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *hexdump
, size_t hexdump_len
)
1033 DATA_BLOB ret_blob
= { 0 };
1035 size_t char_count
= 0;
1036 /* hexdump line length is 77 chars long. We then use the ASCII representation of the bytes
1037 * at the end of the final line to calculate how many are in that line, minus the extra space
1039 size_t hexdump_byte_count
= (16 * (hexdump_len
/ 77));
1040 if (hexdump_len
% 77) {
1041 hexdump_byte_count
+= ((hexdump_len
% 77) - 59 - 2);
1044 ret_blob
= data_blob_talloc(mem_ctx
, NULL
, hexdump_byte_count
+1);
1045 for (; i
+1 < hexdump_len
&& hexdump
[i
] != 0 && hexdump
[i
+1] != 0; i
++) {
1047 i
+= 7; /* Skip the offset at the start of the line */
1048 if ((i
%77) < 56) { /* position 56 is after both hex chunks */
1049 if (hexdump
[i
] != ' ') {
1050 char_count
+= strhex_to_str((char *)&ret_blob
.data
[char_count
],
1051 hexdump_byte_count
- char_count
,
1061 ret_blob
.length
= char_count
;
1067 * Print a buf in hex. Assumes dst is at least (srclen*2)+1 large.
1069 _PUBLIC_
void hex_encode_buf(char *dst
, const uint8_t *src
, size_t srclen
)
1072 for (i
=0; i
<srclen
; i
++) {
1073 snprintf(dst
+ i
*2, 3, "%02X", src
[i
]);
1076 * Ensure 0-termination for 0-length buffers
1078 dst
[srclen
*2] = '\0';
1082 * talloc version of hex_encode_buf()
1084 _PUBLIC_
char *hex_encode_talloc(TALLOC_CTX
*mem_ctx
, const unsigned char *buff_in
, size_t len
)
1088 hex_buffer
= talloc_array(mem_ctx
, char, (len
*2)+1);
1092 hex_encode_buf(hex_buffer
, buff_in
, len
);
1093 talloc_set_name_const(hex_buffer
, hex_buffer
);
1098 varient of strcmp() that handles NULL ptrs
1100 _PUBLIC_
int strcmp_safe(const char *s1
, const char *s2
)
1105 if (s1
== NULL
|| s2
== NULL
) {
1108 return strcmp(s1
, s2
);
1113 return the number of bytes occupied by a buffer in ASCII format
1114 the result includes the null termination
1115 limited by 'n' bytes
1117 _PUBLIC_
size_t ascii_len_n(const char *src
, size_t n
)
1121 len
= strnlen(src
, n
);
1129 struct anonymous_shared_header
{
1136 /* Map a shared memory buffer of at least nelem counters. */
1137 void *anonymous_shared_allocate(size_t orig_bufsz
)
1141 size_t pagesz
= getpagesize();
1143 size_t bufsz
= orig_bufsz
;
1144 struct anonymous_shared_header
*hdr
;
1146 bufsz
+= sizeof(*hdr
);
1148 /* round up to full pages */
1149 pagecnt
= bufsz
/ pagesz
;
1150 if (bufsz
% pagesz
) {
1153 bufsz
= pagesz
* pagecnt
;
1155 if (orig_bufsz
>= bufsz
) {
1163 buf
= mmap(NULL
, bufsz
, PROT_READ
|PROT_WRITE
, MAP_ANON
|MAP_SHARED
,
1164 -1 /* fd */, 0 /* offset */);
1170 fd
= open("/dev/zero", O_RDWR
);
1175 buf
= mmap(NULL
, bufsz
, PROT_READ
|PROT_WRITE
, MAP_FILE
|MAP_SHARED
,
1176 fd
, 0 /* offset */);
1177 saved_errno
= errno
;
1179 errno
= saved_errno
;
1183 if (buf
== MAP_FAILED
) {
1187 hdr
= (struct anonymous_shared_header
*)buf
;
1188 hdr
->u
.length
= bufsz
;
1190 ptr
= (void *)(&hdr
[1]);
1195 void *anonymous_shared_resize(void *ptr
, size_t new_size
, bool maymove
)
1199 size_t pagesz
= getpagesize();
1202 struct anonymous_shared_header
*hdr
;
1210 hdr
= (struct anonymous_shared_header
*)ptr
;
1212 if (hdr
->u
.length
> (new_size
+ sizeof(*hdr
))) {
1217 bufsz
= new_size
+ sizeof(*hdr
);
1219 /* round up to full pages */
1220 pagecnt
= bufsz
/ pagesz
;
1221 if (bufsz
% pagesz
) {
1224 bufsz
= pagesz
* pagecnt
;
1226 if (new_size
>= bufsz
) {
1232 if (bufsz
<= hdr
->u
.length
) {
1237 flags
= MREMAP_MAYMOVE
;
1240 buf
= mremap(hdr
, hdr
->u
.length
, bufsz
, flags
);
1242 if (buf
== MAP_FAILED
) {
1247 hdr
= (struct anonymous_shared_header
*)buf
;
1248 hdr
->u
.length
= bufsz
;
1250 ptr
= (void *)(&hdr
[1]);
1259 void anonymous_shared_free(void *ptr
)
1261 struct anonymous_shared_header
*hdr
;
1267 hdr
= (struct anonymous_shared_header
*)ptr
;
1271 munmap(hdr
, hdr
->u
.length
);
1275 /* used when you want a debugger started at a particular point in the
1276 code. Mostly useful in code that runs as a child process, where
1277 normal gdb attach is harder to organise.
1279 void samba_start_debugger(void)
1282 if (asprintf(&cmd
, "xterm -e \"gdb --pid %u\"&", getpid()) == -1) {
1285 if (system(cmd
) == -1) {