Skip to content

Commit 3ab208c

Browse files
committed
Merge branch 'master' into http
2 parents 3e731e9 + 6858d2c commit 3ab208c

17 files changed

+379
-27
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ include_directories(include/ lib/ lib/msgpack-0.5.9/src)
4848
# MSGPACK modification: disable the install routines.z
4949
set(MSGPACK_NO_INSTALL 1)
5050

51+
# mk_core is aware about jemalloc usage, we need to disable this as
52+
# fluent-bit do not use it.
53+
set(WITH_SYSTEM_MALLOC 1)
54+
5155
# Lib: build the core libraries used by Fluent-Bit
5256
add_subdirectory(lib/msgpack-0.5.9)
5357
add_subdirectory(lib/mk_core)

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ $ make
1919
When building _Fluent-Bit_, the following options are available when running __cmake__:
2020

2121
option | value type | description | default
22-
------------|------------|---------------------------------------------|---------
23-
WITH_ALL | bool | Enable all features available | off
24-
WITH_XBEE | bool | Enable XBee support (input) | off
25-
WITH_DEBUG | bool | Include debug symbols when building targets | off
26-
WITHOUT_BIN | bool | Do not build the fluent-bit executable | off
22+
-------------|------------|---------------------------------------------|---------
23+
WITH_ALL | bool | Enable all features available | off
24+
WITH_IN_XBEE | bool | Enable XBee support (input) | off
25+
WITH_DEBUG | bool | Include debug symbols when building targets | off
26+
WITHOUT_BIN | bool | Do not build the fluent-bit executable | off
2727

2828
In order to active one of these features, you need to set a boolean value. As an example if we would like to build _Fluent-Bit_ with _XBee_ support we should do:
2929

3030
```bash
3131
$ cd build/
32-
$ cmake -DWITH_XBEE=1 ..
32+
$ cmake -DWITH_IN_XBEE=1 ..
3333
$ make
3434
```
3535

@@ -46,11 +46,11 @@ Once the tool have been compiled, a binary file called _Fluent-Bit_ will be foun
4646
| CPU | cpu | gather CPU usage between snapshots of one second. It support multiple cores |
4747
| Memory | mem | usage of system memory |
4848
| Kernel Ring Buffer | kmsg | read Linux Kernel messages, same behavior as the __dmesg__ command line program |
49-
| XBee | xbee | listen for incoming messages over a Xbee device |
49+
| XBee | xbee | listen for incoming messages over a Xbee device |
5050

5151
### Output Plugins
5252

53-
| name | option | description |
53+
| name | option | description |
5454
|--------------------|-------------------------|---------------------------------------------------------------------------------|
5555
| Fluentd | fluentd://host:port | flush content to a [Fluentd](http://fluentd.org) service. On the [Fluentd](http://fluentd.org) side, it requires an __in_forward__.|
5656
| TreasureData | td | flush data collected to [TreasureData](http://treasuredata.com) service (cloud analytics platform) |

lib/mk_core/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
22

33
set(src
44
mk_iov.c
5+
mk_file.c
56
mk_rconf.c
67
mk_string.c
78
mk_memory.c
@@ -12,3 +13,8 @@ set(src
1213

1314
include_directories(include)
1415
add_library(mk_core STATIC ${src})
16+
target_link_libraries(mk_core ${CMAKE_THREAD_LIBS_INIT})
17+
18+
if(NOT WITH_SYSTEM_MALLOC)
19+
target_link_libraries(mk_core libjemalloc)
20+
endif()

lib/mk_core/include/mk_event.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct mk_event_loop *mk_event_loop_create(int size);
8686
void mk_event_loop_destroy(struct mk_event_loop *loop);
8787
int mk_event_add(struct mk_event_loop *loop, int fd,
8888
int type, uint32_t mask, void *data);
89-
int mk_event_del(struct mk_event_loop *loop, int fd);
89+
int mk_event_del(struct mk_event_loop *loop, struct mk_event *event);
9090
int mk_event_timeout_create(struct mk_event_loop *loop, int expire, void *data);
9191
int mk_event_channel_create(struct mk_event_loop *loop,
9292
int *r_fd, int *w_fd, void *data);

lib/mk_core/include/mk_event_kqueue.h

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ static inline int filter_mask(int16_t f)
5959
#define mk_event_foreach(event, evl) \
6060
int __i; \
6161
struct mk_event_ctx *ctx = evl->data; \
62-
struct mk_event_fd_state *st = NULL; \
6362
\
6463
if (evl->n_events > 0) { \
6564
event = ctx->events[0].udata; \

lib/mk_core/include/mk_file.h

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Monkey HTTP Server
4+
* ==================
5+
* Copyright 2001-2015 Monkey Software LLC <eduardo@monkey.io>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* 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, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#ifndef MK_FILE_H
21+
#define MK_FILE_H
22+
23+
#include <time.h>
24+
25+
#define MK_FILE_EXISTS 1
26+
#define MK_FILE_READ 2
27+
#define MK_FILE_EXEC 4
28+
29+
struct file_info
30+
{
31+
off_t size;
32+
time_t last_modification;
33+
34+
/* Suggest flags to open this file */
35+
int flags_read_only;
36+
37+
unsigned char exists;
38+
unsigned char is_file;
39+
unsigned char is_link;
40+
unsigned char is_directory;
41+
unsigned char exec_access;
42+
unsigned char read_access;
43+
};
44+
45+
int mk_file_get_info(const char *path, struct file_info *f_info, int mode);
46+
char *mk_file_to_buffer(const char *path);
47+
48+
#endif

lib/mk_core/include/mk_string.h

+4
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ char *mk_string_copy_substr(const char *string, int pos_init, int pos_end);
6464

6565
char *mk_string_tolower(const char *in);
6666

67+
#if defined (__APPLE__)
68+
void *memrchr(const void *s, int c, size_t n);
69+
#endif
70+
6771
#endif

lib/mk_core/include/mk_utils.h

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ static inline void mk_utils_libc_warning(char *caller, char *file, int line)
8181
mk_warn("%s: %s, errno=%i at %s:%i", caller, buf, _err, file, line);
8282
}
8383

84+
pthread_t mk_utils_worker_spawn(void (*func) (void *), void *arg);
85+
int mk_utils_worker_rename(const char *title);
86+
int mk_utils_set_daemon();
87+
int mk_utils_register_pid(char *path);
88+
int mk_utils_remove_pid(char *path);
8489
int mk_core_init();
8590

8691
#endif

lib/mk_core/mk_core.h

+7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
#ifndef MK_CORE_H
2121
#define MK_CORE_H
2222

23+
#include <sys/types.h>
24+
25+
/* Process UID/GID */
26+
extern gid_t EGID;
27+
extern gid_t EUID;
28+
2329
#include "include/mk_iov.h"
30+
#include "include/mk_file.h"
2431
#include "include/mk_event.h"
2532
#include "include/mk_rbtree.h"
2633
#include "include/mk_rconf.h"

lib/mk_core/mk_event.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ int mk_event_add(struct mk_event_loop *loop, int fd,
8989
}
9090

9191
/* Remove an event */
92-
int mk_event_del(struct mk_event_loop *loop, int fd)
92+
int mk_event_del(struct mk_event_loop *loop, struct mk_event *event)
9393
{
9494
int ret;
9595
struct mk_event_ctx *ctx;
9696

9797
ctx = loop->data;
9898

99-
ret = _mk_event_del(ctx, fd);
99+
ret = _mk_event_del(ctx, event);
100100
if (ret == -1) {
101101
return -1;
102102
}

lib/mk_core/mk_event_epoll.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ static inline int _mk_event_add(struct mk_event_ctx *ctx, int fd,
106106
}
107107

108108
/* Delete an event */
109-
static inline int _mk_event_del(struct mk_event_ctx *ctx, int fd)
109+
static inline int _mk_event_del(struct mk_event_ctx *ctx, struct mk_event *event)
110110
{
111111
int ret;
112112

113-
ret = epoll_ctl(ctx->efd, EPOLL_CTL_DEL, fd, NULL);
113+
ret = epoll_ctl(ctx->efd, EPOLL_CTL_DEL, event->fd, NULL);
114114
MK_TRACE("[FD %i] Epoll, remove from QUEUE_FD=%i, ret=%i",
115-
fd, ctx->efd, ret);
115+
event->fd, ctx->efd, ret);
116116
if (ret < 0) {
117117
mk_libc_error("epoll_ctl");
118118
}

lib/mk_core/mk_event_kqueue.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,31 @@ static inline int _mk_event_add(struct mk_event_ctx *ctx, int fd,
118118
}
119119
}
120120

121+
event->mask = events;
121122
return 0;
122123
}
123124

124-
static inline int _mk_event_del(struct mk_event_ctx *ctx, int fd)
125+
static inline int _mk_event_del(struct mk_event_ctx *ctx, struct mk_event *event)
125126
{
126127
int ret;
127128
struct kevent ke = {0, 0, 0, 0, 0, 0};
128129

129-
EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
130-
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
131-
if (ret < 0) {
132-
mk_libc_error("kevent");
133-
return ret;
130+
if (event->mask & MK_EVENT_READ) {
131+
EV_SET(&ke, event->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
132+
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
133+
if (ret < 0) {
134+
mk_libc_error("kevent");
135+
return ret;
136+
}
134137
}
135138

136-
EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
137-
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
138-
if (ret < 0) {
139-
mk_libc_error("kevent");
140-
return ret;
139+
if (event->mask & MK_EVENT_WRITE) {
140+
EV_SET(&ke, event->fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
141+
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
142+
if (ret < 0) {
143+
mk_libc_error("kevent");
144+
return ret;
145+
}
141146
}
142147

143148
return 0;
@@ -185,7 +190,6 @@ static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
185190
* to confirm how it behave on native OSX.
186191
*/
187192
event->mask = MK_EVENT_READ;
188-
189193
return fd;
190194
}
191195

lib/mk_core/mk_file.c

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*-*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Monkey HTTP Server
4+
* ==================
5+
* Copyright 2001-2015 Monkey Software LLC <eduardo@monkey.io>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* 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, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#define _GNU_SOURCE
21+
22+
#include <sys/types.h>
23+
#include <sys/stat.h>
24+
#include <fcntl.h>
25+
#include <unistd.h>
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include <errno.h>
29+
30+
#include "mk_core.h"
31+
32+
int mk_file_get_info(const char *path, struct file_info *f_info, int mode)
33+
{
34+
struct stat f, target;
35+
36+
f_info->exists = MK_FALSE;
37+
38+
/* Stat right resource */
39+
if (lstat(path, &f) == -1) {
40+
if (errno == EACCES) {
41+
f_info->exists = MK_TRUE;
42+
}
43+
return -1;
44+
}
45+
46+
f_info->exists = MK_TRUE;
47+
f_info->is_file = MK_TRUE;
48+
f_info->is_link = MK_FALSE;
49+
f_info->is_directory = MK_FALSE;
50+
f_info->exec_access = MK_FALSE;
51+
f_info->read_access = MK_FALSE;
52+
53+
if (S_ISLNK(f.st_mode)) {
54+
f_info->is_link = MK_TRUE;
55+
f_info->is_file = MK_FALSE;
56+
if (stat(path, &target) == -1) {
57+
return -1;
58+
}
59+
}
60+
else {
61+
target = f;
62+
}
63+
64+
f_info->size = target.st_size;
65+
f_info->last_modification = target.st_mtime;
66+
67+
if (S_ISDIR(target.st_mode)) {
68+
f_info->is_directory = MK_TRUE;
69+
f_info->is_file = MK_FALSE;
70+
}
71+
72+
/* Check read access */
73+
if (mode & MK_FILE_READ) {
74+
if (((target.st_mode & S_IRUSR) && target.st_uid == EUID) ||
75+
((target.st_mode & S_IRGRP) && target.st_gid == EGID) ||
76+
(target.st_mode & S_IROTH)) {
77+
f_info->read_access = MK_TRUE;
78+
}
79+
}
80+
81+
/* Checking execution */
82+
if (mode & MK_FILE_EXEC) {
83+
if ((target.st_mode & S_IXUSR && target.st_uid == EUID) ||
84+
(target.st_mode & S_IXGRP && target.st_gid == EGID) ||
85+
(target.st_mode & S_IXOTH)) {
86+
f_info->exec_access = MK_TRUE;
87+
}
88+
}
89+
90+
/* Suggest open(2) flags */
91+
f_info->flags_read_only = O_RDONLY | O_NONBLOCK;
92+
93+
#if defined(__linux__)
94+
/*
95+
* If the user is the owner of the file or the user is root, it
96+
* can set the O_NOATIME flag for open(2) operations to avoid
97+
* inode updates about last accessed time
98+
*/
99+
if (target.st_uid == EUID || EUID == 0) {
100+
f_info->flags_read_only |= O_NOATIME;
101+
}
102+
#endif
103+
104+
return 0;
105+
}
106+
107+
/* Read file content to a memory buffer,
108+
* Use this function just for really SMALL files
109+
*/
110+
char *mk_file_to_buffer(const char *path)
111+
{
112+
FILE *fp;
113+
char *buffer;
114+
long bytes;
115+
struct file_info finfo;
116+
117+
if (mk_file_get_info(path, &finfo, MK_FILE_READ) != 0) {
118+
return NULL;
119+
}
120+
121+
if (!(fp = fopen(path, "r"))) {
122+
return NULL;
123+
}
124+
125+
buffer = mk_mem_malloc_z(finfo.size + 1);
126+
if (!buffer) {
127+
fclose(fp);
128+
return NULL;
129+
}
130+
131+
bytes = fread(buffer, finfo.size, 1, fp);
132+
133+
if (bytes < 1) {
134+
mk_mem_free(buffer);
135+
fclose(fp);
136+
return NULL;
137+
}
138+
139+
fclose(fp);
140+
return (char *) buffer;
141+
142+
}

0 commit comments

Comments
 (0)