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/>.
29 #include "system/network.h"
30 #include "system/filesys.h"
31 #include "system/locale.h"
32 #include "system/shmem.h"
33 #include "system/passwd.h"
34 #include "system/time.h"
35 #include "system/wait.h"
37 #include "samba_util.h"
38 #include "lib/util/select.h"
40 #include <gnutls/gnutls.h>
42 #ifdef HAVE_SYS_PRCTL_H
43 #include <sys/prctl.h>
55 * @brief Misc utility functions
59 Find a suitable temporary directory. The result should be copied immediately
60 as it may be overwritten by a subsequent call.
62 _PUBLIC_
const char *tmpdir(void)
65 if ((p
= getenv("TMPDIR")))
72 Create a tmp file, open it and immediately unlink it.
73 If dir is NULL uses tmpdir()
74 Returns the file descriptor or -1 on error.
76 int create_unlink_tmp(const char *dir
)
78 size_t len
= strlen(dir
? dir
: (dir
= tmpdir()));
83 len
= snprintf(fname
, sizeof(fname
), "%s/listenerlock_XXXXXX", dir
);
84 if (len
>= sizeof(fname
)) {
88 mask
= umask(S_IRWXO
| S_IRWXG
);
94 if (unlink(fname
) == -1) {
95 int sys_errno
= errno
;
105 Check if a file exists - call vfs_file_exist for samba files.
107 _PUBLIC_
bool file_exist(const char *fname
)
111 if (stat(fname
, &st
) != 0) {
115 return ((S_ISREG(st
.st_mode
)) || (S_ISFIFO(st
.st_mode
)));
119 * @brief Return a files modification time.
121 * @param fname The name of the file.
123 * @param mt A pointer to store the modification time.
125 * @return 0 on success, errno otherwise.
127 _PUBLIC_
int file_modtime(const char *fname
, struct timespec
*mt
)
129 struct stat st
= {0};
131 if (stat(fname
, &st
) != 0) {
135 *mt
= get_mtimespec(&st
);
140 Check file permissions.
143 _PUBLIC_
bool file_check_permissions(const char *fname
,
157 ret
= stat(fname
, pst
);
159 DEBUG(0, ("stat failed on file '%s': %s\n",
160 fname
, strerror(errno
)));
164 if (pst
->st_uid
!= uid
&& !uid_wrapper_enabled()) {
165 DEBUG(0, ("invalid ownership of file '%s': "
166 "owned by uid %u, should be %u\n",
167 fname
, (unsigned int)pst
->st_uid
,
172 if ((pst
->st_mode
& 0777) != file_perms
) {
173 DEBUG(0, ("invalid permissions on file "
174 "'%s': has 0%o should be 0%o\n", fname
,
175 (unsigned int)(pst
->st_mode
& 0777),
176 (unsigned int)file_perms
));
184 Check if a directory exists.
187 _PUBLIC_
bool directory_exist(const char *dname
)
192 if (stat(dname
,&st
) != 0) {
196 ret
= S_ISDIR(st
.st_mode
);
203 * Try to create the specified directory if it didn't exist.
204 * A symlink to a directory is also accepted as a valid existing directory.
206 * @retval true if the directory already existed
207 * or was successfully created.
209 _PUBLIC_
bool directory_create_or_exist(const char *dname
,
215 /* Create directory */
216 old_umask
= umask(0);
217 ret
= mkdir(dname
, dir_perms
);
218 if (ret
== -1 && errno
!= EEXIST
) {
219 int dbg_level
= geteuid() == 0 ? DBGLVL_ERR
: DBGLVL_NOTICE
;
221 DBG_PREFIX(dbg_level
,
222 ("mkdir failed on directory %s: %s\n",
230 if (ret
!= 0 && errno
== EEXIST
) {
233 ret
= lstat(dname
, &sbuf
);
238 if (S_ISDIR(sbuf
.st_mode
)) {
242 if (S_ISLNK(sbuf
.st_mode
)) {
243 ret
= stat(dname
, &sbuf
);
248 if (S_ISDIR(sbuf
.st_mode
)) {
259 _PUBLIC_
bool directory_create_or_exists_recursive(
265 ok
= directory_create_or_exist(dname
, dir_perms
);
267 if (!directory_exist(dname
)) {
268 char tmp
[PATH_MAX
] = {0};
272 /* Use the null context */
273 n
= strlcpy(tmp
, dname
, sizeof(tmp
));
274 if (n
< strlen(dname
)) {
275 DBG_ERR("Path too long!\n");
279 parent
= dirname(tmp
);
280 if (parent
== NULL
) {
281 DBG_ERR("Failed to create dirname!\n");
285 ok
= directory_create_or_exists_recursive(parent
,
291 ok
= directory_create_or_exist(dname
, dir_perms
);
299 * @brief Try to create a specified directory if it doesn't exist.
301 * The function creates a directory with the given uid and permissions if it
302 * doesn't exist. If it exists it makes sure the uid and permissions are
303 * correct and it will fail if they are different.
305 * @param[in] dname The directory to create.
307 * @param[in] uid The uid the directory needs to belong too.
309 * @param[in] dir_perms The expected permissions of the directory.
311 * @return True on success, false on error.
313 _PUBLIC_
bool directory_create_or_exist_strict(const char *dname
,
321 ok
= directory_create_or_exist(dname
, dir_perms
);
326 rc
= lstat(dname
, &st
);
328 DEBUG(0, ("lstat failed on created directory %s: %s\n",
329 dname
, strerror(errno
)));
333 /* Check ownership and permission on existing directory */
334 if (!S_ISDIR(st
.st_mode
)) {
335 DEBUG(0, ("directory %s isn't a directory\n",
339 if (st
.st_uid
!= uid
&& !uid_wrapper_enabled()) {
340 DBG_NOTICE("invalid ownership on directory "
344 if ((st
.st_mode
& 0777) != dir_perms
) {
345 DEBUG(0, ("invalid permissions on directory "
346 "'%s': has 0%o should be 0%o\n", dname
,
347 (unsigned int)(st
.st_mode
& 0777), (unsigned int)dir_perms
));
356 Sleep for a specified number of milliseconds.
359 _PUBLIC_
void smb_msleep(unsigned int t
)
361 sys_poll_intr(NULL
, 0, t
);
365 Get my own name, return in talloc'ed storage.
368 _PUBLIC_
char *get_myname(TALLOC_CTX
*ctx
)
371 char hostname
[HOST_NAME_MAX
];
373 /* get my host name */
374 if (gethostname(hostname
, sizeof(hostname
)) == -1) {
375 DEBUG(0,("gethostname failed\n"));
379 /* Ensure null termination. */
380 hostname
[sizeof(hostname
)-1] = '\0';
382 /* split off any parts after an initial . */
383 p
= strchr_m(hostname
, '.');
388 return talloc_strdup(ctx
, hostname
);
392 Check if a process exists. Does this work on all unixes?
395 _PUBLIC_
bool process_exists_by_pid(pid_t pid
)
397 /* Doing kill with a non-positive pid causes messages to be
398 * sent to places we don't want. */
402 return(kill(pid
,0) == 0 || errno
!= ESRCH
);
406 Simple routine to do POSIX file locking. Cruft in NFS and 64->32 bit mapping
407 is dealt with in posix.c
410 _PUBLIC_
bool fcntl_lock(int fd
, int op
, off_t offset
, off_t count
, int type
)
415 DEBUG(8,("fcntl_lock %d %d %.0f %.0f %d\n",fd
,op
,(double)offset
,(double)count
,type
));
418 lock
.l_whence
= SEEK_SET
;
419 lock
.l_start
= offset
;
423 ret
= fcntl(fd
,op
,&lock
);
425 if (ret
== -1 && errno
!= 0)
426 DEBUG(3,("fcntl_lock: fcntl lock gave errno %d (%s)\n",errno
,strerror(errno
)));
431 (lock
.l_type
!= F_UNLCK
) &&
433 (lock
.l_pid
!= tevent_cached_getpid())) {
434 DEBUG(3,("fcntl_lock: fd %d is locked by pid %d\n",fd
,(int)lock
.l_pid
));
438 /* it must be not locked or locked by me */
442 /* a lock set or unset */
444 DEBUG(3,("fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
445 (double)offset
,(double)count
,op
,type
,strerror(errno
)));
449 /* everything went OK */
450 DEBUG(8,("fcntl_lock: Lock call successful\n"));
455 struct debug_channel_level
{
460 static void debugadd_channel_cb(const char *buf
, void *private_data
)
462 struct debug_channel_level
*dcl
=
463 (struct debug_channel_level
*)private_data
;
465 DEBUGADDC(dcl
->channel
, dcl
->level
,("%s", buf
));
468 static void debugadd_cb(const char *buf
, void *private_data
)
470 int *plevel
= (int *)private_data
;
471 DEBUGADD(*plevel
, ("%s", buf
));
474 void print_asc_cb(const uint8_t *buf
, int len
,
475 void (*cb
)(const char *buf
, void *private_data
),
482 for (i
=0; i
<len
; i
++) {
483 s
[0] = isprint(buf
[i
]) ? buf
[i
] : '.';
488 void print_asc(int level
, const uint8_t *buf
,int len
)
490 print_asc_cb(buf
, len
, debugadd_cb
, &level
);
493 static void dump_data_block16(const char *prefix
, size_t idx
,
494 const uint8_t *buf
, size_t len
,
495 void (*cb
)(const char *buf
, void *private_data
),
498 size_t prefix_len
= strlen(prefix
);
499 /* 16 (=%04zX) + 2 (=[]) + 1 (='\0') => 19 */
500 char tmp
[prefix_len
+ 19];
503 SMB_ASSERT(len
<= 16);
505 snprintf(tmp
, sizeof(tmp
), "%s[%04zX]", prefix
, idx
);
506 cb(tmp
, private_data
);
508 for (i
=0; i
<16; i
++) {
510 cb(" ", private_data
);
513 snprintf(tmp
, sizeof(tmp
), " %02X", (int)buf
[i
]);
515 snprintf(tmp
, sizeof(tmp
), " ");
517 cb(tmp
, private_data
);
520 cb(" ", private_data
);
523 cb("EMPTY BLOCK\n", private_data
);
527 for (i
=0; i
<len
; i
++) {
529 cb(" ", private_data
);
531 print_asc_cb(&buf
[i
], 1, cb
, private_data
);
534 cb("\n", private_data
);
538 * Write dump of binary data to a callback
540 void dump_data_cb(const uint8_t *buf
, int len
,
541 bool omit_zero_bytes
,
542 void (*cb
)(const char *buf
, void *private_data
),
546 bool skipped
= false;
550 for (i
=0;i
<len
;i
+=16) {
551 size_t remaining_len
= len
- i
;
552 size_t this_len
= MIN(remaining_len
, 16);
553 const uint8_t *this_buf
= &buf
[i
];
555 if ((omit_zero_bytes
== true) &&
556 (i
> 0) && (remaining_len
> 16) &&
557 (this_len
== 16) && all_zero(this_buf
, 16))
560 cb("skipping zero buffer bytes\n",
568 dump_data_block16("", i
, this_buf
, this_len
,
574 * Write dump of binary data to the log file.
576 * The data is only written if the log level is at least level.
578 _PUBLIC_
void dump_data(int level
, const uint8_t *buf
, int len
)
580 if (!DEBUGLVL(level
)) {
583 dump_data_cb(buf
, len
, false, debugadd_cb
, &level
);
587 * Write dump of binary data to the log file.
589 * The data is only written if the log level is at least level for
590 * debug class dbgc_class.
592 _PUBLIC_
void dump_data_dbgc(int dbgc_class
, int level
, const uint8_t *buf
, int len
)
594 struct debug_channel_level dcl
= { dbgc_class
, level
};
596 if (!DEBUGLVLC(dbgc_class
, level
)) {
599 dump_data_cb(buf
, len
, false, debugadd_channel_cb
, &dcl
);
603 * Write dump of binary data to the log file.
605 * The data is only written if the log level is at least level.
606 * 16 zero bytes in a row are omitted
608 _PUBLIC_
void dump_data_skip_zeros(int level
, const uint8_t *buf
, int len
)
610 if (!DEBUGLVL(level
)) {
613 dump_data_cb(buf
, len
, true, debugadd_cb
, &level
);
616 static void fprintf_cb(const char *buf
, void *private_data
)
618 FILE *f
= (FILE *)private_data
;
619 fprintf(f
, "%s", buf
);
622 void dump_data_file(const uint8_t *buf
, int len
, bool omit_zero_bytes
,
625 dump_data_cb(buf
, len
, omit_zero_bytes
, fprintf_cb
, f
);
629 * Write dump of compared binary data to a callback
631 void dump_data_diff_cb(const uint8_t *buf1
, size_t len1
,
632 const uint8_t *buf2
, size_t len2
,
633 bool omit_zero_bytes
,
634 void (*cb
)(const char *buf
, void *private_data
),
637 size_t len
= MAX(len1
, len2
);
639 bool skipped
= false;
641 for (i
=0; i
<len
; i
+=16) {
642 size_t remaining_len
= len
- i
;
643 size_t remaining_len1
= 0;
644 size_t this_len1
= 0;
645 const uint8_t *this_buf1
= NULL
;
646 size_t remaining_len2
= 0;
647 size_t this_len2
= 0;
648 const uint8_t *this_buf2
= NULL
;
651 remaining_len1
= len1
- i
;
652 this_len1
= MIN(remaining_len1
, 16);
653 this_buf1
= &buf1
[i
];
656 remaining_len2
= len2
- i
;
657 this_len2
= MIN(remaining_len2
, 16);
658 this_buf2
= &buf2
[i
];
661 if ((omit_zero_bytes
== true) &&
662 (i
> 0) && (remaining_len
> 16) &&
663 (this_len1
== 16) && all_zero(this_buf1
, 16) &&
664 (this_len2
== 16) && all_zero(this_buf2
, 16))
667 cb("skipping zero buffer bytes\n",
676 if ((this_len1
== this_len2
) &&
677 (memcmp(this_buf1
, this_buf2
, this_len1
) == 0))
679 dump_data_block16(" ", i
, this_buf1
, this_len1
,
684 dump_data_block16("-", i
, this_buf1
, this_len1
,
686 dump_data_block16("+", i
, this_buf2
, this_len2
,
691 _PUBLIC_
void dump_data_diff(int dbgc_class
, int level
,
692 bool omit_zero_bytes
,
693 const uint8_t *buf1
, size_t len1
,
694 const uint8_t *buf2
, size_t len2
)
696 struct debug_channel_level dcl
= { dbgc_class
, level
};
698 if (!DEBUGLVLC(dbgc_class
, level
)) {
701 dump_data_diff_cb(buf1
, len1
, buf2
, len2
, true, debugadd_channel_cb
, &dcl
);
704 _PUBLIC_
void dump_data_file_diff(FILE *f
,
705 bool omit_zero_bytes
,
706 const uint8_t *buf1
, size_t len1
,
707 const uint8_t *buf2
, size_t len2
)
709 dump_data_diff_cb(buf1
, len1
, buf2
, len2
, omit_zero_bytes
, fprintf_cb
, f
);
713 malloc that aborts with smb_panic on fail or zero size.
716 _PUBLIC_
void *smb_xmalloc(size_t size
)
720 smb_panic("smb_xmalloc: called with zero size.\n");
721 if ((p
= malloc(size
)) == NULL
)
722 smb_panic("smb_xmalloc: malloc fail.\n");
727 Memdup with smb_panic on fail.
730 _PUBLIC_
void *smb_xmemdup(const void *p
, size_t size
)
733 p2
= smb_xmalloc(size
);
739 strdup that aborts on malloc fail.
742 char *smb_xstrdup(const char *s
)
744 #if defined(PARANOID_MALLOC_CHECKER)
751 #define strdup rep_strdup
754 char *s1
= strdup(s
);
755 #if defined(PARANOID_MALLOC_CHECKER)
759 #define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
762 smb_panic("smb_xstrdup: malloc failed");
769 strndup that aborts on malloc fail.
772 char *smb_xstrndup(const char *s
, size_t n
)
774 #if defined(PARANOID_MALLOC_CHECKER)
780 #if (defined(BROKEN_STRNDUP) || !defined(HAVE_STRNDUP))
782 #define strndup rep_strndup
785 char *s1
= strndup(s
, n
);
786 #if defined(PARANOID_MALLOC_CHECKER)
790 #define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
793 smb_panic("smb_xstrndup: malloc failed");
801 Like strdup but for memory.
804 _PUBLIC_
void *smb_memdup(const void *p
, size_t size
)
817 * Write a password to the log file.
819 * @note Only actually does something if DEBUG_PASSWORD was defined during
822 _PUBLIC_
void dump_data_pw(const char *msg
, const uint8_t * data
, size_t len
)
824 #ifdef DEBUG_PASSWORD
825 DEBUG(11, ("%s", msg
));
826 if (data
!= NULL
&& len
> 0)
828 dump_data(11, data
, len
);
833 static void dump_data_addbuf_cb(const char *buf
, void *private_data
)
835 char **str
= private_data
;
836 talloc_asprintf_addbuf(str
, "%s", buf
);
839 _PUBLIC_
void dump_data_addbuf(const uint8_t *buf
, size_t buflen
, char **str
)
841 dump_data_cb(buf
, buflen
, false, dump_data_addbuf_cb
, str
);
846 * see if a range of memory is all zero. A NULL pointer is considered
849 _PUBLIC_
bool all_zero(const uint8_t *ptr
, size_t size
)
852 if (!ptr
) return true;
853 for (i
=0;i
<size
;i
++) {
854 if (ptr
[i
]) return false;
860 realloc an array, checking for integer overflow in the array size
862 _PUBLIC_
void *realloc_array(void *ptr
, size_t el_size
, unsigned count
, bool free_on_fail
)
864 #define MAX_MALLOC_SIZE 0x7fffffff
866 count
>= MAX_MALLOC_SIZE
/el_size
) {
872 return malloc(el_size
* count
);
874 return realloc(ptr
, el_size
* count
);
877 /****************************************************************************
879 ****************************************************************************/
881 void *malloc_array(size_t el_size
, unsigned int count
)
883 return realloc_array(NULL
, el_size
, count
, false);
886 /****************************************************************************
888 ****************************************************************************/
890 void *memalign_array(size_t el_size
, size_t align
, unsigned int count
)
892 if (el_size
== 0 || count
>= MAX_MALLOC_SIZE
/el_size
) {
896 return memalign(align
, el_size
*count
);
899 /****************************************************************************
901 ****************************************************************************/
903 void *calloc_array(size_t size
, size_t nmemb
)
905 if (nmemb
>= MAX_MALLOC_SIZE
/size
) {
908 if (size
== 0 || nmemb
== 0) {
911 return calloc(nmemb
, size
);
915 Trim the specified elements off the front and back of a string.
917 _PUBLIC_
bool trim_string(char *s
, const char *front
, const char *back
)
924 /* Ignore null or empty strings. */
925 if (!s
|| (s
[0] == '\0')) {
930 front_len
= front
? strlen(front
) : 0;
931 back_len
= back
? strlen(back
) : 0;
934 size_t front_trim
= 0;
936 while (strncmp(s
+front_trim
, front
, front_len
)==0) {
937 front_trim
+= front_len
;
939 if (front_trim
> 0) {
940 /* Must use memmove here as src & dest can
941 * easily overlap. Found by valgrind. JRA. */
942 memmove(s
, s
+front_trim
, (len
-front_trim
)+1);
949 while ((len
>= back_len
) && strncmp(s
+len
-back_len
,back
,back_len
)==0) {
950 s
[len
-back_len
]='\0';
959 Find the number of 'c' chars in a string
961 _PUBLIC_ _PURE_
size_t count_chars(const char *s
, char c
)
966 if (*s
== c
) count
++;
974 * Routine to get hex characters and turn them into a byte array.
975 * the array can be variable length.
976 * - "0xnn" or "0Xnn" is specially catered for.
977 * - The first non-hex-digit character (apart from possibly leading "0x"
978 * finishes the conversion and skips the rest of the input.
979 * - A single hex-digit character at the end of the string is skipped.
981 * valid examples: "0A5D15"; "0x123456"
983 _PUBLIC_
size_t strhex_to_str(char *p
, size_t p_len
, const char *strhex
, size_t strhex_len
)
986 size_t num_chars
= 0;
988 /* skip leading 0x prefix */
989 if (strncasecmp(strhex
, "0x", 2) == 0) {
990 i
+= 2; /* skip two chars */
993 while ((i
< strhex_len
) && (num_chars
< p_len
)) {
994 bool ok
= hex_byte(&strhex
[i
], (uint8_t *)&p
[num_chars
]);
1006 * Parse a hex string and return a data blob.
1008 _PUBLIC_ DATA_BLOB
strhex_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *strhex
)
1010 DATA_BLOB ret_blob
= data_blob_talloc(mem_ctx
, NULL
, strlen(strhex
)/2+1);
1011 if (ret_blob
.data
== NULL
) {
1012 /* ret_blob.length is already 0 */
1015 ret_blob
.length
= strhex_to_str((char *)ret_blob
.data
, ret_blob
.length
,
1023 * Parse a hex dump and return a data blob. Hex dump is structured as
1024 * is generated from dump_data_cb() elsewhere in this file
1027 _PUBLIC_ DATA_BLOB
hexdump_to_data_blob(TALLOC_CTX
*mem_ctx
, const char *hexdump
, size_t hexdump_len
)
1029 DATA_BLOB ret_blob
= { 0 };
1031 size_t char_count
= 0;
1032 /* hexdump line length is 77 chars long. We then use the ASCII representation of the bytes
1033 * at the end of the final line to calculate how many are in that line, minus the extra space
1035 size_t hexdump_byte_count
= (16 * (hexdump_len
/ 77));
1036 if (hexdump_len
% 77) {
1037 hexdump_byte_count
+= ((hexdump_len
% 77) - 59 - 2);
1040 ret_blob
= data_blob_talloc(mem_ctx
, NULL
, hexdump_byte_count
+1);
1041 for (; i
+1 < hexdump_len
&& hexdump
[i
] != 0 && hexdump
[i
+1] != 0; i
++) {
1043 i
+= 7; /* Skip the offset at the start of the line */
1044 if ((i
%77) < 56) { /* position 56 is after both hex chunks */
1045 if (hexdump
[i
] != ' ') {
1046 char_count
+= strhex_to_str((char *)&ret_blob
.data
[char_count
],
1047 hexdump_byte_count
- char_count
,
1057 ret_blob
.length
= char_count
;
1063 * Print a buf in hex. Assumes dst is at least (srclen*2)+1 large.
1065 _PUBLIC_
void hex_encode_buf(char *dst
, const uint8_t *src
, size_t srclen
)
1068 for (i
=0; i
<srclen
; i
++) {
1069 snprintf(dst
+ i
*2, 3, "%02X", src
[i
]);
1072 * Ensure 0-termination for 0-length buffers
1074 dst
[srclen
*2] = '\0';
1078 * talloc version of hex_encode_buf()
1080 _PUBLIC_
char *hex_encode_talloc(TALLOC_CTX
*mem_ctx
, const unsigned char *buff_in
, size_t len
)
1084 hex_buffer
= talloc_array(mem_ctx
, char, (len
*2)+1);
1088 hex_encode_buf(hex_buffer
, buff_in
, len
);
1089 talloc_set_name_const(hex_buffer
, hex_buffer
);
1094 variant of strcmp() that handles NULL ptrs
1096 _PUBLIC_
int strcmp_safe(const char *s1
, const char *s2
)
1101 if (s1
== NULL
|| s2
== NULL
) {
1104 return strcmp(s1
, s2
);
1109 return the number of bytes occupied by a buffer in ASCII format
1110 the result includes the null termination
1111 limited by 'n' bytes
1113 _PUBLIC_
size_t ascii_len_n(const char *src
, size_t n
)
1117 len
= strnlen(src
, n
);
1125 _PUBLIC_
bool mem_equal_const_time(const void *s1
, const void *s2
, size_t n
)
1127 /* Ensure we won't overflow the unsigned index used by gnutls. */
1128 SMB_ASSERT(n
<= UINT_MAX
);
1130 return gnutls_memcmp(s1
, s2
, n
) == 0;
1133 struct anonymous_shared_header
{
1140 /* Map a shared memory buffer of at least nelem counters. */
1141 void *anonymous_shared_allocate(size_t orig_bufsz
)
1145 size_t pagesz
= getpagesize();
1147 size_t bufsz
= orig_bufsz
;
1148 struct anonymous_shared_header
*hdr
;
1150 bufsz
+= sizeof(*hdr
);
1152 /* round up to full pages */
1153 pagecnt
= bufsz
/ pagesz
;
1154 if (bufsz
% pagesz
) {
1157 bufsz
= pagesz
* pagecnt
;
1159 if (orig_bufsz
>= bufsz
) {
1167 buf
= mmap(NULL
, bufsz
, PROT_READ
|PROT_WRITE
, MAP_ANON
|MAP_SHARED
,
1168 -1 /* fd */, 0 /* offset */);
1174 fd
= open("/dev/zero", O_RDWR
);
1179 buf
= mmap(NULL
, bufsz
, PROT_READ
|PROT_WRITE
, MAP_FILE
|MAP_SHARED
,
1180 fd
, 0 /* offset */);
1181 saved_errno
= errno
;
1183 errno
= saved_errno
;
1187 if (buf
== MAP_FAILED
) {
1191 hdr
= (struct anonymous_shared_header
*)buf
;
1192 hdr
->u
.length
= bufsz
;
1194 ptr
= (void *)(&hdr
[1]);
1199 void *anonymous_shared_resize(void *ptr
, size_t new_size
, bool maymove
)
1203 size_t pagesz
= getpagesize();
1206 struct anonymous_shared_header
*hdr
;
1214 hdr
= (struct anonymous_shared_header
*)ptr
;
1216 if (hdr
->u
.length
> (new_size
+ sizeof(*hdr
))) {
1221 bufsz
= new_size
+ sizeof(*hdr
);
1223 /* round up to full pages */
1224 pagecnt
= bufsz
/ pagesz
;
1225 if (bufsz
% pagesz
) {
1228 bufsz
= pagesz
* pagecnt
;
1230 if (new_size
>= bufsz
) {
1236 if (bufsz
<= hdr
->u
.length
) {
1241 flags
= MREMAP_MAYMOVE
;
1244 buf
= mremap(hdr
, hdr
->u
.length
, bufsz
, flags
);
1246 if (buf
== MAP_FAILED
) {
1251 hdr
= (struct anonymous_shared_header
*)buf
;
1252 hdr
->u
.length
= bufsz
;
1254 ptr
= (void *)(&hdr
[1]);
1263 void anonymous_shared_free(void *ptr
)
1265 struct anonymous_shared_header
*hdr
;
1271 hdr
= (struct anonymous_shared_header
*)ptr
;
1275 munmap(hdr
, hdr
->u
.length
);
1279 /* used when you want a debugger started at a particular point in the
1280 code. Mostly useful in code that runs as a child process, where
1281 normal gdb attach is harder to organise.
1283 void samba_start_debugger(void)
1290 ret
= pipe(ready_pipe
);
1291 SMB_ASSERT(ret
== 0);
1294 SMB_ASSERT(pid
>= 0);
1299 ret
= close(ready_pipe
[0]);
1300 SMB_ASSERT(ret
== 0);
1301 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
1303 * Make sure the child process can attach a debugger.
1305 * We don't check the error code as the debugger
1306 * will tell us if it can't attach.
1308 (void)prctl(PR_SET_PTRACER
, pid
, 0, 0, 0);
1310 ret
= write(ready_pipe
[1], &c
, 1);
1311 SMB_ASSERT(ret
== 1);
1313 ret
= close(ready_pipe
[1]);
1314 SMB_ASSERT(ret
== 0);
1316 /* Wait for gdb to attach. */
1321 ret
= close(ready_pipe
[1]);
1322 SMB_ASSERT(ret
== 0);
1324 ret
= read(ready_pipe
[0], &c
, 1);
1325 SMB_ASSERT(ret
== 1);
1327 ret
= close(ready_pipe
[0]);
1328 SMB_ASSERT(ret
== 0);
1330 ret
= asprintf(&cmd
, "gdb --pid %u", getppid());
1331 SMB_ASSERT(ret
!= -1);
1333 execlp("xterm", "xterm", "-e", cmd
, (char *) NULL
);
1334 smb_panic("execlp() failed");