コード例 #1
0
ファイル: daemon_linux.cpp プロジェクト: dennis-kk/kshell
/**
 * @brief main entry
 */
int main(int argc, char **argv) {
   // open syslog
   openlog("kshell", LOG_PID, LOG_USER);

   // signal handlers
   signal(SIGINT, ctrlC);
   signal(SIGKILL, ctrlC);
   signal(SIGTERM, ctrlC);
   signal(SIGQUIT, ctrlC);
   if ((argc > 1) && ((*argv[1] == '-') || (*argv[1] == '/'))) {
      if (_stricmp( "start", argv[1]+1)) {
         return CmdStartService();
      } else if (_stricmp( "stop", argv[1]+1)) {
         return CmdStopService();
      } else if (_stricmp( "debug", argv[1]+1)) {
         CmdDebugService(argc, argv);
      } else {
         goto dispatch;
      }
      return 0;
   }

   // if it doesn't match any of the above parameters
   // print helper string
   dispatch:
   // this is just to be friendly
   printf( "%s -start            to start the service\n", SZAPPNAME );
   printf( "%s -stop             to start the service\n", SZAPPNAME );
   printf( "%s -debug <params>   to run as a console app for debugging\n", SZAPPNAME );

   return 0;
}
コード例 #2
0
//
//  FUNCTION: main
//
//  PURPOSE: entrypoint for service
//
//  PARAMETERS:
//    argc - number of command line arguments
//    argv - array of command line arguments
//
//  RETURN VALUE:
//    none
//
//  COMMENTS:
//    main() either performs the command line task, or
//    call StartServiceCtrlDispatcher to register the
//    main service thread.  When the this call returns,
//    the service has stopped, so exit.
//
int __cdecl main(int argc, char **argv)
{
   SERVICE_TABLE_ENTRY dispatchTable[] =
   {
      { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
      { NULL, NULL}
   };

   if ( (argc > 1) &&
        ((*argv[1] == '-') || (*argv[1] == '/')) )
   {
      if ( _stricmp( "install", argv[1]+1 ) == 0 )
      {
         return CmdInstallService();
      }
      else if ( _stricmp( "remove", argv[1]+1 ) == 0 )
      {
         return CmdRemoveService();
      }
      else if ( _stricmp( "start", argv[1]+1 ) == 0)
      {
         return CmdStartService();
      }
      else if ( _stricmp( "stop", argv[1]+1 ) == 0)
      {
         return CmdStopService();
      }
      else if ( _stricmp( "debug", argv[1]+1 ) == 0 )
      {
         bDebug = TRUE;
         CmdDebugService(argc, argv);
      }
      else
      {
         goto dispatch;
      }
      return 0;
   }

   // if it doesn't match any of the above parameters
   // the service control manager may be starting the service
   // so we must call StartServiceCtrlDispatcher
   dispatch:
   // this is just to be friendly
   printf( "%s -install          to install the service\n", SZAPPNAME );
   printf( "%s -start            to start the service\n", SZAPPNAME );
   printf( "%s -stop             to start the service\n", SZAPPNAME );
   printf( "%s -remove           to remove the service\n", SZAPPNAME );
   printf( "%s -debug <params>   to run as a console app for debugging\n", SZAPPNAME );
   printf( "\nStartServiceCtrlDispatcher being called.\n" );
   printf( "This may take several seconds.  Please wait.\n" );

   if (!StartServiceCtrlDispatcher(dispatchTable))
      AddToMessageLog(MSG_FLAGS_ERROR, TEXT("StartServiceCtrlDispatcher failed."));

   return 0;
}
コード例 #3
0
ファイル: service.c プロジェクト: benjdag/openvpn
int
_tmain(int argc, TCHAR *argv[])
{
    SERVICE_TABLE_ENTRY dispatchTable[] = {
        { automatic_service.name, ServiceStartAutomatic },
        { interactive_service.name, ServiceStartInteractive },
        { NULL, NULL }
    };

    openvpn_service[0] = automatic_service;
    openvpn_service[1] = interactive_service;

    if (argc > 1 && (*argv[1] == TEXT('-') || *argv[1] == TEXT('/')))
    {
        if (_tcsicmp(TEXT("install"), argv[1] + 1) == 0)
        {
            return CmdInstallServices();
        }
        else if (_tcsicmp(TEXT("remove"), argv[1] + 1) == 0)
        {
            return CmdRemoveServices();
        }
        else if (_tcsicmp(TEXT("start"), argv[1] + 1) == 0)
        {
            BOOL is_auto = argc < 3 || _tcsicmp(TEXT("interactive"), argv[2]) != 0;
            return CmdStartService(is_auto ? automatic : interactive);
        }
        else
        {
            goto dispatch;
        }

        return 0;
    }

    /* If it doesn't match any of the above parameters
     * the service control manager may be starting the service
     * so we must call StartServiceCtrlDispatcher
     */
dispatch:
    _tprintf(TEXT("%s -install        to install the services\n"), APPNAME);
    _tprintf(TEXT("%s -start <name>   to start a service (\"automatic\" or \"interactive\")\n"), APPNAME);
    _tprintf(TEXT("%s -remove         to remove the services\n"), APPNAME);
    _tprintf(TEXT("\nStartServiceCtrlDispatcher being called.\n"));
    _tprintf(TEXT("This may take several seconds. Please wait.\n"));

    if (!StartServiceCtrlDispatcher(dispatchTable))
    {
        MsgToEventLog(MSG_FLAGS_ERROR, TEXT("StartServiceCtrlDispatcher failed."));
    }

    return 0;
}
コード例 #4
0
ファイル: services.c プロジェクト: kymoon/pennmush
int WIN32_CDECL
main(int argc, char **argv)
{

  SERVICE_TABLE_ENTRY dispatchTable[] = {
    {THIS_SERVICE, (LPSERVICE_MAIN_FUNCTION) service_main},
    {NULL, NULL}
  };

  SERVICE_STATUS svcstatus;
  DWORD status;

/*
   Get the command line parameters and see what the user wants us to do.
 */

  if ((argc == 2) &&
      ((*argv[1] == '-') || (*argv[1] == '/') || (*argv[1] == '\\'))) {
    if (!_stricmp("install", argv[1] + 1))
      CmdInstallService(argc, argv);
    else if (!_stricmp("remove", argv[1] + 1))
      CmdRemoveService();
    else if (!_stricmp("start", argv[1] + 1))
      CmdStartService();
    else if (!_stricmp("stop", argv[1] + 1))
      CmdStopService();
    else if (!_stricmp("status", argv[1] + 1))
      CmdStatusService();
    else if (!_stricmp("run", argv[1] + 1)) {

/*  do not start the MUSH if it is already a running service */

      status = get_service_status(&svcstatus, TRUE);
      if (status == 0 && svcstatus.dwCurrentState == SERVICE_RUNNING) {
        fprintf(stderr, "The MUSH is already running as a service.\n");
        return 1;
      }
      worker_thread(NULL);
    } else
      CmdDisplayFormat();
  } else if (argc != 1)
    CmdDisplayFormat();
  else {

    /*  do not start the MUSH if it is already a running service */

    status = get_service_status(&svcstatus, TRUE);
    if (status == 0 && svcstatus.dwCurrentState == SERVICE_RUNNING) {
      fprintf(stderr, "The MUSH is already running as a service.\n");
      return 1;
    }
    /*  Under Windows 95 they won't be able to use the service manager */

    if (status == ERROR_CALL_NOT_IMPLEMENTED) {
      worker_thread(NULL);
      return 0;
    }
    /*
       Register the dispatch table with the service controller.

       If this fails then we are running interactively.

     */

    fprintf(stderr, "Attempting to start PennMUSH as a service ...\n");
    if (!StartServiceCtrlDispatcher(dispatchTable)) {
      fprintf(stderr,
              "Unable to start service, assuming running console-mode application.\n");
      fprintf(stderr,
              "You can save time on the next invocation by specifying: pennmush /run\n");
      worker_thread(NULL);
    }
  }                             /*  end of argc == 1 */

  return 0;
}                               /* end of main */
コード例 #5
0
// parseCommandLine ///////////////////////////////////////////////////////////
//
// check for command line parameters and set various flags 
//
void parseCommandLine (int *argc, char** argv[])
{
    /*
    // A little snipet of code to test the update feature.
    // Uncomment this code, compile, run mpd -update, then run mpd -loser to see if this new functionality exists
    if (GetOpt(*argc, *argv, "-loser"))
    {
	printf("you are a winner\n");
	ExitProcess(0);
    }
    //*/
    if (GetOpt(*argc, *argv, "-norestart"))
	bSetupRestart = false;
    if (GetOpt(*argc, *argv, "-interact"))
    {
	interact = true;
    }
    if (GetOpt(*argc, *argv, "-remove") || GetOpt(*argc, *argv, "-unregserver") || GetOpt(*argc, *argv, "-uninstall"))
    {
	RegDeleteKey(HKEY_CURRENT_USER, MPICHKEY);
	CmdRemoveService(TRUE);
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-install") || GetOpt(*argc, *argv, "-regserver"))
    {
	bool bMPDUserCapable = false;
	char account[100]="", password[100]="", phrase[MPD_PASSPHRASE_MAX_LENGTH+1]="", port[10]="";
	char version[100]="";

	if (CmdRemoveService(FALSE) == FALSE)
	{
	    printf("Unable to remove the previous installation, install failed.\n");
	    ExitProcess(0);
	}
	
	easy_socket_init();
	CreateMPDRegistry();
	bMPDUserCapable = GetOpt(*argc, *argv, "-mpduser");
	if (GetOpt(*argc, *argv, "-phrase", phrase))
	    WriteMPDRegistry("phrase", phrase);
	if (GetOpt(*argc, *argv, "-getphrase"))
	{
	    GetPassword("passphrase for mpd: ", NULL, phrase);
	    WriteMPDRegistry("phrase", phrase);
	}
	if (GetOpt(*argc, *argv, "-port", port))
	    WriteMPDRegistry("port", port);
	if (GetOpt(*argc, *argv, "-account", account))
	{
	    if (!GetOpt(*argc, *argv, "-password", password))
		GetPassword(NULL, account, password);
	    WriteMPDRegistry("SingleUser", "yes");
	    ParseRegistry(true);
	    CmdInstallService(account, password, bMPDUserCapable);
	}
	else
	{
	    WriteMPDRegistry("SingleUser", "no");
	    ParseRegistry(true);
	    CmdInstallService(NULL, NULL, bMPDUserCapable);
	}
	GetMPDVersion(version, 100);
	WriteMPDRegistry("version", version);
	easy_socket_finalize();
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-update"))
    {
	char account[100]="", password[100]="", phrase[MPD_PASSPHRASE_MAX_LENGTH+1]="", port[10]="";
	int nPort;
	char pszHost[MAX_HOST_LENGTH], pszHostFile[MAX_PATH];
	char pszFileName[MAX_PATH];
	char pszError[256];

	if (!GetOpt(*argc, *argv, "-mpd", pszFileName))
	{
	    HMODULE hModule = GetModuleHandle(NULL);
	    if (!GetModuleFileName(hModule, pszFileName, MAX_PATH))
	    {
		printf("Please specify the location of the new mpd.exe with the -mpd option, (-mpd c:\\some\\path\\mpd.exe)\n");
		ExitProcess(0);
	    }
	    printf("updating mpd to '%s'\n", pszFileName);
	}
	if (!GetOpt(*argc, *argv, "-singleuser"))
	{
	    if (GetOpt(*argc, *argv, "-account", account))
	    {
		if (!GetOpt(*argc, *argv, "-password", password))
		    GetPassword(NULL, account, password);
	    }
	    else
	    {
		printf("Enter a user to connect to the remote machines as.\naccount: ");fflush(stdout);
		gets(account);
		GetPassword(NULL, account, password);
	    }
	}

	easy_socket_init();
	nPort = MPD_DEFAULT_PORT;
	if (!ReadMPDRegistry("phrase", phrase, false))
	    strcpy(phrase, MPD_DEFAULT_PASSPHRASE);
	GetOpt(*argc, *argv, "-phrase", phrase);
	if (GetOpt(*argc, *argv, "-getphrase"))
	{
	    GetPassword("passphrase for mpd: ", NULL, phrase);
	}
	if (GetOpt(*argc, *argv, "-port", port))
	    nPort = atoi(port);
	if (GetOpt(*argc, *argv, "-hostfile", pszHostFile))
	{
	    FILE *fin = fopen(pszHostFile, "r");
	    if (fin == NULL)
	    {
		char pszStr[1024];
		Translate_Error(GetLastError(), pszStr);
		printf("Unable to open the host file '%s', %s\n", pszHostFile, pszStr);
		easy_socket_finalize();
		ExitProcess(0);
	    }

	    while (GetNextHost(fin, pszHost))
	    {
		pszError[0] = '\0';
		if (!UpdateMPD(pszHost, account, password, nPort, phrase, pszFileName, pszError, 256))
		{
		    printf("Failed to update mpd on %s:\n%s\n", pszHost, pszError);
		}
	    }
	    fclose(fin);
	}
	else
	{
	    if (!GetOpt(*argc, *argv, "-host", pszHost))
	    {
		printf("Enter the hostname where the mpd that you wish to update is running.\nhost: ");fflush(stdout);
		gets(pszHost);
	    }
	    pszError[0] = '\0';
	    if (!UpdateMPD(pszHost, account, password, nPort, phrase, pszFileName, pszError, 256))
	    {
		printf("Failed to update mpd on %s:\n%s\n", pszHost, pszError);
	    }
	}

	easy_socket_finalize();
	printf("Finished.\n");
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-iupdate"))
    {
	// This option is used internally by the update feature
	char pszOldFileName[MAX_PATH], pszNewFileName[MAX_PATH];
	char pszPid[10];
	if (GetOpt(*argc, *argv, "-pid", pszPid) && 
	    GetOpt(*argc, *argv, "-old", pszOldFileName) && 
	    GetOpt(*argc, *argv, "-new", pszNewFileName))
	{
	    UpdateMPD(pszOldFileName, pszNewFileName, atoi(pszPid));
	}
	ExitProcess(0);
    }
    char host[100];
    if (GetOpt(*argc, *argv, "-console", host))
    {
	char phrase[MPD_PASSPHRASE_MAX_LENGTH+1];
	int port = -1;
	GetOpt(*argc, *argv, "-port", &port);
	/* DoConsole destroys phrase after using it */
	DoConsole(
	    host, port, 
	    GetOpt(*argc, *argv, "-getphrase"),
	    GetOpt(*argc, *argv, "-phrase", phrase) ? phrase : NULL);
	easy_socket_finalize();
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-console"))
    {
	char phrase[MPD_PASSPHRASE_MAX_LENGTH+1];
	int port = -1;
	GetOpt(*argc, *argv, "-port", &port);
	/* DoConsole destroys phrase after using it */
	DoConsole(
	    NULL, port, 
	    GetOpt(*argc, *argv, "-getphrase"),
	    GetOpt(*argc, *argv, "-phrase", phrase) ? phrase : NULL);
	easy_socket_finalize();
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-start"))
    {
	CmdStartService();
	ExitProcess(0);
    }
    char pszFileName[MAX_PATH];
    if (GetOpt(*argc, *argv, "-startdelete", pszFileName))
    {
	// This option is used by the update feature to start the new service and delete the old one.
	char version[100];
	// update the version
	GetMPDVersion(version, 100);
	WriteMPDRegistry("version", version);
	// start the new mpd
	CmdStartService();
	// Give the temporary mpd time to exit
	Sleep(1000);
	// Then delete it.
	DeleteFile(pszFileName);
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-stop"))
    {
	CmdStopService();
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-restart", host))
    {
	ConnectAndRestart(argc, argv, host);
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-restart"))
    {
	CmdStopService();
	Sleep(1000);
	CmdStartService();
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-clean"))
    {
	CleanMPDRegistry();
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-d"))
    {
	char account[100]="", password[100]="", phrase[MPD_PASSPHRASE_MAX_LENGTH+1]="", pszPort[20]="";
	char str_temp[10];
	easy_socket_init();
	CreateMPDRegistry();
	if (GetOpt(*argc, *argv, "-phrase", phrase))
	    WriteMPDRegistry("phrase", phrase);
	if (GetOpt(*argc, *argv, "-getphrase"))
	{
	    GetPassword("passphrase for mpd: ", NULL, phrase);
	    WriteMPDRegistry("phrase", phrase);
	}
	if (GetOpt(*argc, *argv, "-port", pszPort))
	{
	    int g_nSavedPort = g_nPort;
	    g_nPort = atoi(pszPort);
	    if (g_nPort > 0)
	    {
		sprintf(pszPort, "%d", g_nPort);
		WriteMPDRegistry("port", pszPort);
		//printf("using port %d\n", g_nPort);
	    }
	    else
		g_nPort = g_nSavedPort;
	}
	g_bSingleUser = true;
	g_bStartAlone = GetOpt(*argc, *argv, "-startalone");
	if (ReadMPDRegistry("SingleUser", str_temp, false))
	{
	    if (stricmp(str_temp, "no") == 0)
	    {
		WriteMPDRegistry("RevertToMultiUser", "yes");
	    }
	}
	WriteMPDRegistry("SingleUser", "yes");

	ParseRegistry(true);
	CmdDebugService(*argc, *argv);
	easy_socket_finalize();
	ExitProcess(0);
    }
    if (GetOpt(*argc, *argv, "-v") || GetOpt(*argc, *argv, "-version"))
    {
/*#define USE_BAD_NONREDIRECTABLE_VERSION*/
#ifdef USE_BAD_NONREDIRECTABLE_VERSION
	/* stdin, stdout, stderr don't get redirected for some reason when I launch this application
	   with pipes for redirecting output.  If the HANDLE version of stderr is used like the code 
	   after the else, then everything works fine. */
	char str[100];
	GetMPDVersion(str, 100);
	fprintf(stderr, "\nMPD - mpich daemon for Microsoft Windows, version %s\n%s\n", str, COPYRIGHT);
	fflush(stderr);
	ExitProcess(0);
#else
	HANDLE hErr = GetStdHandle(STD_ERROR_HANDLE);
	char str[100], out[200];
	DWORD num_written;

	GetMPDVersion(str, 100);
	sprintf(out, "\nMPD - mpich daemon for Microsoft Windows, version %s\n%s\n", str, COPYRIGHT);
	WriteFile(hErr, out, strlen(out), &num_written, NULL);
	ExitProcess(0);
#endif
    }
    if (GetOpt(*argc, *argv, "-h") || GetOpt(*argc, *argv, "-?") || GetOpt(*argc, *argv, "-help"))
    {
	char str[100];
	GetMPDVersion(str, 100);
	fprintf(stderr, "\nMPD - mpich daemon for Windows NT, version %s\n%s\n", str, COPYRIGHT);
	fprintf(stderr, "Usage:\n  mpd [ -v -h -install -remove -console ]\n\nCommand line options:\n");
	fprintf(stderr, "  -install \t:install the service\n  -install -interact    :allows the mpd to interact with the desktop\n");
	fprintf(stderr, "  -install -mpduser\t:install the service with single user commands enabled.\n");
	fprintf(stderr, "  -remove\t:remove the service\n");
	fprintf(stderr, "  -v\t\t:display version\n");
	fprintf(stderr, "  -h\t\t:this help screen\n");
	fprintf(stderr, "  -console\t:start a console session with the mpd on the current host\n");
	fprintf(stderr, "  -console host [-port x] :start a console session with the mpd on 'host:port'\n");
	fprintf(stderr, "  -d\t\t:run the mpd from the console\n");
	ExitProcess(0);
    }
}