Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReloadNonCommandFlags needed #89

Open
GoogleCodeExporter opened this issue Mar 13, 2015 · 2 comments
Open

ReloadNonCommandFlags needed #89

GoogleCodeExporter opened this issue Mar 13, 2015 · 2 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 25 Nov 2014 at 2:19

@GoogleCodeExporter
Copy link
Author

change the function name from 'ReloadNonCommandFlags' to 
'ReloadNonCommandLineFlags'

Original comment by [email protected] on 25 Nov 2014 at 2:28

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 25 Nov 2014 at 2:40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant