Skip to content

Commit

Permalink
[libplugin] add
Browse files Browse the repository at this point in the history
  • Loading branch information
gozfree committed Sep 16, 2018
1 parent 6b352b4 commit 9a8bd6a
Show file tree
Hide file tree
Showing 11 changed files with 591 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libplugin/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := libplugin

LIBRARIES_DIR := $(LOCAL_PATH)/../

LOCAL_C_INCLUDES := $(LOCAL_PATH)

# Add your application source files here...
LOCAL_SRC_FILES := libplugin.c

include $(BUILD_SHARED_LIBRARY)
104 changes: 104 additions & 0 deletions libplugin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
###############################################################################
# common
###############################################################################
#ARCH: linux/pi/android/ios/
ARCH ?= linux
CROSS_PREFIX ?=
OUTPUT ?= /usr/local
BUILD_DIR := $(shell pwd)/../build/
ARCH_INC := $(BUILD_DIR)/$(ARCH).inc
COLOR_INC := $(BUILD_DIR)/color.inc

ifeq ($(ARCH_INC), $(wildcard $(ARCH_INC)))
include $(ARCH_INC)
endif

CC = $(CROSS_PREFIX)gcc
CXX = $(CROSS_PREFIX)g++
LD = $(CROSS_PREFIX)ld
AR = $(CROSS_PREFIX)ar

ifeq ($(COLOR_INC), $(wildcard $(COLOR_INC)))
include $(COLOR_INC)
else
CC_V = $(CC)
CXX_V = $(CXX)
LD_V = $(LD)
AR_V = $(AR)
CP_V = $(CP)
RM_V = $(RM)
endif

###############################################################################
# target and object
###############################################################################
LIBNAME = libplugin
VERSION_SH = $(shell pwd)/version.sh $(LIBNAME)
VER = $(shell $(VERSION_SH); awk '/define\ $(LIBNAME)_version/{print $$3}' version.h)
TGT_LIB_H = $(LIBNAME).h
TGT_LIB_A = $(LIBNAME).a
TGT_LIB_SO = $(LIBNAME).so
TGT_LIB_SO_VER = $(TGT_LIB_SO).${VER}
TGT_UNIT_TEST = test_$(LIBNAME)

OBJS_LIB = $(LIBNAME).o
OBJS_UNIT_TEST = test_$(LIBNAME).o

###############################################################################
# cflags and ldflags
###############################################################################
CFLAGS := -g -Wall -Werror -fPIC
CFLAGS += $($(ARCH)_CFLAGS)
CFLAGS += -I$(OUTPUT)/include

SHARED := -shared

LDFLAGS := $($(ARCH)_LDFLAGS)
LDFLAGS += -pthread -ldl

###############################################################################
# target
###############################################################################
.PHONY : all clean

TGT := $(TGT_LIB_A)
TGT += $(TGT_LIB_SO)
TGT += $(TGT_UNIT_TEST)

OBJS := $(OBJS_LIB) $(OBJS_UNIT_TEST)

all: $(TGT)

%.o:%.c
$(CC_V) -c $(CFLAGS) $< -o $@

$(TGT_LIB_A): $(OBJS_LIB)
$(AR_V) rcs $@ $^

$(TGT_LIB_SO): $(OBJS_LIB)
$(LD_V) -o $@ $^ $(SHARED)
@mv $(TGT_LIB_SO) $(TGT_LIB_SO_VER)
@ln -sf $(TGT_LIB_SO_VER) $(TGT_LIB_SO)

$(TGT_UNIT_TEST): $(OBJS_UNIT_TEST) $(ANDROID_MAIN_OBJ)
$(CC_V) -o $@ $^ $(TGT_LIB_A) $(LDFLAGS)

clean:
$(RM_V) -f $(OBJS)
$(RM_V) -f $(TGT)
$(RM_V) -f version.h
$(RM_V) -f $(TGT_LIB_SO)*
$(RM_V) -f $(TGT_LIB_SO_VER)

install:
$(MAKEDIR_OUTPUT)
$(CP_V) -r $(TGT_LIB_H) $(OUTPUT)/include
$(CP_V) -r $(TGT_LIB_A) $(OUTPUT)/lib
$(CP_V) -r $(TGT_LIB_SO) $(OUTPUT)/lib
$(CP_V) -r $(TGT_LIB_SO_VER) $(OUTPUT)/lib

uninstall:
$(RM_V) -f $(OUTPUT)/include/$(TGT_LIB_H)
$(RM_V) -f $(OUTPUT)/lib/$(TGT_LIB_A)
$(RM_V) -f $(OUTPUT)/lib/$(TGT_LIB_SO)
$(RM_V) -f $(OUTPUT)/lib/$(TGT_LIB_SO_VER)
3 changes: 3 additions & 0 deletions libplugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## libplugin
This is a simple libplugin library.

130 changes: 130 additions & 0 deletions libplugin/libplugin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <gozfree@163.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "libplugin.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <libmacro.h>

struct plugin_manager *plugin_manager_create()
{
struct plugin_manager *pm = CALLOC(1, struct plugin_manager);
if (!pm) {
printf("malloc failed!\n");
return NULL;
}
INIT_LIST_HEAD(&pm->plugins);
return pm;
}

void plugin_manager_destroy(struct plugin_manager *pm)
{
if (!pm) {
return;
}
}

static void *plugin_get_func(struct plugin *p, const char *name)
{
if (!p || !name) {
return NULL;
}
struct plugin *q = dlsym(p->handle, name);
if (!q) {
printf("dlsym failed:%s\n", dlerror());
return NULL;
}
return q;
}


struct plugin *plugin_lookup(struct plugin_manager *pm, const char *name)
{
if (!pm || !name)
return NULL;

struct list_head* head = NULL;
struct plugin* p = NULL;

list_for_each(head, &pm->plugins) {
p = list_entry(head, struct plugin, entry);
if (0 == strcmp(p->name, name)) {
return plugin_get_func(p, name);
}
}

return NULL;
}

struct plugin *plugin_load(struct plugin_manager *pm, const char *path, const char *name)
{
struct plugin *sym = NULL;
struct plugin *p = NULL;
void *handle = dlopen(path, RTLD_LAZY);
if (!handle) {
printf("dlopen failed: %s\n", dlerror());
goto failed;
}
p = plugin_lookup(pm, name);
if (p) {
printf("plugin %s has already loaded!\n", name);
return p;
}
sym = dlsym(handle, name);
if (!sym) {
printf("incompatible plugin, dlsym failed: %s\n", dlerror());
goto failed;
}
p = CALLOC(1, struct plugin);
if (!p) {
goto failed;
}
p->handle = handle;
p->name = strdup(name);
p->path = strdup(path);
list_add(&p->entry, &pm->plugins);
return p;

failed:
if (handle) dlclose(handle);
if (p) {
free(p->name);
free(p->path);
free(p);
}
return NULL;
}

void plugin_unload(struct plugin_manager *pm, const char *name)
{
struct plugin *p, *tmp;
list_for_each_entry_safe(p, tmp, &pm->plugins, entry) {
dlclose(p->handle);
list_del(&p->entry);
free(p->name);
free(p->path);
free(p);
}
}

struct plugin *plugin_reload(struct plugin_manager *pm, const char *path, const char *name)
{
plugin_unload(pm, name);
return plugin_load(pm, path, name);
}
62 changes: 62 additions & 0 deletions libplugin/libplugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <gozfree@163.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#ifndef LIBPLUGIN_H
#define LIBPLUGIN_H

#include <libmacro.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

struct version {
int major;
int minor;
int patch;
};

struct plugin {
char *name;
char *path;
struct version version;

void *(*open)(void *arg);
void (*close)(void *arg);
void *(*call)(void *arg0, ...);

void *handle;
struct list_head entry;
};

struct plugin_manager {
struct list_head plugins;
};

struct plugin_manager *plugin_manager_create();
void plugin_manager_destroy(struct plugin_manager *);

struct plugin *plugin_lookup(struct plugin_manager *pm, const char *name);
struct plugin *plugin_load(struct plugin_manager *pm, const char *path, const char *name);
void plugin_unload(struct plugin_manager *pm, const char *name);
struct plugin *plugin_reload(struct plugin_manager *pm, const char *path, const char *name);

#ifdef __cplusplus
}
#endif
#endif
80 changes: 80 additions & 0 deletions libplugin/modules/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
###############################################################################
# common
###############################################################################
#ARCH: linux/pi/android/ios/
ARCH ?= linux
CROSS_PREFIX ?=
OUTPUT ?= /usr/local
BUILD_DIR := $(shell pwd)/../build/
ARCH_INC := $(BUILD_DIR)/$(ARCH).inc
COLOR_INC := $(BUILD_DIR)/color.inc

ifeq ($(ARCH_INC), $(wildcard $(ARCH_INC)))
include $(ARCH_INC)
endif

CC = $(CROSS_PREFIX)gcc
CXX = $(CROSS_PREFIX)g++
LD = $(CROSS_PREFIX)ld
AR = $(CROSS_PREFIX)ar

ifeq ($(COLOR_INC), $(wildcard $(COLOR_INC)))
include $(COLOR_INC)
else
CC_V = $(CC)
CXX_V = $(CXX)
LD_V = $(LD)
AR_V = $(AR)
CP_V = $(CP)
RM_V = $(RM)
endif

###############################################################################
# target and object
###############################################################################
LIBNAME = libplugin
VERSION_SH = $(shell pwd)/version.sh $(LIBNAME)
VER = $(shell $(VERSION_SH); awk '/define\ $(LIBNAME)_version/{print $$3}' version.h)
TGT_LIB_H = $(LIBNAME).h
TGT_LIB_SO = plugin_log.so plugin_ipc.so plugin_skt.so

OBJS_LIB = plugin_log.o plugin_skt.o plugin_ipc.o

###############################################################################
# cflags and ldflags
###############################################################################
CFLAGS := -g -Wall -Werror -fPIC
CFLAGS += $($(ARCH)_CFLAGS)
CFLAGS += -I$(OUTPUT)/include

SHARED := -shared

LDFLAGS := $($(ARCH)_LDFLAGS)
LDFLAGS += -pthread -ldl

###############################################################################
# target
###############################################################################
.PHONY : all clean

TGT := $(TGT_LIB_SO)

OBJS := $(OBJS_LIB)

all: $(TGT)

%.o:%.c
$(CC_V) -c $(CFLAGS) $< -o $@

plugin_log.so: plugin_log.o
$(LD_V) -o $@ $^ $(SHARED)

plugin_ipc.so: plugin_ipc.o
$(LD_V) -o $@ $^ $(SHARED)

plugin_skt.so: plugin_skt.o
$(LD_V) -o $@ $^ $(SHARED)
clean:
$(RM_V) -f $(OBJS)
$(RM_V) -f $(TGT)
$(RM_V) -f version.h
Loading

0 comments on commit 9a8bd6a

Please sign in to comment.