diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/async_req/async_sock.c | 2 | ||||
-rw-r--r-- | lib/replace/libreplace_network.m4 | 1 | ||||
-rw-r--r-- | lib/replace/system/network.h | 4 | ||||
-rw-r--r-- | lib/talloc/talloc.c | 35 | ||||
-rw-r--r-- | lib/talloc/talloc.h | 2 | ||||
-rw-r--r-- | lib/tdr/TODO | 1 | ||||
-rw-r--r-- | lib/tdr/config.mk | 9 | ||||
-rw-r--r-- | lib/tdr/tdr.c | 397 | ||||
-rw-r--r-- | lib/tdr/tdr.h | 67 | ||||
-rw-r--r-- | lib/tdr/testsuite.c | 185 | ||||
-rw-r--r-- | lib/tevent/configure.ac | 2 | ||||
-rw-r--r-- | lib/tevent/tevent.c | 170 | ||||
-rw-r--r-- | lib/tevent/tevent.h | 39 | ||||
-rw-r--r-- | lib/tevent/tevent_epoll.c | 6 | ||||
-rw-r--r-- | lib/tevent/tevent_internal.h | 12 | ||||
-rw-r--r-- | lib/tevent/tevent_select.c | 6 | ||||
-rw-r--r-- | lib/tevent/tevent_standard.c | 6 | ||||
-rw-r--r-- | lib/util/fault.m4 | 1 |
18 files changed, 922 insertions, 23 deletions
diff --git a/lib/async_req/async_sock.c b/lib/async_req/async_sock.c index f803b9cc36..be24bae6df 100644 --- a/lib/async_req/async_sock.c +++ b/lib/async_req/async_sock.c @@ -485,7 +485,7 @@ static void writev_handler(struct tevent_context *ev, struct tevent_fd *fde, state->iov[0].iov_len -= written; break; } - written = state->iov[0].iov_len; + written -= state->iov[0].iov_len; state->iov += 1; state->count -= 1; } diff --git a/lib/replace/libreplace_network.m4 b/lib/replace/libreplace_network.m4 index 78fb1abaf0..1dc1c44ed8 100644 --- a/lib/replace/libreplace_network.m4 +++ b/lib/replace/libreplace_network.m4 @@ -16,6 +16,7 @@ AC_CHECK_HEADERS([netinet/ip.h], [], [],[#ifdef HAVE_NETINET_IN_H #endif]) AC_CHECK_HEADERS(netinet/tcp.h netinet/in_ip.h) AC_CHECK_HEADERS(sys/sockio.h sys/un.h) +AC_CHECK_HEADERS(sys/uio.h) dnl we need to check that net/if.h really can be used, to cope with hpux dnl where including it always fails diff --git a/lib/replace/system/network.h b/lib/replace/system/network.h index f135d175d4..6add99c0db 100644 --- a/lib/replace/system/network.h +++ b/lib/replace/system/network.h @@ -83,6 +83,10 @@ #include <sys/ioctl.h> #endif +#ifdef HAVE_SYS_UIO_H +#include <sys/uio.h> +#endif + #ifdef HAVE_STROPTS_H #include <stropts.h> #endif diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index c472e9fda9..60a48ad811 100644 --- a/lib/talloc/talloc.c +++ b/lib/talloc/talloc.c @@ -138,14 +138,30 @@ struct talloc_chunk { #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15) #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc)) +static void (*talloc_abort_fn)(const char *reason); + +void talloc_set_abort_fn(void (*abort_fn)(const char *reason)) +{ + talloc_abort_fn = abort_fn; +} + +static void talloc_abort(const char *reason) +{ + if (!talloc_abort_fn) { + TALLOC_ABORT(reason); + } + + talloc_abort_fn(reason); +} + static void talloc_abort_double_free(void) { - TALLOC_ABORT("Bad talloc magic value - double free"); + talloc_abort("Bad talloc magic value - double free"); } static void talloc_abort_unknown_value(void) { - TALLOC_ABORT("Bad talloc magic value - unknown value"); + talloc_abort("Bad talloc magic value - unknown value"); } /* panic if we get a bad magic value */ @@ -564,7 +580,7 @@ static inline int _talloc_free(void *ptr) pool_object_count = talloc_pool_objectcount(pool); if (*pool_object_count == 0) { - TALLOC_ABORT("Pool object count zero!"); + talloc_abort("Pool object count zero!"); } *pool_object_count -= 1; @@ -810,7 +826,18 @@ static void talloc_abort_type_missmatch(const char *location, const char *name, const char *expected) { - TALLOC_ABORT("Type missmatch"); + const char *reason; + + reason = talloc_asprintf(NULL, + "%s: Type mismatch: name[%s] expected[%s]", + location, + name?name:"NULL", + expected); + if (!reason) { + reason = "Type mismatch"; + } + + talloc_abort(reason); } void *_talloc_get_type_abort(const void *ptr, const char *name, const char *location) diff --git a/lib/talloc/talloc.h b/lib/talloc/talloc.h index 002e06e52d..b62393494b 100644 --- a/lib/talloc/talloc.h +++ b/lib/talloc/talloc.h @@ -182,4 +182,6 @@ char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3) char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); +void talloc_set_abort_fn(void (*abort_fn)(const char *reason)); + #endif diff --git a/lib/tdr/TODO b/lib/tdr/TODO new file mode 100644 index 0000000000..5093afd438 --- /dev/null +++ b/lib/tdr/TODO @@ -0,0 +1 @@ +- Support read/write (to fd) as well as push/pull (to DATA_BLOB) diff --git a/lib/tdr/config.mk b/lib/tdr/config.mk new file mode 100644 index 0000000000..07506ec647 --- /dev/null +++ b/lib/tdr/config.mk @@ -0,0 +1,9 @@ +[SUBSYSTEM::TDR] +CFLAGS = -Ilib/tdr +PUBLIC_DEPENDENCIES = LIBTALLOC LIBSAMBA-UTIL + +TDR_OBJ_FILES = $(libtdrsrcdir)/tdr.o + +$(eval $(call proto_header_template,$(libtdrsrcdir)/tdr_proto.h,$(TDR_OBJ_FILES:.o=.c))) + +PUBLIC_HEADERS += $(libtdrsrcdir)/tdr.h diff --git a/lib/tdr/tdr.c b/lib/tdr/tdr.c new file mode 100644 index 0000000000..293436ed5e --- /dev/null +++ b/lib/tdr/tdr.c @@ -0,0 +1,397 @@ +/* + Unix SMB/CIFS implementation. + + TDR (Trivial Data Representation) helper functions + Based loosely on ndr.c by Andrew Tridgell. + + Copyright (C) Jelmer Vernooij 2005 + + 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 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "system/filesys.h" +#include "system/network.h" +#include "lib/tdr/tdr.h" + +#define TDR_BASE_MARSHALL_SIZE 1024 + +#define TDR_PUSH_NEED_BYTES(tdr, n) TDR_CHECK(tdr_push_expand(tdr, tdr->data.length+(n))) + +#define TDR_PULL_NEED_BYTES(tdr, n) do { \ + if ((n) > tdr->data.length || tdr->offset + (n) > tdr->data.length) { \ + return NT_STATUS_BUFFER_TOO_SMALL; \ + } \ +} while(0) + +#define TDR_BE(tdr) ((tdr)->flags & TDR_BIG_ENDIAN) + +#define TDR_CVAL(tdr, ofs) CVAL(tdr->data.data,ofs) +#define TDR_SVAL(tdr, ofs) (TDR_BE(tdr)?RSVAL(tdr->data.data,ofs):SVAL(tdr->data.data,ofs)) +#define TDR_IVAL(tdr, ofs) (TDR_BE(tdr)?RIVAL(tdr->data.data,ofs):IVAL(tdr->data.data,ofs)) +#define TDR_SCVAL(tdr, ofs, v) SCVAL(tdr->data.data,ofs,v) +#define TDR_SSVAL(tdr, ofs, v) do { if (TDR_BE(tdr)) { RSSVAL(tdr->data.data,ofs,v); } else SSVAL(tdr->data.data,ofs,v); } while (0) +#define TDR_SIVAL(tdr, ofs, v) do { if (TDR_BE(tdr)) { RSIVAL(tdr->data.data,ofs,v); } else SIVAL(tdr->data.data,ofs,v); } while (0) + +/** + expand the available space in the buffer to 'size' +*/ +NTSTATUS tdr_push_expand(struct tdr_push *tdr, uint32_t size) +{ + if (talloc_get_size(tdr->data.data) >= size) { + return NT_STATUS_OK; + } + + tdr->data.data = talloc_realloc(tdr, tdr->data.data, uint8_t, tdr->data.length + TDR_BASE_MARSHALL_SIZE); + + if (tdr->data.data == NULL) + return NT_STATUS_NO_MEMORY; + + return NT_STATUS_OK; +} + + +NTSTATUS tdr_pull_uint8(struct tdr_pull *tdr, TALLOC_CTX *ctx, uint8_t *v) +{ + TDR_PULL_NEED_BYTES(tdr, 1); + *v = TDR_CVAL(tdr, tdr->offset); + tdr->offset += 1; + return NT_STATUS_OK; +} + +NTSTATUS tdr_push_uint8(struct tdr_push *tdr, const uint8_t *v) +{ + TDR_PUSH_NEED_BYTES(tdr, 1); + TDR_SCVAL(tdr, tdr->data.length, *v); + tdr->data.length += 1; + return NT_STATUS_OK; +} + +NTSTATUS tdr_print_uint8(struct tdr_print *tdr, const char *name, uint8_t *v) +{ + tdr->print(tdr, "%-25s: 0x%02x (%u)", name, *v, *v); + return NT_STATUS_OK; +} + +NTSTATUS tdr_pull_uint16(struct tdr_pull *tdr, TALLOC_CTX *ctx, uint16_t *v) +{ + TDR_PULL_NEED_BYTES(tdr, 2); + *v = TDR_SVAL(tdr, tdr->offset); + tdr->offset += 2; + return NT_STATUS_OK; +} + +NTSTATUS tdr_push_uint16(struct tdr_push *tdr, const uint16_t *v) +{ + TDR_PUSH_NEED_BYTES(tdr, 2); + TDR_SSVAL(tdr, tdr->data.length, *v); + tdr->data.length += 2; + return NT_STATUS_OK; +} + +NTSTATUS tdr_print_uint16(struct tdr_print *tdr, const char *name, uint16_t *v) +{ + tdr->print(tdr, "%-25s: 0x%02x (%u)", name, *v, *v); + return NT_STATUS_OK; +} + +NTSTATUS tdr_pull_uint32(struct tdr_pull *tdr, TALLOC_CTX *ctx, uint32_t *v) +{ + TDR_PULL_NEED_BYTES(tdr, 4); + *v = TDR_IVAL(tdr, tdr->offset); + tdr->offset += 4; + return NT_STATUS_OK; +} + +NTSTATUS tdr_push_uint32(struct tdr_push *tdr, const uint32_t *v) +{ + TDR_PUSH_NEED_BYTES(tdr, 4); + TDR_SIVAL(tdr, tdr->data.length, *v); + tdr->data.length += 4; + return NT_STATUS_OK; +} + +NTSTATUS tdr_print_uint32(struct tdr_print *tdr, const char *name, uint32_t *v) +{ + tdr->print(tdr, "%-25s: 0x%02x (%u)", name, *v, *v); + return NT_STATUS_OK; +} + +NTSTATUS tdr_pull_charset(struct tdr_pull *tdr, TALLOC_CTX *ctx, const char **v, uint32_t length, uint32_t el_size, charset_t chset) +{ + size_t ret; + + if (length == -1) { + switch (chset) { + case CH_DOS: + length = ascii_len_n((const char*)tdr->data.data+tdr->offset, tdr->data.length-tdr->offset); + break; + case CH_UTF16: + length = utf16_len_n(tdr->data.data+tdr->offset, tdr->data.length-tdr->offset); + break; + + default: + return NT_STATUS_INVALID_PARAMETER; + } + } + + if (length == 0) { + *v = talloc_strdup(ctx, ""); + return NT_STATUS_OK; + } + + TDR_PULL_NEED_BYTES(tdr, el_size*length); + + if (!convert_string_talloc_convenience(ctx, tdr->iconv_convenience, chset, CH_UNIX, tdr->data.data+tdr->offset, el_size*length, discard_const_p(void *, v), &ret, false)) { + return NT_STATUS_INVALID_PARAMETER; + } + + tdr->offset += length * el_size; + + return NT_STATUS_OK; +} + +NTSTATUS tdr_push_charset(struct tdr_push *tdr, const char **v, uint32_t length, uint32_t el_size, charset_t chset) +{ + size_t ret, required; + + if (length == -1) { + length = strlen(*v) + 1; /* Extra element for null character */ + } + + required = el_size * length; + TDR_PUSH_NEED_BYTES(tdr, required); + + if (!convert_string_convenience(tdr->iconv_convenience, CH_UNIX, chset, *v, strlen(*v), tdr->data.data+tdr->data.length, required, &ret, false)) { + return NT_STATUS_INVALID_PARAMETER; + } + + /* Make sure the remaining part of the string is filled with zeroes */ + if (ret < required) { + memset(tdr->data.data+tdr->data.length+ret, 0, required-ret); + } + + tdr->data.length += required; + + return NT_STATUS_OK; +} + +NTSTATUS tdr_print_charset(struct tdr_print *tdr, const char *name, const char **v, uint32_t length, uint32_t el_size, charset_t chset) +{ + tdr->print(tdr, "%-25s: %s", name, *v); + return NT_STATUS_OK; +} + +/** + parse a hyper +*/ +NTSTATUS tdr_pull_hyper(struct tdr_pull *tdr, TALLOC_CTX *ctx, uint64_t *v) +{ + TDR_PULL_NEED_BYTES(tdr, 8); + *v = TDR_IVAL(tdr, tdr->offset); + *v |= (uint64_t)(TDR_IVAL(tdr, tdr->offset+4)) << 32; + tdr->offset += 8; + return NT_STATUS_OK; +} + +/** + push a hyper +*/ +NTSTATUS tdr_push_hyper(struct tdr_push *tdr, uint64_t *v) +{ + TDR_PUSH_NEED_BYTES(tdr, 8); + TDR_SIVAL(tdr, tdr->data.length, ((*v) & 0xFFFFFFFF)); + TDR_SIVAL(tdr, tdr->data.length+4, ((*v)>>32)); + tdr->data.length += 8; + return NT_STATUS_OK; +} + +/** + push a NTTIME +*/ +NTSTATUS tdr_push_NTTIME(struct tdr_push *tdr, NTTIME *t) +{ + TDR_CHECK(tdr_push_hyper(tdr, t)); + return NT_STATUS_OK; +} + +/** + pull a NTTIME +*/ +NTSTATUS tdr_pull_NTTIME(struct tdr_pull *tdr, TALLOC_CTX *ctx, NTTIME *t) +{ + TDR_CHECK(tdr_pull_hyper(tdr, ctx, t)); + return NT_STATUS_OK; +} + +/** + push a time_t +*/ +NTSTATUS tdr_push_time_t(struct tdr_push *tdr, time_t *t) +{ + return tdr_push_uint32(tdr, (uint32_t *)t); +} + +/** + pull a time_t +*/ +NTSTATUS tdr_pull_time_t(struct tdr_pull *tdr, TALLOC_CTX *ctx, time_t *t) +{ + uint32_t tt; + TDR_CHECK(tdr_pull_uint32(tdr, ctx, &tt)); + *t = tt; + return NT_STATUS_OK; +} + +NTSTATUS tdr_print_time_t(struct tdr_print *tdr, const char *name, time_t *t) +{ + if (*t == (time_t)-1 || *t == 0) { + tdr->print(tdr, "%-25s: (time_t)%d", name, (int)*t); + } else { + tdr->print(tdr, "%-25s: %s", name, timestring(tdr, *t)); + } + + return NT_STATUS_OK; +} + +NTSTATUS tdr_print_NTTIME(struct tdr_print *tdr, const char *name, NTTIME *t) +{ + tdr->print(tdr, "%-25s: %s", name, nt_time_string(tdr, *t)); + + return NT_STATUS_OK; +} + +NTSTATUS tdr_print_DATA_BLOB(struct tdr_print *tdr, const char *name, DATA_BLOB *r) +{ + tdr->print(tdr, "%-25s: DATA_BLOB length=%u", name, r->length); + if (r->length) { + dump_data(10, r->data, r->length); + } + + return NT_STATUS_OK; +} + +#define TDR_ALIGN(l,n) (((l) & ((n)-1)) == 0?0:((n)-((l)&((n)-1)))) + +/* + push a DATA_BLOB onto the wire. +*/ +NTSTATUS tdr_push_DATA_BLOB(struct tdr_push *tdr, DATA_BLOB *blob) +{ + if (tdr->flags & TDR_ALIGN2) { + blob->length = TDR_ALIGN(tdr->data.length, 2); + } else if (tdr->flags & TDR_ALIGN4) { + blob->length = TDR_ALIGN(tdr->data.length, 4); + } else if (tdr->flags & TDR_ALIGN8) { + blob->length = TDR_ALIGN(tdr->data.length, 8); + } + + TDR_PUSH_NEED_BYTES(tdr, blob->length); + + memcpy(tdr->data.data+tdr->data.length, blob->data, blob->length); + return NT_STATUS_OK; +} + +/* + pull a DATA_BLOB from the wire. +*/ +NTSTATUS tdr_pull_DATA_BLOB(struct tdr_pull *tdr, TALLOC_CTX *ctx, DATA_BLOB *blob) +{ + uint32_t length; + + if (tdr->flags & TDR_ALIGN2) { + length = TDR_ALIGN(tdr->offset, 2); + } else if (tdr->flags & TDR_ALIGN4) { + length = TDR_ALIGN(tdr->offset, 4); + } else if (tdr->flags & TDR_ALIGN8) { + length = TDR_ALIGN(tdr->offset, 8); + } else if (tdr->flags & TDR_REMAINING) { + length = tdr->data.length - tdr->offset; + } else { + return NT_STATUS_INVALID_PARAMETER; + } + + if (tdr->data.length - tdr->offset < length) { + length = tdr->data.length - tdr->offset; + } + + TDR_PULL_NEED_BYTES(tdr, length); + + *blob = data_blob_talloc(tdr, tdr->data.data+tdr->offset, length); + tdr->offset += length; + return NT_STATUS_OK; +} + +struct tdr_push *tdr_push_init(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *ic) +{ + struct tdr_push *push = talloc_zero(mem_ctx, struct tdr_push); + + if (push == NULL) + return NULL; + + push->iconv_convenience = talloc_reference(push, ic); + + return push; +} + +struct tdr_pull *tdr_pull_init(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *ic) +{ + struct tdr_pull *pull = talloc_zero(mem_ctx, struct tdr_pull); + + if (pull == NULL) + return NULL; + + pull->iconv_convenience = talloc_reference(pull, ic); + + return pull; +} + +NTSTATUS tdr_push_to_fd(int fd, struct smb_iconv_convenience *iconv_convenience, tdr_push_fn_t push_fn, const void *p) +{ + struct tdr_push *push = tdr_push_init(NULL, iconv_convenience); + + if (push == NULL) + return NT_STATUS_NO_MEMORY; + + if (NT_STATUS_IS_ERR(push_fn(push, p))) { + DEBUG(1, ("Error pushing data\n")); + talloc_free(push); + return NT_STATUS_UNSUCCESSFUL; + } + + if (write(fd, push->data.data, push->data.length) < push->data.length) { + DEBUG(1, ("Error writing all data\n")); + return NT_STATUS_UNSUCCESSFUL; + } + + talloc_free(push); + + return NT_STATUS_OK; +} + +void tdr_print_debug_helper(struct tdr_print *tdr, const char *format, ...) _PRINTF_ATTRIBUTE(2,3) +{ + va_list ap; + char *s = NULL; + int i; + + va_start(ap, format); + vasprintf(&s, format, ap); + va_end(ap); + + for (i=0;i<tdr->level;i++) { DEBUG(0,(" ")); } + + DEBUG(0,("%s\n", s)); + free(s); +} diff --git a/lib/tdr/tdr.h b/lib/tdr/tdr.h new file mode 100644 index 0000000000..c983cd35c1 --- /dev/null +++ b/lib/tdr/tdr.h @@ -0,0 +1,67 @@ +/* + Unix SMB/CIFS implementation. + TDR definitions + Copyright (C) Jelmer Vernooij 2005 + + 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 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __TDR_H__ +#define __TDR_H__ + +#include <talloc.h> +#include "../lib/util/charset/charset.h" + +#define TDR_BIG_ENDIAN 0x01 +#define TDR_ALIGN2 0x02 +#define TDR_ALIGN4 0x04 +#define TDR_ALIGN8 0x08 +#define TDR_REMAINING 0x10 + +struct tdr_pull { + DATA_BLOB data; + uint32_t offset; + int flags; + struct smb_iconv_convenience *iconv_convenience; +}; + +struct tdr_push { + DATA_BLOB data; + int flags; + struct smb_iconv_convenience *iconv_convenience; +}; + +struct tdr_print { + int level; + void (*print)(struct tdr_print *, const char *, ...); + int flags; +}; + +#define TDR_CHECK(call) do { NTSTATUS _status; \ + _status = call; \ + if (!NT_STATUS_IS_OK(_status)) \ + return _status; \ + } while (0) + +#define TDR_ALLOC(ctx, s, n) do { \ + (s) = talloc_array_size(ctx, sizeof(*(s)), n); \ + if ((n) && !(s)) return NT_STATUS_NO_MEMORY; \ + } while (0) + +typedef NTSTATUS (*tdr_push_fn_t) (struct tdr_push *, const void *); +typedef NTSTATUS (*tdr_pull_fn_t) (struct tdr_pull *, TALLOC_CTX *, void *); + +#include "lib/tdr/tdr_proto.h" + +#endif /* __TDR_H__ */ diff --git a/lib/tdr/testsuite.c b/lib/tdr/testsuite.c new file mode 100644 index 0000000000..36bb164a9a --- /dev/null +++ b/lib/tdr/testsuite.c @@ -0,0 +1,185 @@ +/* + Unix SMB/CIFS implementation. + test suite for basic tdr functions + + Copyright (C) Jelmer Vernooij 2007 + + 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 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "torture/torture.h" +#include "lib/tdr/tdr.h" + +static bool test_push_uint8(struct torture_context *tctx) +{ + uint8_t v = 4; + struct tdr_push *tdr = tdr_push_init(tctx, global_iconv_convenience); + + torture_assert_ntstatus_ok(tctx, tdr_push_uint8(tdr, &v), "push failed"); + torture_assert_int_equal(tctx, tdr->data.length, 1, "length incorrect"); + torture_assert_int_equal(tctx, tdr->data.data[0], 4, "data incorrect"); + return true; +} + +static bool test_pull_uint8(struct torture_context *tctx) +{ + uint8_t d = 2; + uint8_t l; + struct tdr_pull *tdr = tdr_pull_init(tctx, global_iconv_convenience); + tdr->data.data = &d; + tdr->data.length = 1; + tdr->offset = 0; + tdr->flags = 0; + torture_assert_ntstatus_ok(tctx, tdr_pull_uint8(tdr, tctx, &l), + "pull failed"); + torture_assert_int_equal(tctx, 1, tdr->offset, + "offset invalid"); + return true; +} + +static bool test_push_uint16(struct torture_context *tctx) +{ + uint16_t v = 0xF32; + struct tdr_push *tdr = tdr_push_init(tctx, global_iconv_convenience); + + torture_assert_ntstatus_ok(tctx, tdr_push_uint16(tdr, &v), "push failed"); + torture_assert_int_equal(tctx, tdr->data.length, 2, "length incorrect"); + torture_assert_int_equal(tctx, tdr->data.data[0], 0x32, "data incorrect"); + torture_assert_int_equal(tctx, tdr->data.data[1], 0x0F, "data incorrect"); + return true; +} + +static bool test_pull_uint16(struct torture_context *tctx) +{ + uint8_t d[2] = { 782 & 0xFF, (782 & 0xFF00) / 0x100 }; + uint16_t l; + struct tdr_pull *tdr = tdr_pull_init(tctx, global_iconv_convenience); + tdr->data.data = d; + tdr->data.length = 2; + tdr->offset = 0; + tdr->flags = 0; + torture_assert_ntstatus_ok(tctx, tdr_pull_uint16(tdr, tctx, &l), + "pull failed"); + torture_assert_int_equal(tctx, 2, tdr->offset, "offset invalid"); + torture_assert_int_equal(tctx, 782, l, "right int read"); + return true; +} + +static bool test_push_uint32(struct torture_context *tctx) +{ + uint32_t v = 0x100F32; + struct tdr_push *tdr = tdr_push_init(tctx, global_iconv_convenience); + + torture_assert_ntstatus_ok(tctx, tdr_push_uint32(tdr, &v), "push failed"); + torture_assert_int_equal(tctx, tdr->data.length, 4, "length incorrect"); + torture_assert_int_equal(tctx, tdr->data.data[0], 0x32, "data incorrect"); + torture_assert_int_equal(tctx, tdr->data.data[1], 0x0F, "data incorrect"); + torture_assert_int_equal(tctx, tdr->data.data[2], 0x10, "data incorrect"); + torture_assert_int_equal(tctx, tdr->data.data[3], 0x00, "data incorrect"); + return true; +} + +static bool test_pull_uint32(struct torture_context *tctx) +{ + uint8_t d[4] = { 782 & 0xFF, (782 & 0xFF00) / 0x100, 0, 0 }; + uint32_t l; + struct tdr_pull *tdr = tdr_pull_init(tctx, global_iconv_convenience); + tdr->data.data = d; + tdr->data.length = 4; + tdr->offset = 0; + tdr->flags = 0; + torture_assert_ntstatus_ok(tctx, tdr_pull_uint32(tdr, tctx, &l), + "pull failed"); + torture_assert_int_equal(tctx, 4, tdr->offset, "offset invalid"); + torture_assert_int_equal(tctx, 782, l, "right int read"); + return true; +} + +static bool test_pull_charset(struct torture_context *tctx) +{ + struct tdr_pull *tdr = tdr_pull_init(tctx, global_iconv_convenience); + const char *l = NULL; + tdr->data.data = (uint8_t *)talloc_strdup(tctx, "bla"); + tdr->data.length = 4; + tdr->offset = 0; + tdr->flags = 0; + torture_assert_ntstatus_ok(tctx, tdr_pull_charset(tdr, tctx, &l, -1, 1, CH_DOS), + "pull failed"); + torture_assert_int_equal(tctx, 4, tdr->offset, "offset invalid"); + torture_assert_str_equal(tctx, "bla", l, "right int read"); + + tdr->offset = 0; + torture_assert_ntstatus_ok(tctx, tdr_pull_charset(tdr, tctx, &l, 2, 1, CH_UNIX), + "pull failed"); + torture_assert_int_equal(tctx, 2, tdr->offset, "offset invalid"); + torture_assert_str_equal(tctx, "bl", l, "right int read"); + + return true; +} + +static bool test_pull_charset_empty(struct torture_context *tctx) +{ + struct tdr_pull *tdr = tdr_pull_init(tctx, global_iconv_convenience); + const char *l = NULL; + tdr->data.data = (uint8_t *)talloc_strdup(tctx, "bla"); + tdr->data.length = 4; + tdr->offset = 0; + tdr->flags = 0; + torture_assert_ntstatus_ok(tctx, tdr_pull_charset(tdr, tctx, &l, 0, 1, CH_DOS), + "pull failed"); + torture_assert_int_equal(tctx, 0, tdr->offset, "offset invalid"); + torture_assert_str_equal(tctx, "", l, "right string read"); + + return true; +} + + + +static bool test_push_charset(struct torture_context *tctx) +{ + const char *l = "bloe"; + struct tdr_push *tdr = tdr_push_init(tctx, global_iconv_convenience); + torture_assert_ntstatus_ok(tctx, tdr_push_charset(tdr, &l, 4, 1, CH_UTF8), + "push failed"); + torture_assert_int_equal(tctx, 4, tdr->data.length, "offset invalid"); + torture_assert(tctx, strcmp("bloe", (const char *)tdr->data.data) == 0, "right string push"); + + torture_assert_ntstatus_ok(tctx, tdr_push_charset(tdr, &l, -1, 1, CH_UTF8), + "push failed"); + torture_assert_int_equal(tctx, 9, tdr->data.length, "offset invalid"); + torture_assert_str_equal(tctx, "bloe", (const char *)tdr->data.data+4, "right string read"); + + return true; +} + +struct torture_suite *torture_local_tdr(TALLOC_CTX *mem_ctx) +{ + struct torture_suite *suite = torture_suite_create(mem_ctx, "TDR"); + + torture_suite_add_simple_test(suite, "pull_uint8", test_pull_uint8); + torture_suite_add_simple_test(suite, "push_uint8", test_push_uint8); + + torture_suite_add_simple_test(suite, "pull_uint16", test_pull_uint16); + torture_suite_add_simple_test(suite, "push_uint16", test_push_uint16); + + torture_suite_add_simple_test(suite, "pull_uint32", test_pull_uint32); + torture_suite_add_simple_test(suite, "push_uint32", test_push_uint32); + + torture_suite_add_simple_test(suite, "pull_charset", test_pull_charset); + torture_suite_add_simple_test(suite, "pull_charset", test_pull_charset_empty); + torture_suite_add_simple_test(suite, "push_charset", test_push_charset); + + return suite; +} diff --git a/lib/tevent/configure.ac b/lib/tevent/configure.ac index 4333461110..69f65c6df7 100644 --- a/lib/tevent/configure.ac +++ b/lib/tevent/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ(2.50) -AC_INIT(tevent, 0.9.3) +AC_INIT(tevent, 0.9.4) AC_CONFIG_SRCDIR([tevent.c]) AC_CONFIG_HEADER(config.h) diff --git a/lib/tevent/tevent.c b/lib/tevent/tevent.c index fc8252960a..867cfc08fe 100644 --- a/lib/tevent/tevent.c +++ b/lib/tevent/tevent.c @@ -59,6 +59,7 @@ */ #include "replace.h" #include "system/filesys.h" +#define TEVENT_DEPRECATED 1 #include "tevent.h" #include "tevent_internal.h" #include "tevent_util.h" @@ -305,6 +306,33 @@ void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags) fde->event_ctx->ops->set_fd_flags(fde, flags); } +bool tevent_signal_support(struct tevent_context *ev) +{ + if (ev->ops->add_signal) { + return true; + } + return false; +} + +static void (*tevent_abort_fn)(const char *reason); + +void tevent_set_abort_fn(void (*abort_fn)(const char *reason)) +{ + tevent_abort_fn = abort_fn; +} + +static void tevent_abort(struct tevent_context *ev, const char *reason) +{ + tevent_debug(ev, TEVENT_DEBUG_FATAL, + "abort: %s\n", reason); + + if (!tevent_abort_fn) { + abort(); + } + + tevent_abort_fn(reason); +} + /* add a timer event return NULL on failure @@ -341,18 +369,152 @@ struct tevent_signal *_tevent_add_signal(struct tevent_context *ev, handler_name, location); } +void tevent_loop_allow_nesting(struct tevent_context *ev) +{ + ev->nesting.allowed = true; +} + +void tevent_loop_set_nesting_hook(struct tevent_context *ev, + tevent_nesting_hook hook, + void *private_data) +{ + ev->nesting.hook_fn = hook; + ev->nesting.hook_private = private_data; +} + +static void tevent_abort_nesting(struct tevent_context *ev, const char *location) +{ + const char *reason; + + reason = talloc_asprintf(NULL, "tevent_loop_once() nesting at %s", + location); + if (!reason) { + reason = "tevent_loop_once() nesting"; + } + + tevent_abort(ev, reason); +} + /* do a single event loop using the events defined in ev */ -int tevent_loop_once(struct tevent_context *ev) +int _tevent_loop_once(struct tevent_context *ev, const char *location) { - return ev->ops->loop_once(ev); + int ret; + void *nesting_stack_ptr = NULL; + + ev->nesting.level++; + + if (ev->nesting.level > 1) { + if (!ev->nesting.allowed) { + tevent_abort_nesting(ev, location); + errno = ELOOP; + return -1; + } + if (ev->nesting.hook_fn) { + int ret2; + ret2 = ev->nesting.hook_fn(ev, + ev->nesting.hook_private, + ev->nesting.level, + true, + (void *)&nesting_stack_ptr, + location); + if (ret2 != 0) { + ret = ret2; + goto done; + } + } + } + + ret = ev->ops->loop_once(ev, location); + + if (ev->nesting.level > 1) { + if (ev->nesting.hook_fn) { + int ret2; + ret2 = ev->nesting.hook_fn(ev, + ev->nesting.hook_private, + ev->nesting.level, + false, + (void *)&nesting_stack_ptr, + location); + if (ret2 != 0) { + ret = ret2; + goto done; + } + } + } + +done: + ev->nesting.level--; + return ret; +} + +/* + this is a performance optimization for the samba4 nested event loop problems +*/ +int _tevent_loop_until(struct tevent_context *ev, + bool (*finished)(void *private_data), + void *private_data, + const char *location) +{ + int ret = 0; + void *nesting_stack_ptr = NULL; + + ev->nesting.level++; + + if (ev->nesting.level > 1) { + if (!ev->nesting.allowed) { + tevent_abort_nesting(ev, location); + errno = ELOOP; + return -1; + } + if (ev->nesting.hook_fn) { + int ret2; + ret2 = ev->nesting.hook_fn(ev, + ev->nesting.hook_private, + ev->nesting.level, + true, + (void *)&nesting_stack_ptr, + location); + if (ret2 != 0) { + ret = ret2; + goto done; + } + } + } + + while (!finished(private_data)) { + ret = ev->ops->loop_once(ev, location); + if (ret != 0) { + break; + } + } + + if (ev->nesting.level > 1) { + if (ev->nesting.hook_fn) { + int ret2; + ret2 = ev->nesting.hook_fn(ev, + ev->nesting.hook_private, + ev->nesting.level, + false, + (void *)&nesting_stack_ptr, + location); + if (ret2 != 0) { + ret = ret2; + goto done; + } + } + } + +done: + ev->nesting.level--; + return ret; } /* return on failure or (with 0) if all fd events are removed */ -int tevent_loop_wait(struct tevent_context *ev) +int _tevent_loop_wait(struct tevent_context *ev, const char *location) { - return ev->ops->loop_wait(ev); + return ev->ops->loop_wait(ev, location); } diff --git a/lib/tevent/tevent.h b/lib/tevent/tevent.h index 2dadfc1abe..4a3f51ae5e 100644 --- a/lib/tevent/tevent.h +++ b/lib/tevent/tevent.h @@ -99,8 +99,13 @@ struct tevent_signal *_tevent_add_signal(struct tevent_context *ev, _tevent_add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data, \ #handler, __location__) -int tevent_loop_once(struct tevent_context *ev); -int tevent_loop_wait(struct tevent_context *ev); +int _tevent_loop_once(struct tevent_context *ev, const char *location); +#define tevent_loop_once(ev) \ + _tevent_loop_once(ev, __location__) \ + +int _tevent_loop_wait(struct tevent_context *ev, const char *location); +#define tevent_loop_wait(ev) \ + _tevent_loop_wait(ev, __location__) \ void tevent_fd_set_close_fn(struct tevent_fd *fde, tevent_fd_close_fn_t close_fn); @@ -108,6 +113,10 @@ void tevent_fd_set_auto_close(struct tevent_fd *fde); uint16_t tevent_fd_get_flags(struct tevent_fd *fde); void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags); +bool tevent_signal_support(struct tevent_context *ev); + +void tevent_set_abort_fn(void (*abort_fn)(const char *reason)); + /* bits for file descriptor event flags */ #define TEVENT_FD_READ 1 #define TEVENT_FD_WRITE 2 @@ -292,6 +301,32 @@ void tevent_queue_stop(struct tevent_queue *queue); size_t tevent_queue_length(struct tevent_queue *queue); +typedef int (*tevent_nesting_hook)(struct tevent_context *ev, + void *private_data, + uint32_t level, + bool begin, + void *stack_ptr, + const char *location); +#ifdef TEVENT_DEPRECATED +#ifndef _DEPRECATED_ +#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 ) +#define _DEPRECATED_ __attribute__ ((deprecated)) +#else +#define _DEPRECATED_ +#endif +#endif +void tevent_loop_allow_nesting(struct tevent_context *ev) _DEPRECATED_; +void tevent_loop_set_nesting_hook(struct tevent_context *ev, + tevent_nesting_hook hook, + void *private_data) _DEPRECATED_; +int _tevent_loop_until(struct tevent_context *ev, + bool (*finished)(void *private_data), + void *private_data, + const char *location) _DEPRECATED_; +#define tevent_loop_until(ev, finished, private_data) \ + _tevent_loop_until(ev, finished, private_data, __location__) +#endif + #ifdef TEVENT_COMPAT_DEFINES #define event_context tevent_context diff --git a/lib/tevent/tevent_epoll.c b/lib/tevent/tevent_epoll.c index 0494f55060..b63d299d94 100644 --- a/lib/tevent/tevent_epoll.c +++ b/lib/tevent/tevent_epoll.c @@ -411,7 +411,7 @@ static void epoll_event_set_fd_flags(struct tevent_fd *fde, uint16_t flags) /* do a single event loop using the events defined in ev */ -static int epoll_event_loop_once(struct tevent_context *ev) +static int epoll_event_loop_once(struct tevent_context *ev, const char *location) { struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data, struct epoll_event_context); @@ -430,12 +430,12 @@ static int epoll_event_loop_once(struct tevent_context *ev) /* return on failure or (with 0) if all fd events are removed */ -static int epoll_event_loop_wait(struct tevent_context *ev) +static int epoll_event_loop_wait(struct tevent_context *ev, const char *location) { struct epoll_event_context *epoll_ev = talloc_get_type(ev->additional_data, struct epoll_event_context); while (epoll_ev->ev->fd_events) { - if (epoll_event_loop_once(ev) != 0) { + if (epoll_event_loop_once(ev, location) != 0) { break; } } diff --git a/lib/tevent/tevent_internal.h b/lib/tevent/tevent_internal.h index 5a645ecb60..f10485398f 100644 --- a/lib/tevent/tevent_internal.h +++ b/lib/tevent/tevent_internal.h @@ -153,8 +153,8 @@ struct tevent_ops { const char *location); /* loop functions */ - int (*loop_once)(struct tevent_context *ev); - int (*loop_wait)(struct tevent_context *ev); + int (*loop_once)(struct tevent_context *ev, const char *location); + int (*loop_wait)(struct tevent_context *ev, const char *location); }; struct tevent_fd { @@ -233,6 +233,14 @@ struct tevent_context { /* debugging operations */ struct tevent_debug_ops debug_ops; + + /* info about the nesting status */ + struct { + bool allowed; + uint32_t level; + tevent_nesting_hook hook_fn; + void *hook_private; + } nesting; }; diff --git a/lib/tevent/tevent_select.c b/lib/tevent/tevent_select.c index 32678f0a15..cdddb601c4 100644 --- a/lib/tevent/tevent_select.c +++ b/lib/tevent/tevent_select.c @@ -213,7 +213,7 @@ static int select_event_loop_select(struct select_event_context *select_ev, stru /* do a single event loop using the events defined in ev */ -static int select_event_loop_once(struct tevent_context *ev) +static int select_event_loop_once(struct tevent_context *ev, const char *location) { struct select_event_context *select_ev = talloc_get_type(ev->additional_data, struct select_event_context); @@ -230,14 +230,14 @@ static int select_event_loop_once(struct tevent_context *ev) /* return on failure or (with 0) if all fd events are removed */ -static int select_event_loop_wait(struct tevent_context *ev) +static int select_event_loop_wait(struct tevent_context *ev, const char *location) { struct select_event_context *select_ev = talloc_get_type(ev->additional_data, struct select_event_context); select_ev->exit_code = 0; while (ev->fd_events && select_ev->exit_code == 0) { - if (select_event_loop_once(ev) != 0) { + if (select_event_loop_once(ev, location) != 0) { break; } } diff --git a/lib/tevent/tevent_standard.c b/lib/tevent/tevent_standard.c index bbd5c5d785..73a45e8c20 100644 --- a/lib/tevent/tevent_standard.c +++ b/lib/tevent/tevent_standard.c @@ -534,7 +534,7 @@ static int std_event_loop_select(struct std_event_context *std_ev, struct timeva /* do a single event loop using the events defined in ev */ -static int std_event_loop_once(struct tevent_context *ev) +static int std_event_loop_once(struct tevent_context *ev, const char *location) { struct std_event_context *std_ev = talloc_get_type(ev->additional_data, struct std_event_context); @@ -557,14 +557,14 @@ static int std_event_loop_once(struct tevent_context *ev) /* return on failure or (with 0) if all fd events are removed */ -static int std_event_loop_wait(struct tevent_context *ev) +static int std_event_loop_wait(struct tevent_context *ev, const char *location) { struct std_event_context *std_ev = talloc_get_type(ev->additional_data, struct std_event_context); std_ev->exit_code = 0; while (ev->fd_events && std_ev->exit_code == 0) { - if (std_event_loop_once(ev) != 0) { + if (std_event_loop_once(ev, location) != 0) { break; } } diff --git a/lib/util/fault.m4 b/lib/util/fault.m4 index da077af31d..bac553a158 100644 --- a/lib/util/fault.m4 +++ b/lib/util/fault.m4 @@ -8,6 +8,7 @@ if test x"$ac_cv_header_execinfo_h" = x"yes" -a x"$ac_cv_func_ext_backtrace" = x EXECINFO_CFLAGS="$CFLAGS" EXECINFO_CPPFLAGS="$CPPFLAGS" EXECINFO_LDFLAGS="$LDFLAGS" + LIB_REMOVE_USR_LIB(EXECINFO_LDFLAGS) else SMB_ENABLE(EXECINFO,NO) fi |