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"
39 #include <gnutls/gnutls.h>
41 #ifdef HAVE_SYS_PRCTL_H
42 #include <sys/prctl.h>
54 * @brief Misc utility functions
58 Find a suitable temporary directory. The result should be copied immediately
59 as it may be overwritten by a subsequent call.
61 _PUBLIC_
const char *tmpdir(void)
64 if ((p
= getenv("TMPDIR")))
71 Create a tmp file, open it and immediately unlink it.
72 If dir is NULL uses tmpdir()
73 Returns the file descriptor or -1 on error.
75 int create_unlink_tmp(const char *dir
)
77 size_t len
= strlen(dir
? dir
: (dir
= tmpdir()));
82 len
= snprintf(fname
, sizeof(fname
), "%s/listenerlock_XXXXXX", dir
);
83 if (len
>= sizeof(fname
)) {
87 mask
= umask(S_IRWXO
| S_IRWXG
);
93 if (unlink(fname
) == -1) {
94 int sys_errno
= errno
;
104 Check if a file exists - call vfs_file_exist for samba files.
106 _PUBLIC_
bool file_exist(const char *fname
)
110 if (stat(fname
, &st
) != 0) {
114 return ((S_ISREG(st
.st_mode
)) || (S_ISFIFO(st
.st_mode
)));
118 Check a files mod time.
121 _PUBLIC_
time_t file_modtime(const char *fname
)
125 if (stat(fname
,&st
) != 0)
132 Check file permissions.
135 _PUBLIC_
bool file_check_permissions(const char *fname
,
149 ret
= stat(fname
, pst
);
151 DEBUG(0, ("stat failed on file '%s': %s\n",
152 fname
, strerror(errno
)));
156 if (pst
->st_uid
!= uid
&& !uid_wrapper_enabled()) {
157 DEBUG(0, ("invalid ownership of file '%s': "
158 "owned by uid %u, should be %u\n",
159 fname
, (unsigned int)pst
->st_uid
,
164 if ((pst
->st_mode
& 0777) != file_perms
) {
165 DEBUG(0, ("invalid permissions on file "
166 "'%s': has 0%o should be 0%o\n", fname
,
167 (unsigned int)(pst
->st_mode
& 0777),
168 (unsigned int)file_perms
));
176 Check if a directory exists.
179 _PUBLIC_
bool directory_exist(const char *dname
)
184 if (stat(dname
,&st
) != 0) {
188 ret
= S_ISDIR(st
.st_mode
);
195 * Try to create the specified directory if it didn't exist.
196 * A symlink to a directory is also accepted as a valid existing directory.
198 * @retval true if the directory already existed
199 * or was successfully created.
201 _PUBLIC_
bool directory_create_or_exist(const char *dname
,
207 /* Create directory */
208 old_umask
= umask(0);
209 ret
= mkdir(dname
, dir_perms
);
210 if (ret
== -1 && errno
!= EEXIST
) {
211 int dbg_level
= geteuid() == 0 ? DBGLVL_ERR
: DBGLVL_NOTICE
;
213 DBG_PREFIX(dbg_level
,
214 ("mkdir failed on directory %s: %s\n",
222 if (ret
!= 0 && errno
== EEXIST
) {
225 ret
= lstat(dname
, &sbuf
);
230 if (S_ISDIR(sbuf
.st_mode
)) {
234 if (S_ISLNK(sbuf
.st_mode
)) {
235 ret
= stat(dname
, &sbuf
);
240 if (S_ISDIR(sbuf
.st_mode
)) {
251 _PUBLIC_
bool directory_create_or_exists_recursive(
257 ok
= directory_create_or_exist(dname
, dir_perms
);
259 if (!directory_exist(dname
)) {
260 char tmp
[PATH_MAX
] = {0};
264 /* Use the null context */
265 n
= strlcpy(tmp
, dname
, sizeof(tmp
));
266 if (n
< strlen(dname
)) {
267 DBG_ERR("Path too long!\n");
271 parent
= dirname(tmp
);
272 if (parent
== NULL
) {
273 DBG_ERR("Failed to create dirname!\n");
277 ok
= directory_create_or_exists_recursive(parent
,
283 ok
= directory_create_or_exist(dname
, dir_perms
);
291 * @brief Try to create a specified directory if it doesn't exist.
293 * The function creates a directory with the given uid and permissions if it
294 * doesn't exist. If it exists it makes sure the uid and permissions are
295 * correct and it will fail if they are different.
297 * @param[in] dname The directory to create.
299 * @param[in] uid The uid the directory needs to belong too.
301 * @param[in] dir_perms The expected permissions of the directory.
303 * @return True on success, false on error.
305 _PUBLIC_
bool directory_create_or_exist_strict(const char *dname
,
313 ok
= directory_create_or_exist(dname
, dir_perms
);
318 rc
= lstat(dname
, &st
);
320 DEBUG(0, ("lstat failed on created directory %s: %s\n",
321 dname
, strerror(errno
)));
325 /* Check ownership and permission on existing directory */
326 if (!S_ISDIR(st
.st_mode
)) {
327 DEBUG(0, ("directory %s isn't a directory\n",
331 if (st
.st_uid
!= uid
&& !uid_wrapper_enabled()) {
332 DBG_NOTICE("invalid ownership on directory "
336 if ((st
.st_mode
& 0777) != dir_perms
) {
337 DEBUG(0, ("invalid permissions on directory "
338 "'%s': has 0%o should be 0%o\n", dname
,
339 (unsigned int)(st
.st_mode
& 0777), (unsigned int)dir_perms
));
348 Sleep for a specified number of milliseconds.
351 _PUBLIC_
void smb_msleep(unsigned int t
)
353 sys_poll_intr(NULL
, 0, t
);
357 Get my own name, return in talloc'ed storage.
360 _PUBLIC_
char *get_myname(TALLOC_CTX
*ctx
)
363 char hostname
[HOST_NAME_MAX
];
365 /* get my host name */
366 if (gethostname(hostname
, sizeof(hostname
)) == -1) {
367 DEBUG(0,("gethostname failed\n"));
371 /* Ensure null termination. */
372 hostname
[sizeof(hostname
)-1] = '\0';
374 /* split off any parts after an initial . */
375 p
= strchr_m(hostname
, '.');
380 return talloc_strdup(ctx
, hostname
);
384 Check if a process exists. Does this work on all unixes?
387 _PUBLIC_
bool process_exists_by_pid(pid_t pid
)
389 /* Doing kill with a non-positive pid causes messages to be
390 * sent to places we don't want. */
394 return(kill(pid
,0) == 0 || errno
!= ESRCH
);
398 Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
399 is dealt with in posix.c
402 _PUBLIC_
bool fcntl_lock(int fd
, int op
, off_t offset
, off_t count
, int type
)
407 DEBUG(8,("fcntl_lock %d %d %.0f %.0f %d\n",fd
,op
,(double)offset
,(double)count
,type
));
410 lock
.l_whence
= SEEK_SET
;
411 lock
.l_start
= offset
;
415 ret
= fcntl(fd
,op
,&lock
);
417 if (ret
== -1 && errno
!= 0)
418 DEBUG(3,("fcntl_lock: fcntl lock gave errno %d (%s)\n",errno
,strerror(errno
)));
423 (lock
.l_type
!= F_UNLCK
) &&
425 (lock
.l_pid
!= getpid())) {
426 DEBUG(3,("fcntl_lock: fd %d is locked by pid %d\n",fd
,(int)lock
.l_pid
));
430 /* it must be not locked or locked by me */
434 /* a lock set or unset */
436 DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
437 (double)offset
,(double)count
,op
,type
,strerror(errno
)));
441 /* everything went OK */
442 DEBUG(8,("fcntl_lock: Lock call successful\n"));
447 struct debug_channel_level
{
452 static void debugadd_channel_cb(const char *buf
, void *private_data
)
454 struct debug_channel_level
*dcl
=
455 (struct debug_channel_level
*)private_data
;
457 DEBUGADDC(dcl
->channel
, dcl
->level
,("%s", buf
));
460 static void debugadd_cb(const char *buf
, void *private_data
)
462 int *plevel
= (int *)private_data
;
463 DEBUGADD(*plevel
, ("%s", buf
));
466 void print_asc_cb(const uint8_t *buf
, int len
,
467 void (*cb
)(const char *buf
, void *private_data
),
474 for (i
=0; i
<len
; i
++) {
475 s
[0] = isprint(buf
[i
]) ? buf
[i
] : '.';
480 void print_asc(int level
, const uint8_t *buf
,int len
)
482 print_asc_cb(buf
, len
, debugadd_cb
, &level
);
485 static void dump_data_block16(const char *prefix
, size_t idx
,
486 const uint8_t *buf
, size_t len
,
487 void (*cb
)(const char *buf
, void *private_data
),
493 SMB_ASSERT(len
>= 0 && len
<= 16);
495 snprintf(tmp
, sizeof(tmp
), "%s[%04zX]", prefix
, idx
);
496 cb(tmp
, private_data
);
498 for (i
=0; i
<16; i
++) {
500 cb(" ", private_data
);
503 snprintf(tmp
, sizeof(tmp
), " %02X", (int)buf
[i
]);
505 snprintf(tmp
, sizeof(tmp
), " ");
507 cb(tmp
, private_data
);
510 cb(" ", private_data
);
513 cb("EMPTY BLOCK\n", private_data
);
517 for (i
=0; i
<len
; i
++) {
519 cb(" ", private_data
);
521 print_asc_cb(&buf
[i
], 1, cb
, private_data
);
524 cb("\n", private_data
);
528 * Write dump of binary data to a callback
530 void dump_data_cb(const uint8_t *buf
, int len
,
531 bool omit_zero_bytes
,
532 void (*cb
)(const char *buf
, void *private_data
),
536 bool skipped
= false;
540 for (i
=0;i
<len
;i
+=16) {
541 size_t remaining_len
= len
- i
;
542 size_t this_len
= MIN(remaining_len
, 16);
543 const uint8_t *this_buf
= &buf
[i
];
545 if ((omit_zero_bytes
== true) &&
546 (i
> 0) && (remaining_len
> 16) &&
547 (this_len
== 16) && all_zero(this_buf
, 16))
550 cb("skipping zero buffer bytes\n",
558 dump_data_block16("", i
, this_buf
, this_len
,
564 * Write dump of binary data to the log file.
566 * The data is only written if the log level is at least level.
568 _PUBLIC_
void dump_data(int level
, const uint8_t *buf
, int len
)
570 if (!DEBUGLVL(level
)) {
573 dump_data_cb(buf
, len
, false, debugadd_cb
, &level
);
577 * Write dump of binary data to the log file.
579 * The data is only written if the log level is at least level for
580 * debug class dbgc_class.
582 _PUBLIC_
void dump_data_dbgc(int dbgc_class
, int level
, const uint8_t *buf
, int len
)
584 struct debug_channel_level dcl
= { dbgc_class
, level
};
586 if (!DEBUGLVLC(dbgc_class
, level
)) {
589 dump_data_cb(buf
, len
, false, debugadd_channel_cb
, &dcl
);
593 * Write dump of binary data to the log file.
595 * The data is only written if the log level is at least level.
596 * 16 zero bytes in a row are omitted
598 _PUBLIC_
void dump_data_skip_zeros(int level
, const uint8_t *buf
, int len
)
600 if (!DEBUGLVL(level
)) {
603 dump_data_cb(buf
, len
, true, debugadd_cb
, &level
);
606 static void fprintf_cb(const char *buf
, void *private_data
)
608 FILE *f
= (FILE *)private_data
;
609 fprintf(f
, "%s", buf
);
612 void dump_data_file(const uint8_t *buf
, int len
, bool omit_zero_bytes
,
615 dump_data_cb(buf
, len
, omit_zero_bytes
, fprintf_cb
, f
);
619 * Write dump of compared binary data to a callback
621 void dump_data_diff_cb(const uint8_t *buf1
, size_t len1
,
622 const uint8_t *buf2
, size_t len2
,
623 bool omit_zero_bytes
,
624 void (*cb
)(const char *buf
, void *private_data
),
627 size_t len
= MAX(len1
, len2
);
629 bool skipped
= false;
631 for (i
=0; i
<len
; i
+=16) {
632 size_t remaining_len
= len
- i
;
633 size_t remaining_len1
= 0;
634 size_t this_len1
= 0;
635 const uint8_t *this_buf1
= NULL
;
636 size_t remaining_len2
= 0;
637 size_t this_len2
= 0;
638 const uint8_t *this_buf2
= NULL
;
641 remaining_len1
= len1
- i
;
642 this_len1
= MIN(remaining_len1
, 16);
643 this_buf1
= &buf1
[i
];
646 remaining_len2
= len2
- i
;
647 this_len2
= MIN(remaining_len2
, 16);
648 this_buf2
= &buf2
[i
];
651 if ((omit_zero_bytes
== true) &&
652 (i
> 0) && (remaining_len
> 16) &&
653 (this_len1
== 16) && all_zero(this_buf1
, 16) &&
654 (this_len2
== 16) && all_zero(this_buf2
, 16))
657 cb("skipping zero buffer bytes\n",
666 if ((this_len1
== this_len2
) &&
667 (memcmp(this_buf1
, this_buf2
, this_len1
) == 0))
669 dump_data_block16(" ", i
, this_buf1
, this_len1
,
674 dump_data_block16("-", i
, this_buf1
, this_len1
,
676 dump_data_block16("+", i
, this_buf2
, this_len2
,
681 _PUBLIC_
void dump_data_diff(int dbgc_class
, int level
,
682 bool omit_zero_bytes
,
683 const uint8_t *buf1
, size_t len1
,
684 const uint8_t *buf2
, size_t len2
)
686 struct debug_channel_level dcl
= { dbgc_class
, level
};
688 if (!DEBUGLVLC(dbgc_class
, level
)) {
691 dump_data_diff_cb(buf1
, len1
, buf2
, len2
, true, debugadd_channel_cb
, &dcl
);
694 _PUBLIC_
void dump_data_file_diff(FILE *f
,
695 bool omit_zero_bytes
,
696 const uint8_t *buf1
, size_t len1
,
697 const uint8_t *buf2
, size_t len2
)
699 dump_data_diff_cb(buf1
, len1
, buf2
, len2
, omit_zero_bytes
, fprintf_cb
, f
);
703 malloc that aborts with smb_panic on fail or zero size.
706 _PUBLIC_
void *smb_xmalloc(size_t size
)
710 smb_panic("smb_xmalloc: called with zero size.\n");
711 if ((p
= malloc(size
)) == NULL
)
712 smb_panic("smb_xmalloc: malloc fail.\n");
717 Memdup with smb_panic on fail.
720 _PUBLIC_
void *smb_xmemdup(const void *p
, size_t size
)
723 p2
= smb_xmalloc(size
);
729 strdup that aborts on malloc fail.
732 char *smb_xstrdup(const char *s
)
734 #if defined(PARANOID_MALLOC_CHECKER)
741 #define strdup rep_strdup
744 char *s1
= strdup(s
);
745 #if defined(PARANOID_MALLOC_CHECKER)
749 #define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
752 smb_panic("smb_xstrdup: malloc failed");
759 strndup that aborts on malloc fail.
762 char *smb_xstrndup(const char *s
, size_t n
)
764 #if defined(PARANOID_MALLOC_CHECKER)
770 #if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
772 #define strndup rep_strndup
775 char *s1
= strndup(s
, n
);
776 #if defined(PARANOID_MALLOC_CHECKER)
780 #define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
783 smb_panic("smb_xstrndup: malloc failed");
791 Like strdup but for memory.
794 _PUBLIC_
void *smb_memdup(const void *p
, size_t size
)
807 * Write a password to the log file.
809 * @note Only actually does something if DEBUG_PASSWORD was defined during
812 _PUBLIC_
void dump_data_pw(const char *msg
, const uint8_t * data
, size_t len
)
814 #ifdef DEBUG_PASSWORD
815 DEBUG(11, ("%s", msg
));
816 if (data
!= NULL
&& len
> 0)
818 dump_data(11, data
, len
);
825 * see if a range of memory is all zero. A NULL pointer is considered
828 _PUBLIC_
bool all_zero(const uint8_t *ptr
, size_t size
)
831 if (!ptr
) return true;
832 for (i
=0;i
<size
;i
++) {
833 if (ptr
[i
]) return false;
839 realloc an array, checking for integer overflow in the array size
841 _PUBLIC_
void *realloc_array(void *ptr
, size_t el_size
, unsigned count
, bool free_on_fail
)
843 #define MAX_MALLOC_SIZE 0x7fffffff
845 count
>= MAX_MALLOC_SIZE
/el_size
) {
851 return malloc(el_size
* count
);
853 return realloc(ptr
, el_size
* count
);
856 /****************************************************************************
858 ****************************************************************************/
860 void *malloc_array(size_t el_size
, unsigned int count
)
862 return realloc_array(NULL
, el_size
, count
, false);
865 /****************************************************************************
867 ****************************************************************************/
869 void *memalign_array(size_t el_size
, size_t align
, unsigned int count
)
871 if (el_size
== 0 || count
>= MAX_MALLOC_SIZE
/el_size
) {
875 return memalign(align
, el_size
*count
);
878 /****************************************************************************
880 ****************************************************************************/
882 void *calloc_array(size_t size
, size_t nmemb
)
884 if (nmemb
>= MAX_MALLOC_SIZE
/size
) {
887 if (size
== 0 || nmemb
== 0) {
890 return calloc(nmemb
, size
);
894 Trim the specified elements off the front and back of a string.
896 _PUBLIC_
bool trim_string(char *s
, const char *front
, const char *back
)
903 /* Ignore null or empty strings. */
904 if (!s
|| (s
[0] == '\0')) {
909 front_len
= front
? strlen(front
) : 0;
910 back_len
= back
? strlen(back
) : 0;
913 size_t front_trim
= 0;
915 while (strncmp(s
+front_trim
, front
, front_len
)==0) {
916 front_trim
+= front_len
;
918 if (front_trim
> 0) {
919 /* Must use memmove here as src & dest can
920 * easily overlap. Found by valgrind. JRA. */
921 memmove(s
, s
+front_trim
, (len
-front_trim
)+1);
928 while ((len
>= back_len
) && strncmp(s
+len
-back_len
,back
,back_len
)==0) {
929 s
[len
-back_len
]='\0';
938 Find the number of 'c' chars in a string
940 _PUBLIC_ _PURE_
size_t count_chars(const char *s
, char c
)
945 if (*s
== c
) count
++;
953 * Routine to get hex characters and turn them into a byte array.
954 * the array can be variable length.
955 * - "0xnn" or "0Xnn" is specially catered for.
956 * - The first non-hex-digit character (apart from possibly leading "0x"
957 * finishes the conversion and skips the rest of the input.
958 * - A single hex-digit character at the end of the string is skipped.
960 * valid examples: "0A5D15"; "0x123456"
962 _PUBLIC_
size_t strhex_to_str(char *p
, size_t p_len
, const char *strhex
, size_t strhex_len
)
965 size_t num_chars
= 0;
967 /* skip leading 0x prefix */
968 if (strncasecmp(strhex
, "0x", 2) == 0) {
969 i
+= 2; /* skip two chars */
972 while ((i
< strhex_len
) && (num_chars
< p_len
)) {
973 bool ok
= hex_byte(&strhex
[i
], (uint8_t *)&p
[num_chars
]);
985 * Parse a hex string and return a data blob.
987 _PUBLIC_ DATA_BLOB
strhex_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *strhex
)
989 DATA_BLOB ret_blob
= data_blob_talloc(mem_ctx
, NULL
, strlen(strhex
)/2+1);
991 ret_blob
.length
= strhex_to_str((char *)ret_blob
.data
, ret_blob
.length
,
999 * Parse a hex dump and return a data blob. Hex dump is structured as
1000 * is generated from dump_data_cb() elsewhere in this file
1003 _PUBLIC_ DATA_BLOB
hexdump_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *hexdump
, size_t hexdump_len
)
1005 DATA_BLOB ret_blob
= { 0 };
1007 size_t char_count
= 0;
1008 /* hexdump line length is 77 chars long. We then use the ASCII representation of the bytes
1009 * at the end of the final line to calculate how many are in that line, minus the extra space
1011 size_t hexdump_byte_count
= (16 * (hexdump_len
/ 77));
1012 if (hexdump_len
% 77) {
1013 hexdump_byte_count
+= ((hexdump_len
% 77) - 59 - 2);
1016 ret_blob
= data_blob_talloc(mem_ctx
, NULL
, hexdump_byte_count
+1);
1017 for (; i
+1 < hexdump_len
&& hexdump
[i
] != 0 && hexdump
[i
+1] != 0; i
++) {
1019 i
+= 7; /* Skip the offset at the start of the line */
1020 if ((i
%77) < 56) { /* position 56 is after both hex chunks */
1021 if (hexdump
[i
] != ' ') {
1022 char_count
+= strhex_to_str((char *)&ret_blob
.data
[char_count
],
1023 hexdump_byte_count
- char_count
,
1033 ret_blob
.length
= char_count
;
1039 * Print a buf in hex. Assumes dst is at least (srclen*2)+1 large.
1041 _PUBLIC_
void hex_encode_buf(char *dst
, const uint8_t *src
, size_t srclen
)
1044 for (i
=0; i
<srclen
; i
++) {
1045 snprintf(dst
+ i
*2, 3, "%02X", src
[i
]);
1048 * Ensure 0-termination for 0-length buffers
1050 dst
[srclen
*2] = '\0';
1054 * talloc version of hex_encode_buf()
1056 _PUBLIC_
char *hex_encode_talloc(TALLOC_CTX
*mem_ctx
, const unsigned char *buff_in
, size_t len
)
1060 hex_buffer
= talloc_array(mem_ctx
, char, (len
*2)+1);
1064 hex_encode_buf(hex_buffer
, buff_in
, len
);
1065 talloc_set_name_const(hex_buffer
, hex_buffer
);
1070 varient of strcmp() that handles NULL ptrs
1072 _PUBLIC_
int strcmp_safe(const char *s1
, const char *s2
)
1077 if (s1
== NULL
|| s2
== NULL
) {
1080 return strcmp(s1
, s2
);
1085 return the number of bytes occupied by a buffer in ASCII format
1086 the result includes the null termination
1087 limited by 'n' bytes
1089 _PUBLIC_
size_t ascii_len_n(const char *src
, size_t n
)
1093 len
= strnlen(src
, n
);
1101 _PUBLIC_
bool mem_equal_const_time(const void *s1
, const void *s2
, size_t n
)
1103 /* Ensure we won't overflow the unsigned index used by gnutls. */
1104 SMB_ASSERT(n
<= UINT_MAX
);
1106 return gnutls_memcmp(s1
, s2
, n
) == 0;
1109 struct anonymous_shared_header
{
1116 /* Map a shared memory buffer of at least nelem counters. */
1117 void *anonymous_shared_allocate(size_t orig_bufsz
)
1121 size_t pagesz
= getpagesize();
1123 size_t bufsz
= orig_bufsz
;
1124 struct anonymous_shared_header
*hdr
;
1126 bufsz
+= sizeof(*hdr
);
1128 /* round up to full pages */
1129 pagecnt
= bufsz
/ pagesz
;
1130 if (bufsz
% pagesz
) {
1133 bufsz
= pagesz
* pagecnt
;
1135 if (orig_bufsz
>= bufsz
) {
1143 buf
= mmap(NULL
, bufsz
, PROT_READ
|PROT_WRITE
, MAP_ANON
|MAP_SHARED
,
1144 -1 /* fd */, 0 /* offset */);
1150 fd
= open("/dev/zero", O_RDWR
);
1155 buf
= mmap(NULL
, bufsz
, PROT_READ
|PROT_WRITE
, MAP_FILE
|MAP_SHARED
,
1156 fd
, 0 /* offset */);
1157 saved_errno
= errno
;
1159 errno
= saved_errno
;
1163 if (buf
== MAP_FAILED
) {
1167 hdr
= (struct anonymous_shared_header
*)buf
;
1168 hdr
->u
.length
= bufsz
;
1170 ptr
= (void *)(&hdr
[1]);
1175 void *anonymous_shared_resize(void *ptr
, size_t new_size
, bool maymove
)
1179 size_t pagesz
= getpagesize();
1182 struct anonymous_shared_header
*hdr
;
1190 hdr
= (struct anonymous_shared_header
*)ptr
;
1192 if (hdr
->u
.length
> (new_size
+ sizeof(*hdr
))) {
1197 bufsz
= new_size
+ sizeof(*hdr
);
1199 /* round up to full pages */
1200 pagecnt
= bufsz
/ pagesz
;
1201 if (bufsz
% pagesz
) {
1204 bufsz
= pagesz
* pagecnt
;
1206 if (new_size
>= bufsz
) {
1212 if (bufsz
<= hdr
->u
.length
) {
1217 flags
= MREMAP_MAYMOVE
;
1220 buf
= mremap(hdr
, hdr
->u
.length
, bufsz
, flags
);
1222 if (buf
== MAP_FAILED
) {
1227 hdr
= (struct anonymous_shared_header
*)buf
;
1228 hdr
->u
.length
= bufsz
;
1230 ptr
= (void *)(&hdr
[1]);
1239 void anonymous_shared_free(void *ptr
)
1241 struct anonymous_shared_header
*hdr
;
1247 hdr
= (struct anonymous_shared_header
*)ptr
;
1251 munmap(hdr
, hdr
->u
.length
);
1255 /* used when you want a debugger started at a particular point in the
1256 code. Mostly useful in code that runs as a child process, where
1257 normal gdb attach is harder to organise.
1259 void samba_start_debugger(void)
1266 ret
= pipe(ready_pipe
);
1267 SMB_ASSERT(ret
== 0);
1270 SMB_ASSERT(pid
>= 0);
1275 ret
= close(ready_pipe
[0]);
1276 SMB_ASSERT(ret
== 0);
1277 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
1279 * Make sure the child process can attach a debugger.
1281 * We don't check the error code as the debugger
1282 * will tell us if it can't attach.
1284 (void)prctl(PR_SET_PTRACER
, pid
, 0, 0, 0);
1286 ret
= write(ready_pipe
[1], &c
, 1);
1287 SMB_ASSERT(ret
== 1);
1289 ret
= close(ready_pipe
[1]);
1290 SMB_ASSERT(ret
== 0);
1292 /* Wait for gdb to attach. */
1297 ret
= close(ready_pipe
[1]);
1298 SMB_ASSERT(ret
== 0);
1300 ret
= read(ready_pipe
[0], &c
, 1);
1301 SMB_ASSERT(ret
== 1);
1303 ret
= close(ready_pipe
[0]);
1304 SMB_ASSERT(ret
== 0);
1306 ret
= asprintf(&cmd
, "gdb --pid %u", getppid());
1307 SMB_ASSERT(ret
!= -1);
1309 execlp("xterm", "xterm", "-e", cmd
, (char *) NULL
);
1310 smb_panic("execlp() failed");