Ejemplo n.º 1
0
/*
 *----------------------------------------------------------------------
 *
 * OS_Accept --
 *
 *  Accepts a new FastCGI connection.  This routine knows whether
 *  we're dealing with TCP based sockets or NT Named Pipes for IPC.
 *
 *  fail_on_intr is ignored in the Win lib.
 *
 * Results:
 *      -1 if the operation fails, otherwise this is a valid IPC fd.
 *
 *----------------------------------------------------------------------
 */
int OS_Accept(int listen_sock, int fail_on_intr, const char *webServerAddrs)
{
    int ipcFd = -1;

    // Touch args to prevent warnings
    listen_sock = 0; fail_on_intr = 0;

    // @todo Muliple listen sockets and sockets other than 0 are not
    // supported due to the use of globals.

    if (shutdownPending) 
    {
        OS_LibShutdown();
        return -1;
    }

    // The mutex is to keep other processes (and threads, when supported)
    // from going into the accept cycle.  The accept cycle needs to
    // periodically break out to check the state of the shutdown flag
    // and there's no point to having more than one thread do that.
    
    if (acceptMutex != INVALID_HANDLE_VALUE) 
    {
        if (WaitForSingleObject(acceptMutex, INFINITE) == WAIT_FAILED) 
		
        {
            printLastError("WaitForSingleObject() failed");
            return -1;
        }
    }
    
    if (shutdownPending) 
    {
        OS_LibShutdown();
    }
    else if (listenType == FD_PIPE_SYNC) 
    {
        ipcFd = acceptNamedPipe();
    }
    else if (listenType == FD_SOCKET_SYNC)
    {
        ipcFd = acceptSocket(webServerAddrs);
    }
    else
    {
        fprintf(stderr, "unknown listenType (%d)\n", listenType);
    }
	    
    if (acceptMutex != INVALID_HANDLE_VALUE) 
    {
        ReleaseMutex(acceptMutex);
    }

    return ipcFd;
}
Ejemplo n.º 2
0
System::~System()
{
	OS_LibShutdown(); // Fixes a memory leak in FastCGI

	DatabaseSystem::deleteDbmsList();
	CodeHighlighter::deleteCodeHighlighters();
	Shell::deleteCommands();
	Session::deleteSessions();
	RequestProfile::deleteProfiles();

	if (_site != nullptr)
		delete _site;
	delete TransManager::get();
	delete TemplatesManager::get();
	for (Plugin* plugin : _loadedPlugins)
		delete plugin;
	delete _cliOptions;
}
Ejemplo n.º 3
0
void api_cleanup( void )
{
    /* free the memory used by the FastCGI library */
    OS_LibShutdown(  );

    /* free the pointers to the methods hash table */
    if ( methods.initialized == 1 ) {
	nbu_log_debug( "freeing methods hash table" );
	ewf_hashtable_destroy( &methods.list );
    }

    /* disconnect from nbd */
    nbd_disconnect(  );

#ifdef DMALLOC
    dmalloc_shutdown(  );
#endif

    nbu_log_info( "api stopped" );
    nbu_log_close(  );
}
Ejemplo n.º 4
0
int main(int argc, char *argv[])
{
    argv = cmdline_setup(argc, argv);
    if (!argv) {
        fprintf(stderr, "Failed to setup cmdline library.\n");
        return EXIT_FAILURE;
    }

    // Initialize timezone before going multi-threaded.
    tzset();

    // Initialize weak (but fast) PRG rand(3).
    randomize();

#if HAVE_OPENSSL_OPENSSLV_H
    // Initialize OpenSSL.
    OpenSSL_add_all_algorithms();
    if (SSL_library_init() != 1) {
        fprintf(stderr, "Failed to initialize OpenSSL.\n");
        return false;
    }
#endif

#if HAVE_CURL_CURL_H_
    // Initialize libcurl.
    if (curl_global_init(CURL_GLOBAL_ALL)) {
        fprintf(stderr, "Failed to initialize libcurl.\n");
        return EXIT_FAILURE;
    }
#endif

#if HAVE_LIBXML_XMLVERSION_H
    // Initialize XML.
    if (!flinter::Xml::Initialize()) {
        fprintf(stderr, "Failed to initialize libxml2.\n");
        return false;
    }
#endif

    // Initialize FastCGI.
    if (FCGX_Init()) {
        fprintf(stderr, "Failed to initialize FastCGI.\n");
        return false;
    }

    // Initialize ClearSilver.
    // Nothing to do.

    int ret = fastcgi_main(argc, argv);

#if HAVE_NERR_SHUTDOWN
    // ClearSilver leaks.
    NEOERR *err = nerr_shutdown();
    if (err != STATUS_OK) {
        nerr_ignore(&err);
    }
#endif

    // FCGX leaks.
    OS_LibShutdown();

#if HAVE_LIBXML_XMLVERSION_H
    // Shutdown libxml2.
    flinter::Xml::Shutdown();
#endif

#if HAVE_CURL_CURL_H
    // Shutdown libcurl.
    curl_global_cleanup();
#endif

#if HAVE_OPENSSL_OPENSSLV_H
    // Shutdown OpenSSL.
#if HAVE_SSL_LIBRARY_CLEANUP
    SSL_library_cleanup();
#endif

    EVP_cleanup();
    CRYPTO_cleanup_all_ex_data();
    ERR_remove_thread_state(NULL);
#endif

    google::protobuf::ShutdownProtobufLibrary();
    return ret;
}
Ejemplo n.º 5
0
static int acceptSocket(const char *webServerAddrs)
{
    SOCKET hSock;
    int ipcFd = -1;

    for (;;)
    {
        struct sockaddr sockaddr;
        int sockaddrLen = sizeof(sockaddr);

        for (;;)
        {
            const struct timeval timeout = {1, 0};
            fd_set readfds;

            FD_ZERO(&readfds);

#pragma warning( disable : 4127 ) 
            FD_SET((unsigned int) hListen, &readfds);
#pragma warning( default : 4127 ) 

            if (select(0, &readfds, NULL, NULL, &timeout) == 0)
            {
                if (shutdownPending) 
                {
                    OS_LibShutdown();
                    return -1;
                }
            }
            else 
            {
                break;
            }
        }
    
#if NO_WSAACEPT
        hSock = accept((SOCKET) hListen, &sockaddr, &sockaddrLen);

        if (hSock == INVALID_SOCKET)
        {
            break;
        }

        if (isAddrOK((struct sockaddr_in *) &sockaddr, webServerAddrs))
        {
            break;
        }

        closesocket(hSock);
#else
        hSock = WSAAccept((unsigned int) hListen,                    
                          &sockaddr,  
                          &sockaddrLen,               
                          isAddrOKCallback,  
                          (DWORD) webServerAddrs);

        if (hSock != INVALID_SOCKET)
        {
            break;
        }
        
        if (WSAGetLastError() != WSAECONNREFUSED)
        {
            break;
        }
#endif
    }

    if (hSock == INVALID_SOCKET) 
    {
        /* Use FormatMessage() */
        fprintf(stderr, "accept()/WSAAccept() failed: %d", WSAGetLastError());
        return -1;
    }
    
    ipcFd = Win32NewDescriptor(FD_SOCKET_SYNC, hSock, -1);
	if (ipcFd == -1) 
    {
	    closesocket(hSock);
	}

    return ipcFd;
}
Ejemplo n.º 6
0
Archivo: ows.c Proyecto: Ezio47/tinyows
int main(int argc, char *argv[])
{
  ows *o;
  char *query;

  o = ows_init();
  o->config_file = buffer_init();

  /* Config Files */
  if (getenv("TINYOWS_CONFIG_FILE"))
    buffer_add_str(o->config_file, getenv("TINYOWS_CONFIG_FILE"));
  else if (getenv("TINYOWS_MAPFILE")) {
    buffer_add_str(o->config_file, getenv("TINYOWS_MAPFILE"));
    o->mapfile = true;
  } else
    buffer_add_str(o->config_file, OWS_CONFIG_FILE_PATH);

  LIBXML_TEST_VERSION
  xmlInitParser();

  /* Parse the configuration file and initialize ows struct */
  if (!o->exit) ows_parse_config(o, o->config_file->buf);
  if (!o->exit) ows_log(o, 2, "== TINYOWS STARTUP ==");

  /* Connect the ows to the database */
  if (!o->exit) ows_pg(o, o->pg_dsn->buf);
  if (!o->exit) ows_log(o, 2, "== Connection PostGIS ==");

  /* Fill layers storage metadata */
  if (!o->exit) ows_layers_storage_fill(o);
  if (!o->exit) ows_log(o, 2, "== Filling Storage ==");

  o->init = false;

#if TINYOWS_FCGI
  if (!o->exit) ows_log(o, 2, "== FCGI START ==");
  while (FCGI_Accept() >= 0) {
#endif

    query=NULL;
    if (!o->exit) query = cgi_getback_query(o);  /* Retrieve safely query string */
    if (!o->exit) ows_log(o, 4, query);          /* Log input query if asked */

    if (!o->exit && (!query || !strlen(query))) {
      /* Usage or Version command line options */
      if (argc > 1) {
        if (    !strncmp(argv[1], "--help", 6)
                || !strncmp(argv[1], "-h", 2)
                || !strncmp(argv[1], "--check", 7)) ows_usage(o);

        else if (    !strncmp(argv[1], "--version", 9)
                     || !strncmp(argv[1], "-v", 2))
          fprintf(stdout, "%s\n", TINYOWS_VERSION);

        else ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service");

      } else ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service");

      o->exit=true;  /* Have done what we have to */
    }

    if (!o->exit) o->request = ows_request_init();
    if (!o->exit) ows_kvp_or_xml(o, query);  /* Method is KVP or XML ? */

    if (!o->exit) {

      switch (o->request->method) {
        case OWS_METHOD_KVP:
          o->cgi = cgi_parse_kvp(o, query);
          break;
        case OWS_METHOD_XML:
          o->cgi = cgi_parse_xml(o, query);
          break;

        default:
          ows_error(o, OWS_ERROR_REQUEST_HTTP, "Wrong HTTP request Method", "http");
      }
    }

    if (!o->exit) o->psql_requests = list_init();
    if (!o->exit) ows_metadata_fill(o, o->cgi);                    /* Fill service's metadata */
    if (!o->exit) ows_request_check(o, o->request, o->cgi, query); /* Process service request */

    /* Run the right OWS service */
    if (!o->exit) {
      switch (o->request->service) {
        case WFS:
          o->request->request.wfs = wfs_request_init();
          wfs_request_check(o, o->request->request.wfs, o->cgi);
          if (!o->exit) wfs(o, o->request->request.wfs);
          break;
        default:
          ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service");
      }
    }

    if (o->request) {
      ows_request_free(o->request);
      o->request=NULL;
    }

    /* We allocated memory only on post case */
    if (cgi_method_post() && query) free(query);

#if TINYOWS_FCGI
    fflush(stdout);
    o->exit = false;
  }
  ows_log(o, 2, "== FCGI SHUTDOWN ==");
  OS_LibShutdown();
#endif
  ows_log(o, 2, "== TINYOWS SHUTDOWN ==");
  ows_free(o);

  xmlCleanupParser();

  return EXIT_SUCCESS;
}