xunzhang / gflags

Automatically exported from code.google.com/p/gflags
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

ReloadNonCommandFlags needed #89

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What I want is: 
when the daemon running, I modify the flagfile or some env flags, and send the 
daemon SIGUSR1 to reload the flagfile.

the Code:
extern GFLAGS_DLL_DECL void ReloadNonCommandFlags();

static uint32 __ReloadNonCommandFlagsInternal(int* argc, char*** argv,
                                            bool remove_flags, bool do_report) {
  SetArgv(*argc, const_cast<const char**>(*argv));    // save it for later

  FlagRegistry* const registry = FlagRegistry::GlobalRegistry();
  CommandLineFlagParser parser(registry);

  // When we parse the commandline flags, we'll handle --flagfile,
  // --tryfromenv, etc. as we see them (since flag-evaluation order
  // may be important).  But sometimes apps set FLAGS_tryfromenv/etc.
  // manually before calling ParseCommandLineFlags.  We want to evaluate
  // those too, as if they were the first flags on the commandline.
  registry->Lock();
  parser.ProcessFlagfileLocked(FLAGS_flagfile, SET_FLAGS_VALUE);
  // Last arg here indicates whether flag-not-found is a fatal error or not
  parser.ProcessFromenvLocked(FLAGS_fromenv, SET_FLAGS_VALUE, true);
  parser.ProcessFromenvLocked(FLAGS_tryfromenv, SET_FLAGS_VALUE, false);
  registry->Unlock();

  if (do_report)
    HandleCommandLineHelpFlags();   // may cause us to exit on --help, etc.

  // See if any of the unset flags fail their validation checks
  parser.ValidateAllFlags();

  if (parser.ReportErrors())        // may cause us to exit on illegal flags
    gflags_exitfunc(1);
  return 0;
}

void ReloadNonCommandFlags()
{
  // We make a copy of argc and argv to pass in
  const vector<string>& argvs = GetArgvs();
  int tmp_argc = static_cast<int>(argvs.size());
  char** tmp_argv = new char* [tmp_argc + 1];
  for (int i = 0; i < tmp_argc; ++i)
    tmp_argv[i] = strdup(argvs[i].c_str());   // TODO(csilvers): don't dup

  __ReloadNonCommandFlagsInternal(&tmp_argc, &tmp_argv, false, true);

  for (int i = 0; i < tmp_argc; ++i)
    free(tmp_argv[i]);
  delete[] tmp_argv;
}

Original issue reported on code.google.com by guang22c...@gmail.com on 25 Nov 2014 at 2:19

GoogleCodeExporter commented 9 years ago
change the function name from 'ReloadNonCommandFlags' to 
'ReloadNonCommandLineFlags'

Original comment by guang22c...@gmail.com on 25 Nov 2014 at 2:28

GoogleCodeExporter commented 9 years ago
Interesting suggestion. Any reason for duplicating the 
ParseCommandLineFlagsInternal function? Why not just call the existing function 
from ReloadNonCommandLineFlags? Actually, this function is doing the exact same 
as ReparseCommandLineNonHelpFlags, except that help flags are treated as well. 
However, I cannot see the why you would want a daemon to read a help flag from 
a flag file or an environment variable to print a help screen and then exit... 
Please elaborate the use case a bit more and why what you need cannot be 
achieved using ReparseCommandLineNonHelpFlags.

Original comment by andreas....@gmail.com on 25 Nov 2014 at 2:40