1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 
18 #include <base/command_line.h>
19 #include <base/logging.h>
20 #include <brillo/syslog_logging.h>
21 #include "dhcp_client/daemon.h"
22 
23 using std::vector;
24 
25 namespace {
26 
27 namespace switches {
28 
29 // Don't daemon()ize; run in foreground.
30 const char kForeground[] = "foreground";
31 // Flag to show the help message.
32 const char kHelp[] = "help";
33 // The help message shown if help flag is passed to the program.
34 const char kHelpMessage[] = "\n help message \n";
35 
36 }  // namespace switches
37 
38 }  // namespace
39 
40 // Always logs to the syslog and logs to stderr if
41 // we are running in the foreground.
SetupLogging(bool foreground,const char * daemon_name)42 void SetupLogging(bool foreground,
43                   const char* daemon_name) {
44   int log_flags = 0;
45   log_flags |= brillo::kLogToSyslog;
46   log_flags |= brillo::kLogHeader;
47   if (foreground) {
48     log_flags |= brillo::kLogToStderr;
49   }
50   brillo::InitLog(log_flags);
51 }
52 
OnStartup(const char * daemon_name,base::CommandLine * cl)53 void OnStartup(const char* daemon_name, base::CommandLine* cl) {
54   LOG(INFO) << __func__;
55   SetupLogging(cl->HasSwitch(switches::kForeground), daemon_name);
56   return;
57 }
58 
main(int argc,char * argv[])59 int main(int argc, char* argv[]) {
60   base::CommandLine::Init(argc, argv);
61   base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
62 
63   if (cl->HasSwitch(switches::kHelp)) {
64     LOG(INFO) << switches::kHelpMessage;
65     return 0;
66   }
67 
68   dhcp_client::Daemon daemon(base::Bind(&OnStartup, argv[0], cl));
69 
70   daemon.Run();
71 
72   return 0;
73 }
74