/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2022 Olof Hagsand and Kristofer Hallin Sponsored by Siklu Communications LTD This file is part of CLIXON. 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. Alternatively, the contents of this file may be used under the terms of the GNU General Public License Version 3 or later (the "GPL"), in which case the provisions of the GPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of the GPL, and not to allow others to use your version of this file under the terms of Apache License version 2, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the Apache License version 2 or the GPL. ***** END LICENSE BLOCK ***** * This is the clixon_snmp daemon * It assumes a netsnmp damon is running. * - If netsnmp does not run, clixon_snmp will not start * - If netsnmp dies, clixon_snmp will exit * - If netsnmp is restarted, clixon_snmp should also be restarted * It is possible to be more resilient, such as setting a timer and trying again, in fact, libnetsnmp * has some such mechanisms but these are NOT implemented * @see RFC 6643 Translation of Structure of Management Information Version 2 (SMIv2) * MIB Modules to YANG Modules */ #ifdef HAVE_CONFIG_H #include "clixon_config.h" /* generated by config & autoconf */ #endif #include #include #include #include #include #include #include #include /* net-snmp */ #include #include #include /* cligen */ #include /* clixon */ #include #include "snmp_lib.h" #include "snmp_register.h" #include "snmp_stream.h" /* Command line options to be passed to getopt(3) */ #define SNMP_OPTS "hVD:f:l:C:o:z" /* Forward */ static int clixon_snmp_input_cb(int s, void *arg); /*! Return (hardcoded) pid file */ static char* clicon_snmp_pidfile(clixon_handle h) { return "/var/tmp/clixon_snmp.pid"; } /*! Signal terminates process * * Just set exit flag for proper exit in event loop */ static void clixon_snmp_sig_term(int arg) { clixon_log(NULL, LOG_NOTICE, "%s: %s: pid: %u Signal %d", __PROGRAM__, __FUNCTION__, getpid(), arg); /* This should ensure no more accepts or incoming packets are processed because next time eventloop * is entered, it will terminate. * However there may be a case of sockets closing rather abruptly for clients */ clixon_exit_set(1); } /*! Clean and close all state of netconf process (but dont exit). * * Cannot use h after this * @param[in] h Clixon handle */ static int snmp_terminate(clixon_handle h) { yang_stmt *yspec; cvec *nsctx; cxobj *x = NULL; char *pidfile = clicon_snmp_pidfile(h); clixon_snmp_stream_shutdown(h); snmp_shutdown(__FUNCTION__); shutdown_agent(); clixon_snmp_api_agent_cleanup(); if (clicon_ptr_get(h, "snmp-rowstatus-tree", (void**)&x) == 0 && x){ xml_free(x); x = NULL; } clicon_rpc_close_session(h); yang_exit(h); if ((nsctx = clicon_nsctx_global_get(h)) != NULL) cvec_free(nsctx); if ((x = clicon_conf_xml(h)) != NULL) xml_free(x); xpath_optimize_exit(); clixon_event_exit(); clixon_handle_exit(h); clixon_err_exit(); clixon_log_exit(); if (pidfile) unlink(pidfile); return 0; } /*! Get which sockets are used from SNMP API, the register single sockets into clixon event system * * @param[in] h Clixon handle * @param[in] register If 1 register snmp sockets with event handler. If 0 close and unregister * @retval 0 OK * @retval -1 Error * This is a workaround for netsnmps API usiing fdset:s, instead an fdset is created before calling * the snmp api * if you use select(), see snmp_select_info() in snmp_api(3) * snmp_select_info(int *numfds, fd_set *fdset, struct timeval *timeout, int *block) * @see clixon_snmp_input_cb */ static int clixon_snmp_fdset_register(clixon_handle h, int regfd) { int retval = -1; int numfds = 0; fd_set readfds; struct timeval timeout = { LONG_MAX, 0 }; int block = 0; int nr; int s; FD_ZERO(&readfds); if ((nr = snmp_sess_select_info(NULL, &numfds, &readfds, &timeout, &block)) < 0){ clixon_err(OE_XML, errno, "snmp_select_error"); goto done; } /* eg 4, 6, 8 */ for (s=0; s \tDebug level (see available levels below)\n" "\t-f \tConfiguration file (mandatory)\n" "\t-l (e|o|s|f) Log on std(e)rr, std(o)ut, (s)yslog(default), (f)ile\n" "\t-C \tDump configuration options on stdout after loading. Format is xml|json|text\n" "\t-z\t\tKill other %s daemon and exit\n" "\t-o \"