LCOV - code coverage report
Current view: top level - monetdb5/mal - mal_embedded.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 54 88 61.4 %
Date: 2021-09-14 22:17:06 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * (author) M.L. Kersten
      11             :  * These routines assume that the signatures for all MAL files are defined as text in mal_embedded.h
      12             :  * They are parsed upon system restart without access to their source files.
      13             :  * This way the definitions are part of the library upon compilation.
      14             :  * It assumes that all necessary libraries are already loaded.
      15             :  * A failure to bind the address in the context of an embedded version is not considered an error.
      16             :  */
      17             : 
      18             : #include "monetdb_config.h"
      19             : 
      20             : #include "mal_embedded.h"
      21             : #include "mal_builder.h"
      22             : #include "mal_stack.h"
      23             : #include "mal_linker.h"
      24             : #include "mal_session.h"
      25             : #include "mal_scenario.h"
      26             : #include "mal_parser.h"
      27             : #include "mal_interpreter.h"
      28             : #include "mal_namespace.h"  /* for initNamespace() */
      29             : #include "mal_client.h"
      30             : #include "mal_dataflow.h"
      31             : #include "mal_private.h"
      32             : #include "mal_runtime.h"
      33             : #include "mal_atom.h"
      34             : #include "mal_resource.h"
      35             : #include "mal_atom.h"
      36             : #include "msabaoth.h"
      37             : #include "mal_authorize.h"
      38             : #include "mal_profiler.h"
      39             : #include "mutils.h"
      40             : 
      41             : static bool embeddedinitialized = false;
      42             : 
      43             : str
      44           1 : malEmbeddedBoot(int workerlimit, int memorylimit, int querytimeout, int sessiontimeout, int with_mapi_server)
      45             : {
      46             :         Client c;
      47             :         str msg = MAL_SUCCEED;
      48             : 
      49           1 :         if( embeddedinitialized )
      50             :                 return MAL_SUCCEED;
      51             : 
      52             :         {
      53             :                 /* unlock the vault, first see if we can find the file which
      54             :                  * holds the secret */
      55             :                 char secret[1024];
      56             :                 char *secretp = secret;
      57             :                 FILE *secretf;
      58             :                 size_t len;
      59             : 
      60           1 :                 if (GDKinmemory(0) || GDKgetenv("monet_vault_key") == NULL) {
      61             :                         /* use a default (hard coded, non safe) key */
      62           1 :                         snprintf(secret, sizeof(secret), "%s", "Xas632jsi2whjds8");
      63             :                 } else {
      64           0 :                         if ((secretf = MT_fopen(GDKgetenv("monet_vault_key"), "r")) == NULL) {
      65           0 :                                 throw(MAL, "malEmbeddedBoot",
      66             :                                         "unable to open vault_key_file %s: %s\n",
      67           0 :                                         GDKgetenv("monet_vault_key"), strerror(errno));
      68             :                         }
      69           0 :                         len = fread(secret, 1, sizeof(secret), secretf);
      70           0 :                         secret[len] = '\0';
      71           0 :                         len = strlen(secret); /* secret can contain null-bytes */
      72           0 :                         if (len == 0) {
      73           0 :                                 throw(MAL, "malEmbeddedBoot", "vault key has zero-length!\n");
      74           0 :                         } else if (len < 5) {
      75           0 :                                 throw(MAL, "malEmbeddedBoot",
      76             :                                         "#warning: your vault key is too short "
      77             :                                         "(%zu), enlarge your vault key!\n", len);
      78             :                         }
      79           0 :                         fclose(secretf);
      80             :                 }
      81           1 :                 if ((msg = AUTHunlockVault(secretp)) != MAL_SUCCEED) {
      82             :                         /* don't show this as a crash */
      83             :                         return msg;
      84             :                 }
      85             :         }
      86           1 :         if ((msg = AUTHinitTables(NULL)) != MAL_SUCCEED)
      87             :                 return msg;
      88             : 
      89           1 :         if (!MCinit())
      90           0 :                 throw(MAL, "malEmbeddedBoot", "MAL debugger failed to start");
      91             : #ifndef NDEBUG
      92           1 :         if (!mdbInit()) {
      93           0 :                 mal_client_reset();
      94           0 :                 throw(MAL, "malEmbeddedBoot", "MAL debugger failed to start");
      95             :         }
      96             : #endif
      97             :         // monet_memory = MT_npages() * MT_pagesize();
      98           1 :         initNamespace();
      99           1 :         initParser();
     100           1 :         initHeartbeat();
     101             :         // initResource();
     102             : 
     103           1 :         c = MCinitClient((oid) 0, 0, 0);
     104           1 :         if(c == NULL)
     105           0 :                 throw(MAL, "malEmbeddedBoot", "Failed to initialize client");
     106           1 :         c->workerlimit = workerlimit;
     107           1 :         c->memorylimit = memorylimit;
     108           1 :         c->querytimeout = querytimeout * 1000000;    // from sec to usec
     109           1 :         c->sessiontimeout = sessiontimeout * 1000000;
     110           1 :         c->curmodule = c->usermodule = userModule();
     111           1 :         if(c->usermodule == NULL) {
     112           0 :                 MCcloseClient(c);
     113           0 :                 throw(MAL, "malEmbeddedBoot", "Failed to initialize client MAL module");
     114             :         }
     115           1 :         if ( (msg = defaultScenario(c)) ) {
     116           0 :                 MCcloseClient(c);
     117           0 :                 return msg;
     118             :         }
     119           1 :         if ((msg = MSinitClientPrg(c, "user", "main")) != MAL_SUCCEED) {
     120           0 :                 MCcloseClient(c);
     121           0 :                 return msg;
     122             :         }
     123           1 :         char *modules[5] = { "embedded", "sql", "generator", "udf" };
     124           1 :         if ((msg = malIncludeModules(c, modules, 0, !with_mapi_server)) != MAL_SUCCEED) {
     125           0 :                 MCcloseClient(c);
     126           0 :                 return msg;
     127             :         }
     128           1 :         pushEndInstruction(c->curprg->def);
     129           1 :         msg = chkProgram(c->usermodule, c->curprg->def);
     130           1 :         if ( msg != MAL_SUCCEED || (msg= c->curprg->def->errors) != MAL_SUCCEED ) {
     131           0 :                 MCcloseClient(c);
     132           0 :                 return msg;
     133             :         }
     134           1 :         msg = MALengine(c);
     135           1 :         if (msg == MAL_SUCCEED)
     136           1 :                 embeddedinitialized = true;
     137           1 :         MCcloseClient(c);
     138           1 :         initProfiler();
     139           1 :         return msg;
     140             : }
     141             : 
     142             : /*
     143             :  * Upon exit we should attempt to remove all allocated memory explicitly.
     144             :  * This seemingly superflous action is necessary to simplify analyis of
     145             :  * memory leakage problems later ons and to allow an embedded server to
     146             :  * restart the server properly.
     147             :  *
     148             :  * It is the responsibility of the enclosing application to finish/cease all
     149             :  * activity first.
     150             :  * This function should be called after you have issued sql_reset();
     151             :  */
     152             : 
     153             : void
     154           1 : malEmbeddedReset(void) //remove extra modules and set to non-initialized again
     155             : {
     156           1 :         if (!embeddedinitialized)
     157             :                 return;
     158             : 
     159           1 :         GDKprepareExit();
     160           1 :         MCstopClients(0);
     161           1 :         setHeartbeat(-1);
     162           1 :         stopProfiler(0);
     163           1 :         AUTHreset();
     164           1 :         if (!GDKinmemory(0) && !GDKembedded()) {
     165             :                 str err = 0;
     166             : 
     167           0 :                 if ((err = msab_wildRetreat()) != NULL) {
     168           0 :                         TRC_ERROR(MAL_SERVER, "%s\n", err);
     169           0 :                         free(err);
     170             :                 }
     171           0 :                 if ((err = msab_registerStop()) != NULL) {
     172           0 :                         TRC_ERROR(MAL_SERVER, "%s\n", err);
     173           0 :                         free(err);
     174             :                 }
     175             :         }
     176           1 :         mal_factory_reset();
     177           1 :         mal_dataflow_reset();
     178           1 :         mal_client_reset();
     179           1 :         mal_linker_reset();
     180           1 :         mal_resource_reset();
     181           1 :         mal_runtime_reset();
     182           1 :         mal_module_reset();
     183           1 :         mal_atom_reset();
     184             : #ifndef NDEBUG
     185           1 :         mdbExit();
     186             : #endif
     187             : 
     188           1 :         memset((char*)monet_cwd, 0, sizeof(monet_cwd));
     189           1 :         memset((char*)monet_characteristics,0, sizeof(monet_characteristics));
     190           1 :         mal_namespace_reset();
     191             :         /* No need to clean up the namespace, it will simply be extended
     192             :          * upon restart mal_namespace_reset(); */
     193           1 :         GDKreset(0);    // terminate all other threads
     194           1 :         embeddedinitialized = false;
     195             : }
     196             : 
     197             : /* stopping clients should be done with care, as they may be in the mids of
     198             :  * transactions. One safe place is between MAL instructions, which would
     199             :  * abort the transaction by raising an exception. All sessions are
     200             :  * terminate this way.
     201             :  * We should also ensure that no new client enters the scene while shutting down.
     202             :  * For this we mark the client records as BLOCKCLIENT.
     203             :  *
     204             :  * Beware, mal_exit is also called during a SIGTERM from the monetdb tool
     205             :  */
     206             : 
     207             : void
     208           0 : malEmbeddedStop(int status)
     209             : {
     210           0 :         malEmbeddedReset();
     211           0 :         exit(status); /* properly end GDK */
     212             : }

Generated by: LCOV version 1.14