Skip to content

Commit

Permalink
mk_core: upgrade to newer version used in Monkey Server
Browse files Browse the repository at this point in the history
this patch upgrades the mk_core sub-project to the latest one available
on Monkey Server project (http://github.com/monkey/monkey), it includes
improvements and fixes for OSX and BSD systems.

 090462e mk_core: event: epoll: fix parameter when trace is enabled
 eed8157 mk_core: adopt mk_file interface
 42225b6 mk_core: event: epoll: align API change for event_del()
 5a2d722 mk_core: event (mk_server): use event context when deleting a connection
 8ba5c4f mk_core: kqueue: fix mask when adding a new fd
 2cdeec3 mk_core: add missing declaration of memrchr for OSX
 c80b114 mk_core: kqueue: remove unused variable

Signed-off-by: Eduardo Silva <eduardo@treasure-data.com>
  • Loading branch information
edsiper committed Jun 9, 2015
1 parent d72b589 commit 7fd3c10
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ include_directories(include/ lib/ lib/msgpack-0.5.9/src)
# MSGPACK modification: disable the install routines.z
set(MSGPACK_NO_INSTALL 1)

# mk_core is aware about jemalloc usage, we need to disable this as
# fluent-bit do not use it.
set(WITH_SYSTEM_MALLOC 1)

# Lib: build the core libraries used by Fluent-Bit
add_subdirectory(lib/msgpack-0.5.9)
#add_subdirectory(lib/mk_config)
Expand Down
6 changes: 6 additions & 0 deletions lib/mk_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")

set(src
mk_iov.c
mk_file.c
mk_rconf.c
mk_string.c
mk_memory.c
Expand All @@ -12,3 +13,8 @@ set(src

include_directories(include)
add_library(mk_core STATIC ${src})
target_link_libraries(mk_core ${CMAKE_THREAD_LIBS_INIT})

if(NOT WITH_SYSTEM_MALLOC)
target_link_libraries(mk_core libjemalloc)
endif()
2 changes: 1 addition & 1 deletion lib/mk_core/include/mk_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct mk_event_loop *mk_event_loop_create(int size);
void mk_event_loop_destroy(struct mk_event_loop *loop);
int mk_event_add(struct mk_event_loop *loop, int fd,
int type, uint32_t mask, void *data);
int mk_event_del(struct mk_event_loop *loop, int fd);
int mk_event_del(struct mk_event_loop *loop, struct mk_event *event);
int mk_event_timeout_create(struct mk_event_loop *loop, int expire, void *data);
int mk_event_channel_create(struct mk_event_loop *loop,
int *r_fd, int *w_fd, void *data);
Expand Down
1 change: 0 additions & 1 deletion lib/mk_core/include/mk_event_kqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ static inline int filter_mask(int16_t f)
#define mk_event_foreach(event, evl) \
int __i; \
struct mk_event_ctx *ctx = evl->data; \
struct mk_event_fd_state *st = NULL; \
\
if (evl->n_events > 0) { \
event = ctx->events[0].udata; \
Expand Down
48 changes: 48 additions & 0 deletions lib/mk_core/include/mk_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Monkey HTTP Server
* ==================
* Copyright 2001-2015 Monkey Software LLC <eduardo@monkey.io>
*
* Licensed 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.
*/

#ifndef MK_FILE_H
#define MK_FILE_H

#include <time.h>

#define MK_FILE_EXISTS 1
#define MK_FILE_READ 2
#define MK_FILE_EXEC 4

struct file_info
{
off_t size;
time_t last_modification;

/* Suggest flags to open this file */
int flags_read_only;

unsigned char exists;
unsigned char is_file;
unsigned char is_link;
unsigned char is_directory;
unsigned char exec_access;
unsigned char read_access;
};

int mk_file_get_info(const char *path, struct file_info *f_info, int mode);
char *mk_file_to_buffer(const char *path);

#endif
4 changes: 4 additions & 0 deletions lib/mk_core/include/mk_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ char *mk_string_copy_substr(const char *string, int pos_init, int pos_end);

char *mk_string_tolower(const char *in);

#if defined (__APPLE__)
void *memrchr(const void *s, int c, size_t n);
#endif

#endif
5 changes: 5 additions & 0 deletions lib/mk_core/include/mk_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ static inline void mk_utils_libc_warning(char *caller, char *file, int line)
mk_warn("%s: %s, errno=%i at %s:%i", caller, buf, _err, file, line);
}

pthread_t mk_utils_worker_spawn(void (*func) (void *), void *arg);
int mk_utils_worker_rename(const char *title);
int mk_utils_set_daemon();
int mk_utils_register_pid(char *path);
int mk_utils_remove_pid(char *path);
int mk_core_init();

#endif
7 changes: 7 additions & 0 deletions lib/mk_core/mk_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
#ifndef MK_CORE_H
#define MK_CORE_H

#include <sys/types.h>

/* Process UID/GID */
extern gid_t EGID;
extern gid_t EUID;

#include "include/mk_iov.h"
#include "include/mk_file.h"
#include "include/mk_event.h"
#include "include/mk_rbtree.h"
#include "include/mk_rconf.h"
Expand Down
4 changes: 2 additions & 2 deletions lib/mk_core/mk_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ int mk_event_add(struct mk_event_loop *loop, int fd,
}

/* Remove an event */
int mk_event_del(struct mk_event_loop *loop, int fd)
int mk_event_del(struct mk_event_loop *loop, struct mk_event *event)
{
int ret;
struct mk_event_ctx *ctx;

ctx = loop->data;

ret = _mk_event_del(ctx, fd);
ret = _mk_event_del(ctx, event);
if (ret == -1) {
return -1;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/mk_core/mk_event_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ static inline int _mk_event_add(struct mk_event_ctx *ctx, int fd,
}

/* Delete an event */
static inline int _mk_event_del(struct mk_event_ctx *ctx, int fd)
static inline int _mk_event_del(struct mk_event_ctx *ctx, struct mk_event *event)
{
int ret;

ret = epoll_ctl(ctx->efd, EPOLL_CTL_DEL, fd, NULL);
ret = epoll_ctl(ctx->efd, EPOLL_CTL_DEL, event->fd, NULL);
MK_TRACE("[FD %i] Epoll, remove from QUEUE_FD=%i, ret=%i",
fd, ctx->efd, ret);
event->fd, ctx->efd, ret);
if (ret < 0) {
mk_libc_error("epoll_ctl");
}
Expand Down
28 changes: 16 additions & 12 deletions lib/mk_core/mk_event_kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,31 @@ static inline int _mk_event_add(struct mk_event_ctx *ctx, int fd,
}
}

event->mask = events;
return 0;
}

static inline int _mk_event_del(struct mk_event_ctx *ctx, int fd)
static inline int _mk_event_del(struct mk_event_ctx *ctx, struct mk_event *event)
{
int ret;
struct kevent ke = {0, 0, 0, 0, 0, 0};

EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
if (ret < 0) {
mk_libc_error("kevent");
return ret;
if (event->mask & MK_EVENT_READ) {
EV_SET(&ke, event->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
if (ret < 0) {
mk_libc_error("kevent");
return ret;
}
}

EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
if (ret < 0) {
mk_libc_error("kevent");
return ret;
if (event->mask & MK_EVENT_WRITE) {
EV_SET(&ke, event->fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
ret = kevent(ctx->kfd, &ke, 1, NULL, 0, NULL);
if (ret < 0) {
mk_libc_error("kevent");
return ret;
}
}

return 0;
Expand Down Expand Up @@ -185,7 +190,6 @@ static inline int _mk_event_timeout_create(struct mk_event_ctx *ctx,
* to confirm how it behave on native OSX.
*/
event->mask = MK_EVENT_READ;

return fd;
}

Expand Down
142 changes: 142 additions & 0 deletions lib/mk_core/mk_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*-*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Monkey HTTP Server
* ==================
* Copyright 2001-2015 Monkey Software LLC <eduardo@monkey.io>
*
* Licensed 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.
*/

#define _GNU_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "mk_core.h"

int mk_file_get_info(const char *path, struct file_info *f_info, int mode)
{
struct stat f, target;

f_info->exists = MK_FALSE;

/* Stat right resource */
if (lstat(path, &f) == -1) {
if (errno == EACCES) {
f_info->exists = MK_TRUE;
}
return -1;
}

f_info->exists = MK_TRUE;
f_info->is_file = MK_TRUE;
f_info->is_link = MK_FALSE;
f_info->is_directory = MK_FALSE;
f_info->exec_access = MK_FALSE;
f_info->read_access = MK_FALSE;

if (S_ISLNK(f.st_mode)) {
f_info->is_link = MK_TRUE;
f_info->is_file = MK_FALSE;
if (stat(path, &target) == -1) {
return -1;
}
}
else {
target = f;
}

f_info->size = target.st_size;
f_info->last_modification = target.st_mtime;

if (S_ISDIR(target.st_mode)) {
f_info->is_directory = MK_TRUE;
f_info->is_file = MK_FALSE;
}

/* Check read access */
if (mode & MK_FILE_READ) {
if (((target.st_mode & S_IRUSR) && target.st_uid == EUID) ||
((target.st_mode & S_IRGRP) && target.st_gid == EGID) ||
(target.st_mode & S_IROTH)) {
f_info->read_access = MK_TRUE;
}
}

/* Checking execution */
if (mode & MK_FILE_EXEC) {
if ((target.st_mode & S_IXUSR && target.st_uid == EUID) ||
(target.st_mode & S_IXGRP && target.st_gid == EGID) ||
(target.st_mode & S_IXOTH)) {
f_info->exec_access = MK_TRUE;
}
}

/* Suggest open(2) flags */
f_info->flags_read_only = O_RDONLY | O_NONBLOCK;

#if defined(__linux__)
/*
* If the user is the owner of the file or the user is root, it
* can set the O_NOATIME flag for open(2) operations to avoid
* inode updates about last accessed time
*/
if (target.st_uid == EUID || EUID == 0) {
f_info->flags_read_only |= O_NOATIME;
}
#endif

return 0;
}

/* Read file content to a memory buffer,
* Use this function just for really SMALL files
*/
char *mk_file_to_buffer(const char *path)
{
FILE *fp;
char *buffer;
long bytes;
struct file_info finfo;

if (mk_file_get_info(path, &finfo, MK_FILE_READ) != 0) {
return NULL;
}

if (!(fp = fopen(path, "r"))) {
return NULL;
}

buffer = mk_mem_malloc_z(finfo.size + 1);
if (!buffer) {
fclose(fp);
return NULL;
}

bytes = fread(buffer, finfo.size, 1, fp);

if (bytes < 1) {
mk_mem_free(buffer);
fclose(fp);
return NULL;
}

fclose(fp);
return (char *) buffer;

}

0 comments on commit 7fd3c10

Please sign in to comment.