"Fossies" - the Fresh Open Source Software Archive

Member "opensaf-5.21.09/src/base/ncs_main_pub.c" (14 Sep 2021, 14490 Bytes) of package /linux/misc/opensaf-5.21.09.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ncs_main_pub.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.21.06_vs_5.21.09.

    1 /*      -*- OpenSAF  -*-
    2  *
    3  * (C) Copyright 2008 The OpenSAF Foundation
    4  *
    5  * This program is distributed in the hope that it will be useful, but
    6  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    7  * or FITNESS FOR A PARTICULAR PURPOSE. This file and program are licensed
    8  * under the GNU Lesser General Public License Version 2.1, February 1999.
    9  * The complete license can be accessed from the following location:
   10  * http://opensource.org/licenses/lgpl-license.php
   11  * See the Copying file included with the OpenSAF distribution for full
   12  * licensing terms.
   13  *
   14  * Author(s): Emerson Network Power
   15  *
   16  */
   17 
   18 /*****************************************************************************
   19 ..............................................................................
   20 
   21   MODULE NAME:       ncs_main_pub.c
   22 
   23   DESCRIPTION:       Contains an API that agent starts up NCS.
   24 
   25   TRACE GUIDE:
   26   Policy is to not use logging/syslog from library code.
   27   Only the trace part of logtrace is used from library.
   28 
   29   It is possible to turn on trace for the IMMA library used
   30   by an application process. This is done by the application
   31   defining the environment variable: IMMA_TRACE_PATHNAME.
   32   The trace will end up in the file defined by that variable.
   33 
   34   TRACE   debug traces                 - aproximates DEBUG
   35   TRACE_1 normal but important events  - aproximates INFO.
   36   TRACE_2 user errors with return code - aproximates NOTICE.
   37   TRACE_3 unusual or strange events    - aproximates WARNING
   38   TRACE_4 library errors ERR_LIBRARY   - aproximates ERROR
   39 ******************************************************************************
   40 */
   41 
   42 #include "osaf/configmake.h"
   43 
   44 #include <pthread.h>
   45 #include <dlfcn.h>
   46 
   47 #include "base/ncsgl_defs.h"
   48 #include "mds/mds_papi.h"
   49 #include "base/ncs_main_papi.h"
   50 #include "base/ncs_mda_papi.h"
   51 #include "base/ncs_lib.h"
   52 #include "base/ncssysf_lck.h"
   53 #include "mds/mds_dl_api.h"
   54 #include "sprr_dl_api.h"
   55 #include "mds/mda_dl_api.h"
   56 #include "base/ncssysf_def.h"
   57 #include "base/ncs_main_pub.h"
   58 #include "base/osaf_utility.h"
   59 
   60 /**************************************************************************\
   61 
   62        L O C A L      D A T A    S T R U C T U R E S
   63 
   64 \**************************************************************************/
   65 
   66 typedef uint32_t (*LIB_REQ)(NCS_LIB_REQ_INFO *);
   67 
   68 typedef struct ncs_agent_data {
   69     uint32_t use_count;
   70     LIB_REQ lib_req;
   71 } NCS_AGENT_DATA;
   72 
   73 typedef struct ncs_main_pub_cb {
   74     void *lib_hdl;
   75 
   76     NCS_LOCK lock;
   77     bool core_started;
   78     uint32_t my_nodeid;
   79     uint32_t my_procid;
   80 
   81     uint32_t core_use_count;
   82     uint32_t leap_use_count;
   83     uint32_t mds_use_count;
   84 
   85     NCS_AGENT_DATA mbca;
   86 } NCS_MAIN_PUB_CB;
   87 
   88 static uint32_t mainget_node_id(uint32_t *node_id);
   89 static uint32_t ncs_non_core_agents_startup(void);
   90 
   91 static NCS_MAIN_PUB_CB gl_ncs_main_pub_cb;
   92 
   93 /* mutex for synchronising agent startup and shutdown */
   94 static pthread_mutex_t s_agent_startup_mutex = PTHREAD_MUTEX_INITIALIZER;
   95 
   96 /* mutex protecting gl_ncs_main_pub_cb.core_started and
   97  * gl_ncs_main_pub_cb.core_use_count  */
   98 static pthread_mutex_t s_leap_core_mutex = PTHREAD_MUTEX_INITIALIZER;
   99 
  100 /***************************************************************************\
  101 
  102   PROCEDURE    :    ncs_agents_startup
  103 
  104 \***************************************************************************/
  105 unsigned int ncs_agents_startup(void)
  106 {
  107     uint32_t rc = ncs_core_agents_startup();
  108     if (rc != NCSCC_RC_SUCCESS)
  109         return rc;
  110 
  111     rc = ncs_non_core_agents_startup();
  112     return rc;
  113 }
  114 
  115 /***************************************************************************\
  116 
  117   PROCEDURE    :    ncs_agents_shutdown
  118 
  119 \***************************************************************************/
  120 unsigned int ncs_agents_shutdown(void)
  121 {
  122     ncs_mbca_shutdown();
  123     ncs_core_agents_shutdown();
  124 
  125     return NCSCC_RC_SUCCESS;
  126 }
  127 
  128 /***************************************************************************\
  129 
  130   PROCEDURE    :    ncs_leap_startup
  131 
  132 \***************************************************************************/
  133 unsigned int ncs_leap_startup(void)
  134 {
  135     NCS_LIB_REQ_INFO lib_create;
  136     NCS_LIB_REQ_INFO lib_destroy;
  137 
  138     osaf_mutex_lock_ordie(&s_agent_startup_mutex);
  139 
  140     if (gl_ncs_main_pub_cb.leap_use_count > 0) {
  141         gl_ncs_main_pub_cb.leap_use_count++;
  142         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  143         return NCSCC_RC_SUCCESS;
  144     }
  145 
  146     /* Print the process-id for information sakes */
  147     gl_ncs_main_pub_cb.my_procid = (uint32_t)getpid();
  148     TRACE("\nNCS:PROCESS_ID=%d\n", gl_ncs_main_pub_cb.my_procid);
  149 
  150     memset(&lib_create, 0, sizeof(lib_create));
  151     lib_create.i_op = NCS_LIB_REQ_CREATE;
  152     lib_create.info.create.argc = 0;
  153     lib_create.info.create.argv = NULL;
  154 
  155     /* Initalize basic services */
  156     if (leap_env_init() != NCSCC_RC_SUCCESS) {
  157         TRACE_4("\nERROR: Couldn't initialised LEAP basic services \n");
  158         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  159         return NCSCC_RC_FAILURE;
  160     }
  161 
  162     if (sprr_lib_req(&lib_create) != NCSCC_RC_SUCCESS) {
  163         TRACE_4("\nERROR: SPRR lib_req NCS_LIB_REQ_CREATE failed \n");
  164         goto lib_req_fail;
  165     }
  166 
  167     /* Get & Update system specific arguments */
  168     if (mainget_node_id(&gl_ncs_main_pub_cb.my_nodeid) !=
  169         NCSCC_RC_SUCCESS) {
  170         TRACE_4("Not able to get the NODE ID\n");
  171         goto get_node_fail;
  172     }
  173     TRACE("NCS:NODE_ID=0x%08X\n", gl_ncs_main_pub_cb.my_nodeid);
  174     gl_ncs_main_pub_cb.leap_use_count = 1;
  175 
  176     osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  177 
  178     /* start initializing all the required agents */
  179     gl_ncs_main_pub_cb.lib_hdl = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
  180 
  181     return NCSCC_RC_SUCCESS;
  182 
  183 get_node_fail:
  184     memset(&lib_destroy, 0, sizeof(lib_destroy));
  185     lib_destroy.i_op = NCS_LIB_REQ_DESTROY;
  186     lib_destroy.info.destroy.dummy = 0;
  187     if (sprr_lib_req(&lib_destroy) != NCSCC_RC_SUCCESS)
  188         TRACE_4("\nERROR: SPRR lib_req NCS_LIB_REQ_DESTROY failed\n");
  189 
  190 lib_req_fail:
  191     leap_env_destroy();
  192     osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  193     return NCSCC_RC_FAILURE;
  194 }
  195 
  196 /***************************************************************************\
  197 
  198   PROCEDURE    :    ncs_mds_startup
  199 
  200 \***************************************************************************/
  201 unsigned int ncs_mds_startup(void)
  202 {
  203     NCS_LIB_REQ_INFO lib_create;
  204 
  205     osaf_mutex_lock_ordie(&s_agent_startup_mutex);
  206 
  207     if (!gl_ncs_main_pub_cb.leap_use_count) {
  208         TRACE_4("\nLEAP core not yet started.... \n");
  209         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  210         return NCSCC_RC_FAILURE;
  211     }
  212 
  213     if (gl_ncs_main_pub_cb.mds_use_count > 0) {
  214         gl_ncs_main_pub_cb.mds_use_count++;
  215         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  216         return NCSCC_RC_SUCCESS;
  217     }
  218 
  219     memset(&lib_create, 0, sizeof(lib_create));
  220     lib_create.i_op = NCS_LIB_REQ_CREATE;
  221     lib_create.info.create.argc = 0;
  222     lib_create.info.create.argv = NULL;
  223 
  224     /* STEP : Initialize the MDS layer */
  225     if (mds_lib_req(&lib_create) != NCSCC_RC_SUCCESS) {
  226         TRACE_4("ERROR: MDS lib_req failed \n");
  227         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  228         return NCSCC_RC_FAILURE;
  229     }
  230 
  231     /* STEP : Initialize the ADA/VDA layer */
  232     if (mda_lib_req(&lib_create) != NCSCC_RC_SUCCESS) {
  233         TRACE_4("ERROR: MDA lib_req failed \n");
  234         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  235         return NCSCC_RC_FAILURE;
  236     }
  237 
  238     gl_ncs_main_pub_cb.mds_use_count = 1;
  239 
  240     osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  241 
  242     return NCSCC_RC_SUCCESS;
  243 }
  244 
  245 /***************************************************************************\
  246 
  247   PROCEDURE    :    ncs_non_core_agents_startup
  248 
  249 \***************************************************************************/
  250 uint32_t ncs_non_core_agents_startup(void)
  251 {
  252     uint32_t rc = ncs_mbca_startup();
  253 
  254     return rc;
  255 }
  256 
  257 /***************************************************************************\
  258 
  259   PROCEDURE    :    ncs_core_agents_startup
  260 
  261 \***************************************************************************/
  262 unsigned int ncs_core_agents_startup(void)
  263 {
  264     osaf_mutex_lock_ordie(&s_leap_core_mutex);
  265 
  266     if (gl_ncs_main_pub_cb.core_use_count) {
  267         gl_ncs_main_pub_cb.core_use_count++;
  268         osaf_mutex_unlock_ordie(&s_leap_core_mutex);
  269         return NCSCC_RC_SUCCESS;
  270     }
  271 
  272     if (ncs_leap_startup() != NCSCC_RC_SUCCESS) {
  273         TRACE_4("ERROR: LEAP svcs startup failed \n");
  274         osaf_mutex_unlock_ordie(&s_leap_core_mutex);
  275         return m_LEAP_DBG_SINK(NCSCC_RC_FAILURE);
  276     }
  277 
  278     if (ncs_mds_startup() != NCSCC_RC_SUCCESS) {
  279         TRACE_4("ERROR: MDS startup failed \n");
  280         osaf_mutex_unlock_ordie(&s_leap_core_mutex);
  281         return m_LEAP_DBG_SINK(NCSCC_RC_FAILURE);
  282     }
  283 
  284     gl_ncs_main_pub_cb.core_started = true;
  285     gl_ncs_main_pub_cb.core_use_count = 1;
  286 
  287     osaf_mutex_unlock_ordie(&s_leap_core_mutex);
  288     return NCSCC_RC_SUCCESS;
  289 }
  290 
  291 /***************************************************************************\
  292 
  293   PROCEDURE    :    ncs_mbca_startup
  294 
  295 \***************************************************************************/
  296 unsigned int ncs_mbca_startup(void)
  297 {
  298     NCS_LIB_REQ_INFO lib_create;
  299 
  300     if (!gl_ncs_main_pub_cb.core_started) {
  301         TRACE_4("\nNCS core not yet started.... \n");
  302         return NCSCC_RC_FAILURE;
  303     }
  304 
  305     memset(&lib_create, 0, sizeof(lib_create));
  306     lib_create.i_op = NCS_LIB_REQ_CREATE;
  307     lib_create.info.create.argc = 0;
  308     lib_create.info.create.argv = NULL;
  309 
  310     if (gl_ncs_main_pub_cb.lib_hdl == NULL)
  311         return NCSCC_RC_SUCCESS; /* No agents to load */
  312 
  313     osaf_mutex_lock_ordie(&s_agent_startup_mutex);
  314 
  315     if (gl_ncs_main_pub_cb.mbca.use_count > 0) {
  316         /* Already created, so just increment the use_count */
  317         gl_ncs_main_pub_cb.mbca.use_count++;
  318     } else {
  319         gl_ncs_main_pub_cb.mbca.lib_req =
  320             (LIB_REQ)dlsym(gl_ncs_main_pub_cb.lib_hdl, "mbcsv_lib_req");
  321         if (gl_ncs_main_pub_cb.mbca.lib_req == NULL) {
  322             TRACE_4("\nMBCSV:MBCA:OFF");
  323         } else {
  324             if ((*gl_ncs_main_pub_cb.mbca.lib_req)(&lib_create) !=
  325                 NCSCC_RC_SUCCESS) {
  326                 osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  327                 return m_LEAP_DBG_SINK(NCSCC_RC_FAILURE);
  328             } else {
  329                 TRACE("\nMBCSV:MBCA:ON");
  330                 gl_ncs_main_pub_cb.mbca.use_count = 1;
  331             }
  332         }
  333     }
  334 
  335     osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  336 
  337     return NCSCC_RC_SUCCESS;
  338 }
  339 
  340 /***************************************************************************\
  341 
  342   PROCEDURE    :    ncs_mbca_shutdown
  343 
  344 \***************************************************************************/
  345 unsigned int ncs_mbca_shutdown(void)
  346 {
  347     NCS_LIB_REQ_INFO lib_destroy;
  348     uint32_t rc = NCSCC_RC_SUCCESS;
  349 
  350     osaf_mutex_lock_ordie(&s_agent_startup_mutex);
  351     if (gl_ncs_main_pub_cb.mbca.use_count > 1) {
  352         /* Still users extis, so just decrement the use_count */
  353         gl_ncs_main_pub_cb.mbca.use_count--;
  354         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  355         return rc;
  356     }
  357 
  358     memset(&lib_destroy, 0, sizeof(lib_destroy));
  359     lib_destroy.i_op = NCS_LIB_REQ_DESTROY;
  360     lib_destroy.info.destroy.dummy = 0;
  361 
  362     if (gl_ncs_main_pub_cb.mbca.lib_req != NULL)
  363         rc = (*gl_ncs_main_pub_cb.mbca.lib_req)(&lib_destroy);
  364 
  365     gl_ncs_main_pub_cb.mbca.use_count = 0;
  366     gl_ncs_main_pub_cb.mbca.lib_req = NULL;
  367 
  368     osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  369 
  370     return rc;
  371 }
  372 
  373 /***************************************************************************\
  374 
  375   PROCEDURE    :    ncs_leap_shutdown
  376 
  377 \***************************************************************************/
  378 void ncs_leap_shutdown()
  379 {
  380     NCS_LIB_REQ_INFO lib_destroy;
  381 
  382     osaf_mutex_lock_ordie(&s_agent_startup_mutex);
  383 
  384     if (gl_ncs_main_pub_cb.leap_use_count > 1) {
  385         /* Still users extis, so just decrement the use_count */
  386         gl_ncs_main_pub_cb.leap_use_count--;
  387         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  388         return;
  389     }
  390 
  391     memset(&lib_destroy, 0, sizeof(lib_destroy));
  392     lib_destroy.i_op = NCS_LIB_REQ_DESTROY;
  393     lib_destroy.info.destroy.dummy = 0;
  394 
  395     dlclose(gl_ncs_main_pub_cb.lib_hdl);
  396     gl_ncs_main_pub_cb.lib_hdl = NULL;
  397 
  398     sprr_lib_req(&lib_destroy);
  399 
  400     leap_env_destroy();
  401 
  402     gl_ncs_main_pub_cb.leap_use_count = 0;
  403     gl_ncs_main_pub_cb.core_started = false;
  404 
  405     osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  406 }
  407 
  408 /***************************************************************************\
  409 
  410   PROCEDURE    :    ncs_mds_shutdown
  411 
  412 \***************************************************************************/
  413 void ncs_mds_shutdown()
  414 {
  415     NCS_LIB_REQ_INFO lib_destroy;
  416 
  417     osaf_mutex_lock_ordie(&s_agent_startup_mutex);
  418 
  419     if (gl_ncs_main_pub_cb.mds_use_count > 1) {
  420         /* Still users extis, so just decrement the use_count */
  421         gl_ncs_main_pub_cb.mds_use_count--;
  422         osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  423         return;
  424     }
  425 
  426     memset(&lib_destroy, 0, sizeof(lib_destroy));
  427     lib_destroy.i_op = NCS_LIB_REQ_DESTROY;
  428     lib_destroy.info.destroy.dummy = 0;
  429 
  430     mda_lib_req(&lib_destroy);
  431     mds_lib_req(&lib_destroy);
  432 
  433     gl_ncs_main_pub_cb.mds_use_count = 0;
  434     gl_ncs_main_pub_cb.core_started = false;
  435 
  436     osaf_mutex_unlock_ordie(&s_agent_startup_mutex);
  437 }
  438 
  439 /***************************************************************************\
  440 
  441   PROCEDURE    :    ncs_core_agents_shutdown
  442 
  443 \***************************************************************************/
  444 unsigned int ncs_core_agents_shutdown()
  445 {
  446     osaf_mutex_lock_ordie(&s_leap_core_mutex);
  447 
  448     if (!gl_ncs_main_pub_cb.core_use_count) {
  449         TRACE_4("\nNCS core not yet started.... \n");
  450         osaf_mutex_unlock_ordie(&s_leap_core_mutex);
  451         return NCSCC_RC_FAILURE;
  452     }
  453 
  454     if (gl_ncs_main_pub_cb.core_use_count > 1) {
  455         /* Decrement the use count */
  456         gl_ncs_main_pub_cb.core_use_count--;
  457         osaf_mutex_unlock_ordie(&s_leap_core_mutex);
  458         return NCSCC_RC_SUCCESS;
  459     }
  460 
  461     /* Shutdown basic services */
  462     ncs_mds_shutdown();
  463     ncs_leap_shutdown();
  464     gl_ncs_main_pub_cb.core_use_count = 0;
  465 
  466     osaf_mutex_unlock_ordie(&s_leap_core_mutex);
  467     return NCSCC_RC_SUCCESS;
  468 }
  469 
  470 /***************************************************************************\
  471 
  472   PROCEDURE    :    ncs_get_node_id
  473 
  474 \***************************************************************************/
  475 NCS_NODE_ID ncs_get_node_id(void)
  476 {
  477     if (!gl_ncs_main_pub_cb.leap_use_count) {
  478         return m_LEAP_DBG_SINK(0);
  479     }
  480 
  481     return gl_ncs_main_pub_cb.my_nodeid;
  482 }
  483 
  484 uint32_t mainget_node_id(uint32_t *node_id)
  485 {
  486     uint32_t res = NCSCC_RC_FAILURE;
  487 
  488     FILE *fp = fopen(PKGLOCALSTATEDIR "/node_id", "r");
  489 
  490     if (fp != NULL) {
  491         if (fscanf(fp, "%x", node_id) == 1) {
  492             res = NCSCC_RC_SUCCESS;
  493         }
  494         fclose(fp);
  495     }
  496 
  497     return res;
  498 }
  499 
  500 /***************************************************************************\
  501 
  502   PROCEDURE    :    ncs_util_search_argv_list
  503 
  504 \***************************************************************************/
  505 char *ncs_util_search_argv_list(int argc, char *argv[], char *arg_prefix)
  506 {
  507     /* Check   argv[argc-1] through argv[1] */
  508     for (; argc > 1; argc--) {
  509         char *tmp = strstr(argv[argc - 1], arg_prefix);
  510         if (tmp != NULL)
  511             return tmp;
  512     }
  513     return NULL;
  514 }