Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2016 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 #include "shill/shill_daemon.h"
     18 
     19 #include <sysexits.h>
     20 
     21 #include <base/bind.h>
     22 
     23 using base::Bind;
     24 using base::Unretained;
     25 
     26 namespace shill {
     27 
     28 ShillDaemon::ShillDaemon(const base::Closure& startup_callback,
     29                          const shill::DaemonTask::Settings& settings,
     30                          Config* config)
     31     : DaemonTask(settings, config), startup_callback_(startup_callback) {}
     32 
     33 ShillDaemon::~ShillDaemon() {}
     34 
     35 int ShillDaemon::OnInit() {
     36   // Manager DBus interface will get registered as part of this init call.
     37   int return_code = brillo::Daemon::OnInit();
     38   if (return_code != EX_OK) {
     39     return return_code;
     40   }
     41 
     42   Init();
     43 
     44   // Signal that we've acquired all resources.
     45   startup_callback_.Run();
     46 
     47   return EX_OK;
     48 }
     49 
     50 void ShillDaemon::OnShutdown(int* return_code) {
     51   if (!DaemonTask::Quit(base::Bind(&DaemonTask::BreakTerminationLoop,
     52                                    base::Unretained(this)))) {
     53     // Run a message loop to allow shill to complete its termination
     54     // procedures. This is different from the secondary loop in
     55     // brillo::Daemon. This loop will run until we explicitly
     56     // breakout of the loop, whereas the secondary loop in
     57     // brillo::Daemon will run until no more tasks are posted on the
     58     // loop.  This allows asynchronous D-Bus method calls to complete
     59     // before exiting.
     60     brillo::MessageLoop::current()->Run();
     61   }
     62 
     63   brillo::Daemon::OnShutdown(return_code);
     64 }
     65 
     66 }  // namespace shill
     67