Skip to content

Commit 6529c13

Browse files
committed
Windows port
1 parent 3fde138 commit 6529c13

File tree

15 files changed

+127
-32
lines changed

15 files changed

+127
-32
lines changed

src/daemon/collectd.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#ifndef COLLECTD_H
2828
#define COLLECTD_H
2929

30+
#ifdef WIN32
31+
#include "gnulib_config.h"
32+
#endif
33+
3034
#if HAVE_CONFIG_H
3135
#include "config.h"
3236
#endif
@@ -121,6 +125,10 @@
121125
#include <sys/isa_defs.h>
122126
#endif
123127

128+
#if HAVE_SYS_PARAM_H
129+
#include <sys/param.h>
130+
#endif
131+
124132
#ifndef BYTE_ORDER
125133
#if defined(_BYTE_ORDER)
126134
#define BYTE_ORDER _BYTE_ORDER
@@ -182,10 +190,6 @@
182190
#endif
183191
#endif
184192

185-
#if HAVE_SYS_PARAM_H
186-
#include <sys/param.h>
187-
#endif
188-
189193
#ifndef PACKAGE_NAME
190194
#define PACKAGE_NAME "collectd"
191195
#endif

src/daemon/common.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
* Michał Mirosław <mirq-linux at rere.qmqm.pl>
2828
**/
2929

30-
#if HAVE_CONFIG_H
31-
#include "config.h"
32-
#endif
33-
3430
#include "collectd.h"
3531

3632
#include "common.h"
@@ -65,11 +61,11 @@ extern kstat_ctl_t *kc;
6561
#endif
6662

6763
/* AIX doesn't have MSG_DONTWAIT */
68-
#ifndef MSG_DONTWAIT
64+
#if !defined(MSG_DONTWAIT) && defined(MSG_NONBLOCK)
6965
#define MSG_DONTWAIT MSG_NONBLOCK
7066
#endif
7167

72-
#if !HAVE_GETPWNAM_R
68+
#if !HAVE_GETPWNAM_R && defined(HAVE_GETPWNAM)
7369
static pthread_mutex_t getpwnam_r_lock = PTHREAD_MUTEX_INITIALIZER;
7470
#endif
7571

@@ -263,7 +259,12 @@ int swrite(int fd, const void *buf, size_t count) {
263259
pfd.revents = 0;
264260
if (poll(&pfd, 1, 0) > 0) {
265261
char buffer[32];
266-
if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) {
262+
if (recv(fd, buffer, sizeof(buffer),
263+
MSG_PEEK
264+
#ifndef WIN32
265+
| MSG_DONTWAIT
266+
#endif
267+
) == 0) {
267268
/* if recv returns zero (even though poll() said there is data to be
268269
* read), that means the connection has been closed */
269270
errno = ECONNRESET;
@@ -1126,6 +1127,9 @@ int parse_value_file(char const *path, value_t *ret_value, int ds_type) {
11261127
#if !HAVE_GETPWNAM_R
11271128
int getpwnam_r(const char *name, struct passwd *pwbuf, char *buf, size_t buflen,
11281129
struct passwd **pwbufp) {
1130+
#ifndef HAVE_GETPWNAM
1131+
return -1;
1132+
#else
11291133
int status = 0;
11301134
struct passwd *pw;
11311135

@@ -1168,6 +1172,7 @@ int getpwnam_r(const char *name, struct passwd *pwbuf, char *buf, size_t buflen,
11681172
pthread_mutex_unlock(&getpwnam_r_lock);
11691173

11701174
return status;
1175+
#endif /* HAVE_GETPWNAM */
11711176
} /* int getpwnam_r */
11721177
#endif /* !HAVE_GETPWNAM_R */
11731178

src/daemon/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ int parse_values(char *buffer, value_list_t *vl, const data_set_t *ds);
332332
int parse_value_file(char const *path, value_t *ret_value, int ds_type);
333333

334334
#if !HAVE_GETPWNAM_R
335+
struct passwd;
335336
int getpwnam_r(const char *name, struct passwd *pwbuf, char *buf, size_t buflen,
336337
struct passwd **pwbufp);
337338
#endif

src/daemon/globals.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
#endif
3232

3333
#ifndef PRIsz
34+
#ifdef WIN32
35+
#define PRIsz "Iu"
36+
#else
3437
#define PRIsz "zu"
35-
#endif /* PRIsz */
38+
#endif /* WIN32 */
39+
#endif /* !PRIsz */
3640

3741
/* Type for time as used by "utils_time.h" */
3842
typedef uint64_t cdtime_t;

src/daemon/plugin.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
#include "utils_random.h"
4343
#include "utils_time.h"
4444

45+
#ifdef WIN32
46+
#include <sys/stat.h>
47+
#include <unistd.h>
48+
#endif
49+
4550
#if HAVE_PTHREAD_NP_H
4651
#include <pthread_np.h> /* for pthread_set_name_np(3) */
4752
#endif
@@ -637,7 +642,7 @@ static void start_read_threads(size_t num) /* {{{ */
637642
}
638643

639644
char name[THREAD_NAME_MAX];
640-
snprintf(name, sizeof(name), "reader#%" PRIsz, read_threads_num);
645+
snprintf(name, sizeof(name), "reader#%" PRIu64, (uint64_t)read_threads_num);
641646
set_thread_name(read_threads[read_threads_num], name);
642647

643648
read_threads_num++;
@@ -843,7 +848,7 @@ static void start_write_threads(size_t num) /* {{{ */
843848
}
844849

845850
char name[THREAD_NAME_MAX];
846-
snprintf(name, sizeof(name), "writer#%" PRIsz, write_threads_num);
851+
snprintf(name, sizeof(name), "writer#%" PRIu64, (uint64_t)write_threads_num);
847852
set_thread_name(write_threads[write_threads_num], name);
848853

849854
write_threads_num++;
@@ -954,6 +959,11 @@ static void plugin_free_loaded(void) {
954959
}
955960

956961
#define BUFSIZE 512
962+
#ifdef WIN32
963+
# define SHLIB_SUFFIX ".dll"
964+
#else
965+
# define SHLIB_SUFFIX ".so"
966+
#endif
957967
int plugin_load(char const *plugin_name, _Bool global) {
958968
DIR *dh;
959969
const char *dir;
@@ -990,11 +1000,11 @@ int plugin_load(char const *plugin_name, _Bool global) {
9901000
(strcasecmp("python", plugin_name) == 0))
9911001
global = 1;
9921002

993-
/* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
1003+
/* `cpu' should not match `cpufreq'. To solve this we add SHLIB_SUFFIX to the
9941004
* type when matching the filename */
995-
status = snprintf(typename, sizeof(typename), "%s.so", plugin_name);
1005+
status = snprintf(typename, sizeof(typename), "%s" SHLIB_SUFFIX, plugin_name);
9961006
if ((status < 0) || ((size_t)status >= sizeof(typename))) {
997-
WARNING("plugin_load: Filename too long: \"%s.so\"", plugin_name);
1007+
WARNING("plugin_load: Filename too long: \"%s" SHLIB_SUFFIX "\"", plugin_name);
9981008
return -1;
9991009
}
10001010

src/daemon/plugin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@
3434
#include "meta_data.h"
3535
#include "utils_time.h"
3636

37+
#include <inttypes.h>
3738
#include <pthread.h>
3839

40+
#ifdef WIN32
41+
#undef ERROR
42+
#endif
43+
3944
#define DS_TYPE_COUNTER 0
4045
#define DS_TYPE_GAUGE 1
4146
#define DS_TYPE_DERIVE 2

src/daemon/utils_heap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* Florian octo Forster <octo at collectd.org>
2525
**/
2626

27+
#include "collectd.h"
28+
2729
#include <assert.h>
2830
#include <errno.h>
2931
#include <pthread.h>

src/daemon/utils_random.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131

3232
#include <pthread.h>
3333

34+
#ifdef WIN32
35+
double erand48(unsigned short unused[3]) {
36+
return (double)rand() / (double)RAND_MAX;
37+
}
38+
39+
long int jrand48(unsigned short unused[3]) {
40+
return rand();
41+
}
42+
#endif
43+
3444
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
3545
static _Bool have_seed = 0;
3646
static unsigned short seed[3];
@@ -47,6 +57,10 @@ static void cdrand_seed(void) {
4757
seed[1] = (unsigned short)(t >> 16);
4858
seed[2] = (unsigned short)(t >> 32);
4959

60+
#ifdef WIN32
61+
srand((unsigned)t);
62+
#endif
63+
5064
have_seed = 1;
5165
}
5266

src/libcollectdclient/client.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
* Florian octo Forster <octo at collectd.org>
2525
**/
2626

27+
#ifdef WIN32
28+
#include "gnulib_config.h"
29+
#include <winsock2.h>
30+
#else
31+
#include <sys/socket.h>
32+
#include <sys/un.h>
33+
#endif
34+
2735
#include "config.h"
2836

2937
#if !defined(__GNUC__) || !__GNUC__
@@ -40,9 +48,7 @@
4048
#include <stdio.h>
4149
#include <stdlib.h>
4250
#include <string.h>
43-
#include <sys/socket.h>
4451
#include <sys/types.h>
45-
#include <sys/un.h>
4652
#include <unistd.h>
4753

4854
#include "collectd/client.h"
@@ -367,6 +373,10 @@ static int lcc_sendreceive(lcc_connection_t *c, /* {{{ */
367373

368374
static int lcc_open_unixsocket(lcc_connection_t *c, const char *path) /* {{{ */
369375
{
376+
#ifdef WIN32
377+
lcc_set_errno(c, ENOTSUP);
378+
return -1;
379+
#else
370380
struct sockaddr_un sa = {0};
371381
int fd;
372382
int status;
@@ -401,6 +411,7 @@ static int lcc_open_unixsocket(lcc_connection_t *c, const char *path) /* {{{ */
401411
}
402412

403413
return 0;
414+
#endif /* WIN32 */
404415
} /* }}} int lcc_open_unixsocket */
405416

406417
static int lcc_open_netsocket(lcc_connection_t *c, /* {{{ */
@@ -453,8 +464,11 @@ static int lcc_open_netsocket(lcc_connection_t *c, /* {{{ */
453464
}
454465

455466
struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
456-
.ai_flags = AI_ADDRCONFIG,
467+
.ai_flags = 0,
457468
.ai_socktype = SOCK_STREAM};
469+
#ifdef AI_ADDRCONFIG
470+
ai_hints.ai_flags = AI_ADDRCONFIG;
471+
#endif
458472

459473
status = getaddrinfo(addr, port == NULL ? LCC_DEFAULT_PORT : port, &ai_hints,
460474
&ai_res);

src/libcollectdclient/collectd/network.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include <inttypes.h>
3333
#include <stdint.h>
3434

35+
#ifdef WIN32
36+
extern unsigned int if_nametoindex(const char *interface_name);
37+
#endif
38+
3539
#define NET_DEFAULT_V4_ADDR "239.192.74.66"
3640
#define NET_DEFAULT_V6_ADDR "ff18::efc0:4a42"
3741
#define NET_DEFAULT_PORT "25826"
@@ -60,7 +64,7 @@ int lcc_server_destroy(lcc_network_t *net, lcc_server_t *srv);
6064

6165
/* Configure servers */
6266
int lcc_server_set_ttl(lcc_server_t *srv, uint8_t ttl);
63-
int lcc_server_set_interface(lcc_server_t *srv, char const *interface);
67+
int lcc_server_set_interface(lcc_server_t *srv, char const *interface_);
6468
int lcc_server_set_security_level(lcc_server_t *srv, lcc_security_level_t level,
6569
const char *username, const char *password);
6670

0 commit comments

Comments
 (0)