From 6624805141bd1e97a70b032789f967751a7c3fc8 Mon Sep 17 00:00:00 2001 From: Colin McCabe Date: Wed, 7 Dec 2016 08:44:29 -0800 Subject: [PATCH] HTRACE-153. htrace-c should compile with -Pnative on Mac --- htrace-c/src/CMakeLists.txt | 5 ++-- htrace-c/src/core/htrace.hpp | 1 + htrace-c/src/receiver/hrpc.c | 12 +++------ htrace-c/src/receiver/htraced.c | 6 +++++ htrace-c/src/receiver/receiver.h | 6 ++--- htrace-c/src/test/cmp_util-unit.c | 2 +- htrace-c/src/test/mini_htraced.c | 8 +++++- htrace-c/src/test/time-unit.c | 4 +-- htrace-c/src/util/build.h.cmake | 2 ++ htrace-c/src/util/endian.h | 53 +++++++++++++++++++++++++++++++++++++++ htrace-c/src/util/net.h | 26 +++++++++++++++++++ htrace-c/src/util/tracer_id.c | 8 +++--- 12 files changed, 111 insertions(+), 22 deletions(-) create mode 100644 htrace-c/src/util/endian.h create mode 100644 htrace-c/src/util/net.h diff --git a/htrace-c/src/CMakeLists.txt b/htrace-c/src/CMakeLists.txt index 727880b..214602b 100644 --- a/htrace-c/src/CMakeLists.txt +++ b/htrace-c/src/CMakeLists.txt @@ -42,6 +42,7 @@ endif() INCLUDE(CheckCSourceCompiles) CHECK_C_SOURCE_COMPILES("int main(void) { static __thread int i = 0; return 0; }" HAVE_IMPROVED_TLS) +CHECK_C_SOURCE_COMPILES("#include \nint main(void) { return 0; }" HAVE_PR_SET_PDEATHSIG) CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/util/build.h.cmake ${CMAKE_BINARY_DIR}/util/build.h) get_filename_component(HTRACED_TOOL_ABSPATH "../../htrace-htraced/go/build/htracedTool" ABSOLUTE) @@ -51,9 +52,9 @@ CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/test/test_config.h.cmake ${CMAKE_BINARY_DIR}/ find_package(PkgConfig) pkg_check_modules(PC_JSON-C QUIET json-c) find_path(JSON_C_INCLUDE_DIR "json.h" - HINTS ${JSON_C_PREFIX} ${PC_JSON-C_INCLUDEDIR} ${PC_JSON-C_INCLUDE_DIRS} PATH_SUFFIXES json-c json) + HINTS /usr/local/include/json-c ${JSON_C_PREFIX} ${PC_JSON-C_INCLUDEDIR} ${PC_JSON-C_INCLUDE_DIRS} PATH_SUFFIXES json-c json) find_library(JSON_C_LIBRARY NAMES json-c json libjson - HINTS ${JSON_C_PREFIX}${PC_JSON-C_LIBDIR} ${PC_JSON-C_LIBRARY_DIRS}) + HINTS /usr/local/lib ${JSON_C_PREFIX}${PC_JSON-C_LIBDIR} ${PC_JSON-C_LIBRARY_DIRS}) IF(JSON_C_INCLUDE_DIR AND JSON_C_LIBRARY) ELSE(JSON_C_INCLUDE_DIR AND JSON_C_LIBRARY) MESSAGE(FATAL_ERROR "Failed to find libjson-c. Try installing libjson-c with apt-get or yum, or install it manually from http://oss.metaparadigm.com/json-c/") diff --git a/htrace-c/src/core/htrace.hpp b/htrace-c/src/core/htrace.hpp index 6f6a398..09990f3 100644 --- a/htrace-c/src/core/htrace.hpp +++ b/htrace-c/src/core/htrace.hpp @@ -21,6 +21,7 @@ #include "htrace.h" +#include #include /** diff --git a/htrace-c/src/receiver/hrpc.c b/htrace-c/src/receiver/hrpc.c index f2e296d..bdfe835 100644 --- a/htrace-c/src/receiver/hrpc.c +++ b/htrace-c/src/receiver/hrpc.c @@ -17,6 +17,7 @@ */ #include "receiver/hrpc.h" +#include "util/endian.h" #include "util/log.h" #include "util/string.h" #include "util/time.h" @@ -35,15 +36,8 @@ #include #include -#if defined(__OpenBSD__) -#include -#define be16toh(x) betoh16(x) -#define be32toh(x) betoh32(x) -#define be64toh(x) betoh64(x) -#elif defined(__NetBSD__) || defined(__FreeBSD__) -#include -#else -#include +#if defined(__APPLE__) +#include // needed for writev on MacOS X #endif /** diff --git a/htrace-c/src/receiver/htraced.c b/htrace-c/src/receiver/htraced.c index 3a2a091..b455939 100644 --- a/htrace-c/src/receiver/htraced.c +++ b/htrace-c/src/receiver/htraced.c @@ -38,6 +38,12 @@ #include #include +#if defined(__APPLE__) +// Get pthread_yield on MacOS X. +#include +#define pthread_yield sched_yield +#endif + /** * @file htraced.c * diff --git a/htrace-c/src/receiver/receiver.h b/htrace-c/src/receiver/receiver.h index 1e01486..80d6d9a 100644 --- a/htrace-c/src/receiver/receiver.h +++ b/htrace-c/src/receiver/receiver.h @@ -105,9 +105,9 @@ struct htrace_rcv *htrace_rcv_create(struct htracer *tracer, /* * HTrace span receiver types. */ -const struct htrace_rcv_ty g_noop_rcv_ty; -const struct htrace_rcv_ty g_local_file_rcv_ty; -const struct htrace_rcv_ty g_htraced_rcv_ty; +extern const struct htrace_rcv_ty g_noop_rcv_ty; +extern const struct htrace_rcv_ty g_local_file_rcv_ty; +extern const struct htrace_rcv_ty g_htraced_rcv_ty; #endif diff --git a/htrace-c/src/test/cmp_util-unit.c b/htrace-c/src/test/cmp_util-unit.c index d272cae..0c2ac41 100644 --- a/htrace-c/src/test/cmp_util-unit.c +++ b/htrace-c/src/test/cmp_util-unit.c @@ -96,7 +96,7 @@ static int test_serialize_spans(struct htrace_span **test_spans) cmp_bcopy_ctx_init(&bctx, buf, TEST_BUF_LENGTH); EXPECT_INT_ZERO(serialize_test_spans(test_spans, (cmp_ctx_t *)&bctx)); EXPECT_UINT64_EQ(cctx.count, bctx.off); - EXPECT_UINT64_EQ(TEST_BUF_LENGTH, bctx.len); + EXPECT_UINT64_EQ((uint64_t)TEST_BUF_LENGTH, bctx.len); bctx.off = 0; for (i = 0; i < NUM_TEST_SPANS; i++) { diff --git a/htrace-c/src/test/mini_htraced.c b/htrace-c/src/test/mini_htraced.c index ba3f7a9..eff1efa 100644 --- a/htrace-c/src/test/mini_htraced.c +++ b/htrace-c/src/test/mini_htraced.c @@ -22,6 +22,7 @@ #include "test/temp_dir.h" #include "test/test_config.h" #include "test/test.h" +#include "util/build.h" #include "util/log.h" #include @@ -34,13 +35,16 @@ #include #include #include -#include #include #include #include #include #include +#ifdef HAVE_PR_SET_PDEATHSIG +#include +#endif + /** * The maximum size of the notification data sent from the htraced daemon on * startup. @@ -421,8 +425,10 @@ pid_t mini_htraced_launch(const struct mini_htraced *ht, const char *path, // is to be done at all. unregister_tempdir_for_cleanup(ht->root_dir); +#if defined(HAVE_PR_SET_PDEATHSIG) // Make things nicer by exiting when the parent process exits. prctl(PR_SET_PDEATHSIG, SIGHUP); +#endif if (flags & MINI_HTRACED_LAUNCH_REDIRECT_FDS) { int null_fd; diff --git a/htrace-c/src/test/time-unit.c b/htrace-c/src/test/time-unit.c index acdd14f..f92fbf6 100644 --- a/htrace-c/src/test/time-unit.c +++ b/htrace-c/src/test/time-unit.c @@ -43,8 +43,8 @@ static int test_timespec_conversions(void) ts2.tv_sec = 0; ts2.tv_nsec = 0; ms_to_timespec(ms, &ts2); - EXPECT_UINT64_EQ(1LU, ts2.tv_sec); - EXPECT_UINT64_EQ((uint64_t)999000000ULL, ts2.tv_nsec); + EXPECT_UINT64_EQ((uint64_t)1LU, (uint64_t)ts2.tv_sec); + EXPECT_UINT64_EQ((uint64_t)999000000ULL, (uint64_t)ts2.tv_nsec); return EXIT_SUCCESS; } diff --git a/htrace-c/src/util/build.h.cmake b/htrace-c/src/util/build.h.cmake index ad04882..3fc64ff 100644 --- a/htrace-c/src/util/build.h.cmake +++ b/htrace-c/src/util/build.h.cmake @@ -20,4 +20,6 @@ #cmakedefine HAVE_IMPROVED_TLS +#cmakedefine HAVE_PR_SET_PDEATHSIG + #endif diff --git a/htrace-c/src/util/endian.h b/htrace-c/src/util/endian.h new file mode 100644 index 0000000..2d7f66a --- /dev/null +++ b/htrace-c/src/util/endian.h @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file util/endian.h + * + * Import the standard endian.h functions. + */ + +#ifndef APACHE_HTRACE_UTIL_ENDIAN_H +#define APACHE_HTRACE_UTIL_ENDIAN_H + +#if defined(__OpenBSD__) +#include +#define be16toh(x) betoh16(x) +#define be32toh(x) betoh32(x) +#define be64toh(x) betoh64(x) +#elif defined(__NetBSD__) || defined(__FreeBSD__) +#include +#elif defined(__APPLE__) +#include +#define be16toh(x) OSSwapBigToHostInt16(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htobe64(x) OSSwapHostToBigInt64(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#else +#include +#endif + +#endif diff --git a/htrace-c/src/util/net.h b/htrace-c/src/util/net.h new file mode 100644 index 0000000..f79667f --- /dev/null +++ b/htrace-c/src/util/net.h @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#if defined(__APPLE__) +// Fix IN6_IS_ADDR_LOOPBACK on Mac OS X +#define s6_addr16 __u6_addr.__u6_addr16 +#define s6_addr32 __u6_addr.__u6_addr32 +#endif diff --git a/htrace-c/src/util/tracer_id.c b/htrace-c/src/util/tracer_id.c index 9d4812d..71d5226 100644 --- a/htrace-c/src/util/tracer_id.c +++ b/htrace-c/src/util/tracer_id.c @@ -18,15 +18,14 @@ #include "util/log.h" #include "util/tracer_id.h" +#include "util/net.h" -#include #include #include #include #include #include #include -#include #include #include @@ -292,13 +291,14 @@ enum ip_addr_type get_ipv4_addr_type(const struct sockaddr_in *ip) enum ip_addr_type get_ipv6_addr_type(const struct sockaddr_in6 *ip) { +/* if (IN6_IS_ADDR_LOOPBACK(ip)) { return ADDR_TYPE_IPV6_LOOPBACK; } else if (IN6_IS_ADDR_SITELOCAL(ip)) { return ADDR_TYPE_IPV6_SITE_LOCAL; - } else { + } else { */ return ADDR_TYPE_IPV6_OTHER; - } + //} } // vim:ts=4:sw=4:et -- 2.9.3 (Apple Git-75)