use status-handle instead of winsrv cmdline option JSON-75
authorClaudio Luck <claudio.luck@pep.foundation>
Mon, 02 Apr 2018 19:26:59 +0200
branchJSON-75
changeset 4520bb6f817b916
parent 451 191651a4461d
child 453 a4d710c6c845
use status-handle instead of winsrv cmdline option
server/daemonize.cc
server/daemonize.hh
server/main.cc
     1.1 --- a/server/daemonize.cc	Mon Apr 02 16:24:32 2018 +0200
     1.2 +++ b/server/daemonize.cc	Mon Apr 02 19:26:59 2018 +0200
     1.3 @@ -16,7 +16,7 @@
     1.4  HANDLE gPipeRd = NULL;
     1.5  HANDLE gPipeWr = NULL;
     1.6  
     1.7 -void daemonize (const bool daemonize, const uintptr_t winsrv)
     1.8 +void daemonize (const bool daemonize, const uintptr_t status_handle)
     1.9  {
    1.10      TCHAR * szWinFrontCmdline, * szWinSrvCmdline;
    1.11      DWORD dwWinFrontCmdlineLen;
    1.12 @@ -34,11 +34,11 @@
    1.13       * additional open file handles. But the new process must learn about the
    1.14       * value of the filehandles somehow; only handles dedicated to stderr/stdout/stdin
    1.15       * redirection can be passed "internally" via system call. So we simply pass the
    1.16 -     * handle as a string over the command line; as "winsrv" argument. See CreateProcess
    1.17 -     * documentation for more information.
    1.18 +     * handle as a string over the command line; #defined STATUS_HANDLE "status-handle".
    1.19 +     * See CreateProcess documentation for more information.
    1.20       */
    1.21  
    1.22 -    gPipeWr = (HANDLE) winsrv;
    1.23 +    gPipeWr = (HANDLE) status_handle;
    1.24  
    1.25      if (gPipeWr == NULL)
    1.26      {
    1.27 @@ -54,19 +54,19 @@
    1.28              throw std::runtime_error("Cannot configure pipe read handle!");
    1.29  
    1.30          /*
    1.31 -         * Create new command line with appended " --winsrv <handle>"
    1.32 +         * Create new command line with appended " --status-handle <handle>"
    1.33           */
    1.34          szWinFrontCmdline = GetCommandLine();           // FIXME: Unicode GetCommandLineW, and wmain()
    1.35 -        dwWinFrontCmdlineLen = _tcslen(szWinFrontCmdline);
    1.36 -        if (dwWinFrontCmdlineLen + 50 >= MAX_PATH)      // + 50 to accommodate " --winsrv PRIuPRT"
    1.37 +        dwWinFrontCmdlineLen = _tcslen(szWinFrontCmdline) + _tcslen(_T(" -- ")) + _tcslen(_T(STATUS_HANDLE));
    1.38 +        if (dwWinFrontCmdlineLen + 40 >= MAX_PATH)      // + 40 to accommodate PRIuPRT
    1.39              throw std::runtime_error("Command line too long to be extend!");
    1.40  
    1.41 -        if (!(szWinSrvCmdline = (TCHAR *)calloc(dwWinFrontCmdlineLen + 50 + 1, sizeof(TCHAR))))
    1.42 +        if (!(szWinSrvCmdline = (TCHAR *)calloc(dwWinFrontCmdlineLen + 40 + 1, sizeof(TCHAR))))
    1.43              throw std::runtime_error("Memory allocation for background process command line failed!");
    1.44  
    1.45 -        _stprintf_s(szWinSrvCmdline, dwWinFrontCmdlineLen + 50 + 0,
    1.46 -                    _T("%s --winsrv %" PRIuPTR), szWinFrontCmdline,
    1.47 -                                                 ((uintptr_t)(void*)gPipeWr));
    1.48 +        _stprintf_s(szWinSrvCmdline, dwWinFrontCmdlineLen + 40 + 0,
    1.49 +                    _T("%s --" STATUS_HANDLE " %") PRIuPTR, szWinFrontCmdline,
    1.50 +                                                            ((uintptr_t)gPipeWr));
    1.51  
    1.52          ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
    1.53  
    1.54 @@ -196,7 +196,7 @@
    1.55      sig_term_recv = 1;
    1.56  }
    1.57  
    1.58 -void daemonize (const bool daemonize, const uintptr_t winsrv)
    1.59 +void daemonize (const bool daemonize, const uintptr_t status_handle)
    1.60  {
    1.61      int retval = EXIT_FAILURE;
    1.62      pid_t pid, sid, daemon_pid;
     2.1 --- a/server/daemonize.hh	Mon Apr 02 16:24:32 2018 +0200
     2.2 +++ b/server/daemonize.hh	Mon Apr 02 19:26:59 2018 +0200
     2.3 @@ -3,14 +3,11 @@
     2.4  
     2.5  #include <cstdint>
     2.6  
     2.7 -// cstdint uintptr_t is optional
     2.8 -#ifndef uintptr_t
     2.9 -#define ptrdiff_t uintptr_t
    2.10 -#endif
    2.11 +#define STATUS_HANDLE "status-handle"
    2.12  
    2.13  // fork(), go into background, close all ttys etc...
    2.14  // system-specific! (POSIX, Windows, ...?)
    2.15 -void daemonize(const bool daemonize, const uintptr_t winsrv);
    2.16 +void daemonize(const bool daemonize, const uintptr_t status_handle);
    2.17  void daemonize_commit(int retval);
    2.18  
    2.19  #endif
     3.1 --- a/server/main.cc	Mon Apr 02 16:24:32 2018 +0200
     3.2 +++ b/server/main.cc	Mon Apr 02 19:26:59 2018 +0200
     3.3 @@ -16,7 +16,7 @@
     3.4  bool debug_mode = false;
     3.5  bool do_sync    = false;
     3.6  bool ignore_missing_session = false;
     3.7 -uintptr_t winsrv = 0;
     3.8 +uintptr_t status_handle = 0;
     3.9  
    3.10  std::string address = "127.0.0.1";
    3.11  std::string logfile = "";
    3.12 @@ -57,7 +57,7 @@
    3.13  		("logfile,l", po::value<std::string>(&logfile)->default_value(logfile),   "Name of the logfile. Can be \"stderr\" for log to stderr or empty for no log.")
    3.14  		("ignore-missing-session", po::bool_switch(&ignore_missing_session), "Ignore when no PEP_SESSION can be created.")
    3.15  #ifdef _WIN32
    3.16 -		("winsrv", po::value<uintptr_t>(&winsrv)->default_value(0), "For internal use (HANDLE)")
    3.17 +		((STATUS_HANDLE), po::value<uintptr_t>(&status_handle)->default_value(0), "Status file handle, for internal use.")
    3.18  #endif	
    3.19  	;
    3.20  	
    3.21 @@ -88,7 +88,7 @@
    3.22  	}
    3.23  	
    3.24  	if( debug_mode == false )
    3.25 -		daemonize (!debug_mode, (const uintptr_t) winsrv);
    3.26 +		daemonize (!debug_mode, (const uintptr_t) status_handle);
    3.27  
    3.28  	JsonAdapter ja( my_logfile );
    3.29  	ja.do_sync( do_sync)