1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 /*  Fluent Bit
4  *  ==========
5  *  Copyright (C) 2019-2021 The Fluent Bit Authors
6  *  Copyright (C) 2015-2018 Treasure Data Inc.
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  */
20 
21 /*
22  * This file contains compatibility functions and macros for various platforms.
23  *
24  * Including this header file should make platforms behave more consistently;
25  * Add more macros if you find any missing features.
26  */
27 
28 #ifndef FLB_COMPAT_H
29 #define FLB_COMPAT_H
30 
31 /*
32  * libmonkey exposes compat macros for <unistd.h>, which some platforms lack,
33  * so include the header here.
34  */
35 #include <monkey/mk_core.h>
36 
37 #ifdef FLB_SYSTEM_WINDOWS
38 #define WIN32_LEAN_AND_MEAN
39 #include <winsock2.h>
40 #include <windows.h>
41 #include <Wincrypt.h> /* flb_io_tls.c */
42 
43 #include <monkey/mk_core/mk_sleep.h>
44 #include <fluent-bit/flb_dlfcn_win32.h>
45 
46 #define FLB_DIRCHAR '\\'
47 #define PATH_MAX MAX_PATH
48 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
49 #define S_ISLNK(m) (0)  /* Windows doesn't support S_IFLNK */
50 #define SHUT_RD   SD_RECEIVE
51 #define SHUT_WR   SD_SEND
52 #define SHUT_RDWR SD_BOTH
53 
54 /* monkey exposes a broken vsnprintf macro. Undo it  */
55 #undef vsnprintf
56 
57 /*
58  * Windows prefer to add an underscore to each POSIX function.
59  * To suppress compiler warnings, we need these trivial macros.
60  */
61 #define timezone _timezone
62 #define tzname _tzname
63 #define strncasecmp _strnicmp
64 #define timegm _mkgmtime
65 
getpagesize(void)66 static inline int getpagesize(void)
67 {
68     SYSTEM_INFO info;
69     GetSystemInfo(&info);
70     return info.dwPageSize;
71 }
72 
gmtime_r(const time_t * timep,struct tm * result)73 static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
74 {
75     if (gmtime_s(result, timep)) {
76         return NULL;
77     }
78     return result;
79 }
80 
ctime_r(const time_t * timep,char * result)81 static inline char *ctime_r(const time_t *timep, char *result)
82 {
83     char *tmp = ctime(timep);
84     if (tmp == NULL) {
85         return NULL;
86     }
87     return strcpy(result, tmp);
88 }
89 
90 /*
91  * We can't just define localtime_r here, since mk_core/mk_utils.c is
92  * exposing a symbol with the same name inadvertently.
93  */
flb_localtime_r(time_t * timep,struct tm * result)94 static struct tm *flb_localtime_r(time_t *timep, struct tm *result)
95 {
96     if (localtime_s(result, timep)) {
97         return NULL;
98     }
99     return result;
100 }
101 #define localtime_r flb_localtime_r
102 
basename(const char * path)103 static inline char* basename(const char *path)
104 {
105     char drive[_MAX_DRIVE];
106     char dir[_MAX_DIR];
107     char fname[_MAX_FNAME];
108     char ext[_MAX_EXT];
109     static char buf[_MAX_PATH];
110 
111     _splitpath_s(path, drive, _MAX_DRIVE, dir, _MAX_DIR,
112                        fname, _MAX_FNAME, ext, _MAX_EXT);
113 
114     _makepath_s(buf, _MAX_PATH, "", "", fname, ext);
115     return buf;
116 }
117 
realpath(char * path,char * buf)118 static inline char* realpath(char *path, char *buf)
119 {
120     if (buf != NULL) {
121         return NULL;  /* Read BUGS in realpath(3) */
122     }
123     return _fullpath(NULL, path, 0);
124 }
125 
usleep(LONGLONG usec)126 static inline int usleep(LONGLONG usec)
127 {
128     // Convert into 100ns unit.
129     return nanosleep(usec * 10);
130 }
131 #else
132 #include <netdb.h>
133 #include <netinet/in.h>
134 #include <netinet/tcp.h>
135 #include <sys/socket.h>
136 #include <arpa/inet.h>
137 #include <libgen.h>
138 #include <dlfcn.h>
139 
140 #define FLB_DIRCHAR '/'
141 #endif
142 
143 #endif
144