diff --git a/builds/any/rootfs/jessie/standard/standard.yml b/builds/any/rootfs/jessie/standard/standard.yml index 82e08834..785a2638 100644 --- a/builds/any/rootfs/jessie/standard/standard.yml +++ b/builds/any/rootfs/jessie/standard/standard.yml @@ -58,6 +58,7 @@ Configure: - 'initdev defaults' - 'onl-platform-baseconf defaults' - 'faultd defaults' + - 'onlpd defaults' - 'snmpd remove' - 'onlp-snmpd remove' - 'ssh defaults' diff --git a/builds/any/rootfs/wheezy/standard/standard.yml b/builds/any/rootfs/wheezy/standard/standard.yml index e7abdcc4..16567372 100644 --- a/builds/any/rootfs/wheezy/standard/standard.yml +++ b/builds/any/rootfs/wheezy/standard/standard.yml @@ -58,6 +58,7 @@ Configure: - 'initdev defaults' - 'onl-platform-baseconf defaults' - 'faultd defaults' + - 'onlpd defaults' - 'snmpd remove' - 'onlp-snmpd remove' - 'ssh defaults' diff --git a/make/config.mk b/make/config.mk index 9f4109a9..4721ddae 100644 --- a/make/config.mk +++ b/make/config.mk @@ -35,3 +35,11 @@ export MODULEMANIFEST := $(shell $(BUILDER)/tools/mmg.py --dirs $(ONL) $(ONLPM_O ONL_MAKE_FLAGS += --no-print-directory -s ONL_MAKE := $(MAKE) $(ONL_MAKE_FLAGS) ONL_V_at := @ + +# +# Some build and autogen tools require these settings. +# +export SUBMODULE_INFRA := $(ONL)/sm/infra +export SUBMODULE_BIGCODE := $(ONL)/sm/bigcode + + diff --git a/packages/base/any/onlp/APKG.yml b/packages/base/any/onlp/APKG.yml index c9e3fbb4..b0d91c6a 100644 --- a/packages/base/any/onlp/APKG.yml +++ b/packages/base/any/onlp/APKG.yml @@ -26,6 +26,8 @@ packages: builds/onlp-platform-defaults/$BUILD_DIR/${TOOLCHAIN}/bin/libonlp-platform-defaults.so : $libdir/ builds/onlpd/$BUILD_DIR/${TOOLCHAIN}/bin/onlpd : $bindir/ + init: $ONL/packages/base/any/onlp/src/onlpd.init + changelog: Change changes changes., diff --git a/packages/base/any/onlp/builds/onlp/Makefile b/packages/base/any/onlp/builds/onlp/Makefile index f2918840..fb0df874 100644 --- a/packages/base/any/onlp/builds/onlp/Makefile +++ b/packages/base/any/onlp/builds/onlp/Makefile @@ -44,6 +44,8 @@ GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_CTOR_DTOR=1 GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_MODULES_INIT=1 GLOBAL_CFLAGS += -DONLP_CONFIG_API_LOCK_GLOBAL_SHARED=1 GLOBAL_CFLAGS += -DONLP_CONFIG_INCLUDE_SHLOCK_GLOBAL_INIT=1 +GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_PVS_SYSLOG=1 +GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_DAEMONIZE=1 GLOBAL_CFLAGS += -fPIC GLOBAL_LINK_LIBS += -lpthread $(LIBONLP_PLATFORM) $(LIBONLP_PLATFORM_DEFAULTS) diff --git a/packages/base/any/onlp/builds/onlpd/Makefile b/packages/base/any/onlp/builds/onlpd/Makefile index 0a3a9d49..258bd9f7 100644 --- a/packages/base/any/onlp/builds/onlpd/Makefile +++ b/packages/base/any/onlp/builds/onlpd/Makefile @@ -40,8 +40,11 @@ LIBONLP_SO := ../onlp/$(BUILD_DIR)/bin/libonlp.so GLOBAL_CFLAGS += -DAIM_CONFIG_AIM_MAIN_FUNCTION=onlpdump_main GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_MODULES_INIT=1 GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_MAIN=1 +GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_DAEMONIZE=1 +GLOBAL_CFLAGS += -DAIM_CONFIG_INCLUDE_PVS_SYSLOG=1 + GLOBAL_LINK_LIBS += $(LIBONLP_SO) -Wl,--unresolved-symbols=ignore-in-shared-libs -GLOBAL_LINK_LIBS += -lpthread -lm +GLOBAL_LINK_LIBS += -lpthread -lm -lrt include $(BUILDER)/targets.mk diff --git a/packages/base/any/onlp/src/onlp/module/inc/onlp/sys.h b/packages/base/any/onlp/src/onlp/module/inc/onlp/sys.h index 1d9029ee..8d12781f 100644 --- a/packages/base/any/onlp/src/onlp/module/inc/onlp/sys.h +++ b/packages/base/any/onlp/src/onlp/module/inc/onlp/sys.h @@ -1,21 +1,21 @@ /************************************************************ * - * - * Copyright 2014, 2015 Big Switch Networks, Inc. - * + * + * Copyright 2014, 2015 Big Switch Networks, Inc. + * * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/legal/epl-v10.html - * + * * 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. - * + * * ************************************************************ * @@ -96,12 +96,17 @@ int onlp_sys_vioctl(int code, va_list vargs); /** * @brief Start the platform management thread. */ -int onlp_sys_platform_manage_start(void); +int onlp_sys_platform_manage_start(int block); /** * @brief Stop the platform management thread. */ -int onlp_sys_platform_manage_stop(void); +int onlp_sys_platform_manage_stop(int block); + +/** + * @brief Join the platform management thread. + */ +int onlp_sys_platform_manage_join(void); /** * @brief Perform any pending platform management activities. diff --git a/packages/base/any/onlp/src/onlp/module/src/Makefile b/packages/base/any/onlp/src/onlp/module/src/Makefile index 3033edf5..27348ddb 100644 --- a/packages/base/any/onlp/src/onlp/module/src/Makefile +++ b/packages/base/any/onlp/src/onlp/module/src/Makefile @@ -1,28 +1,28 @@ ############################################################ # -# -# Copyright 2014, 2015 Big Switch Networks, Inc. -# +# +# Copyright 2014, 2015 Big Switch Networks, Inc. +# # Licensed under the Eclipse Public License, Version 1.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.eclipse.org/legal/epl-v10.html -# +# # 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. -# +# # ############################################################ # # Local source generation targets. # ############################################################ -include ../../../../init.mk +include $(ONL)/make/config.mk ucli: $(SUBMODULE_BIGCODE)/tools/uclihandlers.py onlp_ucli.c diff --git a/packages/base/any/onlp/src/onlp/module/src/onlp_main.c b/packages/base/any/onlp/src/onlp/module/src/onlp_main.c index f1343487..990d8a54 100644 --- a/packages/base/any/onlp/src/onlp/module/src/onlp_main.c +++ b/packages/base/any/onlp/src/onlp/module/src/onlp_main.c @@ -28,6 +28,10 @@ #include #include #include +#include +#include + +static void platform_manager_daemon__(const char* pidfile); /** * Human-readable SFP inventory. @@ -165,10 +169,12 @@ onlpdump_main(int argc, char* argv[]) int x = 0; int S = 0; int l = 0; + int M = 0; + char* pidfile = NULL; const char* O = NULL; const char* t = NULL; - while( (c = getopt(argc, argv, "srehdojmipxlSt:O:")) != -1) { + while( (c = getopt(argc, argv, "srehdojmM:ipxlSt:O:")) != -1) { switch(c) { case 's': show=1; break; @@ -180,6 +186,7 @@ onlpdump_main(int argc, char* argv[]) case 'o': o=1; break; case 'x': x=1; break; case 'm': m=1; break; + case 'M': M=1; pidfile = optarg; break; case 'i': i=1; break; case 'p': p=1; show=-1; break; case 't': t = optarg; break; @@ -190,6 +197,11 @@ onlpdump_main(int argc, char* argv[]) } } + if(M) { + platform_manager_daemon__(pidfile); + exit(0); + } + if(help) { printf("Usage: %s [OPTIONS]\n", argv[0]); printf(" -d Use dump(). This is the default.\n"); @@ -200,6 +212,7 @@ onlpdump_main(int argc, char* argv[]) printf(" -x Dump Platform Info only.\n"); printf(" -j Dump ONIE data in JSON format.\n"); printf(" -m Run platform manager.\n"); + printf(" -M Run as platform manager daemon.\n"); printf(" -i Iterate OIDs.\n"); printf(" -p Show SFP presence.\n"); printf(" -t Decode TlvInfo data.\n"); @@ -302,10 +315,10 @@ onlpdump_main(int argc, char* argv[]) if(m) { printf("Running the platform manager for 600 seconds...\n"); - onlp_sys_platform_manage_start(); + onlp_sys_platform_manage_start(0); sleep(600); printf("Stopping the platform manager.\n"); - onlp_sys_platform_manage_stop(); + onlp_sys_platform_manage_stop(1); } if(p) { @@ -323,3 +336,75 @@ onlpdump_main(int argc, char* argv[]) return 0; } + +#if AIM_CONFIG_INCLUDE_DAEMONIZE == 1 + +#include +#include +#include +#include + +void +sighandler__(int signal) +{ + onlp_sys_platform_manage_stop(0); +} + +static void +platform_manager_daemon__(const char* pidfile) +{ + aim_pvs_t* aim_pvs_syslog = NULL; + aim_daemon_restart_config_t rconfig; + aim_daemon_config_t config; + + memset(&config, 0, sizeof(config)); + aim_daemon_restart_config_init(&rconfig, 1, 1); + AIM_BITMAP_CLR(&rconfig.signal_restarts, SIGTERM); + AIM_BITMAP_CLR(&rconfig.exit_restarts, 0); + rconfig.maximum_restarts=50; + rconfig.pvs = NULL; + config.wd = "/"; + + aim_daemonize(&config, &rconfig); + aim_log_handler_basic_init_all("onlpd", + "/var/log/onlpd.log", + 1024*1024, + 99); + if(pidfile) { + FILE* fp = fopen(pidfile, "w"); + if(fp == NULL) { + int e = errno; + aim_printf(aim_pvs_syslog, "fatal: open(%s): %s\n", + pidfile, strerror(e)); + aim_printf(&aim_pvs_stderr, "fatal: open(%s): %s\n", + pidfile, strerror(e)); + + /* Don't attempt restart */ + raise(SIGTERM); + } + fprintf(fp, "%d\n", getpid()); + fclose(fp); + } + + /** Signal handler for terminating the platform manager */ + signal(SIGTERM, sighandler__); + + /** Start and block in platform manager. */ + onlp_sys_platform_manage_start(1); + + /** Terminated via signal. Cleanup and exit. */ + onlp_sys_platform_manage_stop(1); + + aim_log_handler_basic_denit_all(); + exit(0); +} + + +#else +static void +platform_manager_daemon__(const char* pidfile) +{ + fprintf(stderr, "Daemon mode not supported in this build."); + exit(1); +} +#endif diff --git a/packages/base/any/onlp/src/onlp/module/src/platform_manager.c b/packages/base/any/onlp/src/onlp/module/src/platform_manager.c index a2a884b2..16684832 100644 --- a/packages/base/any/onlp/src/onlp/module/src/platform_manager.c +++ b/packages/base/any/onlp/src/onlp/module/src/platform_manager.c @@ -228,7 +228,7 @@ onlp_sys_platform_manage_thread__(void* vctrl) } int -onlp_sys_platform_manage_start(void) +onlp_sys_platform_manage_start(int block) { onlp_sys_platform_manage_init(); @@ -250,22 +250,39 @@ onlp_sys_platform_manage_start(void) return -1; } + if(block) { + onlp_sys_platform_manage_join(); + } + return 0; } int -onlp_sys_platform_manage_stop(void) +onlp_sys_platform_manage_stop(int block) { if(control__.eventfd > 0) { uint64_t zero = 1; /* Tell the thread to exit */ write(control__.eventfd, &zero, sizeof(zero)); - /* Wait for the thread to terminate */ - pthread_join(control__.thread, NULL); + + if(block) { + onlp_sys_platform_manage_join(); + } } return 0; } +int +onlp_sys_platform_manage_join(void) +{ + if(control__.eventfd > 0) { + /* Wait for the thread to terminate */ + pthread_join(control__.thread, NULL); + close(control__.eventfd); + control__.eventfd = -1; + } + return 0; +} static int platform_psus_notify__(void) diff --git a/packages/base/any/onlp/src/onlpd.init b/packages/base/any/onlp/src/onlpd.init new file mode 100644 index 00000000..99165b56 --- /dev/null +++ b/packages/base/any/onlp/src/onlpd.init @@ -0,0 +1,67 @@ +#!/bin/sh + +### BEGIN INIT INFO +# Provides: onlpd +# Required-Start: $syslog +# Required-Stop: $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Start ONLP Platform Agent +# Description: ONLP PLATFORM AGENTX +### END INIT INFO + +PATH=/sbin:/bin:/usr/sbin:/usr/bin + +. /lib/lsb/init-functions + +DAEMON=/bin/onlpd +PIDFILE=/var/run/onlpd.pid +ONLP_SNMPD_OPTS="-M $PIDFILE" +QUIET= + +test -x $DAEMON || exit 5 + +RUNASUSER=root +UGID=$(getent passwd $RUNASUSER | cut -f 3,4 -d:) || true + +case $1 in + start) + log_daemon_msg "Starting ONLP Platform Agent" "onlpd" + if [ -z "$UGID" ]; then + log_failure_msg "user \"$RUNASUSER\" does not exist" + exit 1 + fi + start-stop-daemon --start $QUIET --oknodo --pidfile $PIDFILE --startas $DAEMON -- $ONLP_SNMPD_OPTS $ONLP_SNMPD_EXTRA_OPTS + status=$? + log_end_msg $status + ;; + stop) + log_daemon_msg "Stopping ONLP Platform Agent" "onlpd" + start-stop-daemon --stop $QUIET --oknodo --pidfile $PIDFILE + log_end_msg $? + rm -f $PIDFILE + ;; + restart|force-reload) + $0 stop && sleep 2 && $0 start + ;; + try-restart) + if $0 status >/dev/null; then + $0 restart + else + exit 0 + fi + ;; + reload) + log_daemon_msg "Reloading ONLP Platform Agent" "onlpd" + start-stop-daemon --stop $QUIET --oknodo --pidfile $PIDFILE --signal 1 + status=$? + log_end_msg $status + ;; + status) + status_of_proc $DAEMON "ONLP Platform Agent" + ;; + *) + echo "Usage: $0 {start|stop|restart|try-restart|force-reload|reload|status}" + exit 2 + ;; +esac