Debian Codegraph @

c/src/platform/platform.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "platform.h"
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #ifdef PN_WINAPI
29 #include <windows.h>
30 int pn_i_getpid() {
31 return (int) GetCurrentProcessId();
32 }
33 #else
34 #include <unistd.h>
35 int pn_i_getpid() {
36 return (int) getpid();
37 }
38 #endif
39
40 void pni_vfatal(const char *fmt, va_list ap)
41 {
42 vfprintf(stderr, fmt, ap);
43 abort();
44 }
45
46 void pni_fatal(const char *fmt, ...)
47 {
48 va_list ap;
49 va_start(ap, fmt);
50 pni_vfatal(fmt, ap);
51 va_end(ap);
52 }
53
54 /* Allow for systems that do not implement clock_gettime()*/
55 #ifdef USE_CLOCK_GETTIME
56 #include <time.h>
57 pn_timestamp_t pn_i_now(void)
58 {
59 struct timespec now;
60 if (clock_gettime(CLOCK_REALTIME, &now)) pni_fatal("clock_gettime() failed\n");
61 return ((pn_timestamp_t)now.tv_sec) * 1000 + (now.tv_nsec / 1000000);
62 }
63 #elif defined(USE_WIN_FILETIME)
64 #include <windows.h>
65 pn_timestamp_t pn_i_now(void)
66 {
67 FILETIME now;
68 GetSystemTimeAsFileTime(&now);
69 ULARGE_INTEGER t;
70 t.u.HighPart = now.dwHighDateTime;
71 t.u.LowPart = now.dwLowDateTime;
72 // Convert to milliseconds and adjust base epoch
73 return t.QuadPart / 10000 - 11644473600000;
74 }
75 #else
76 #include <sys/time.h>
77 pn_timestamp_t pn_i_now(void)
78 {
79 struct timeval now;
80 if (gettimeofday(&now, NULL)) pni_fatal("gettimeofday failed\n");
81 return ((pn_timestamp_t)now.tv_sec) * 1000 + (now.tv_usec / 1000);
82 }
83 #endif
84
85 #include <string.h>
86 #include <stdio.h>
87 static void pn_i_strerror(int errnum, char *buf, size_t buflen)
88 {
89 // PROTON-1029 provide a simple default in case strerror fails
90 snprintf(buf, buflen, "errno: %d", errnum);
91 #ifdef USE_STRERROR_R
92 strerror_r(errnum, buf, buflen);
93 #elif USE_STRERROR_S
94 strerror_s(buf, buflen, errnum);
95 #elif USE_OLD_STRERROR
96 strncpy(buf, strerror(errnum), buflen);
97 #endif
98 }
99
100 int pn_i_error_from_errno(pn_error_t *error, const char *msg)
101 {
102 char err[1024];
103 pn_i_strerror(errno, err, 1024);
104 int code = PN_ERR;
105 if (errno == EINTR)
106 code = PN_INTR;
107 return pn_error_format(error, code, "%s: %s", msg, err);
108 }