blob: 7c2eee8cb5fd59a44fbc83c24969a6e5f7cbb142 [file] [log] [blame]
// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <base/macros.h>
namespace chromeos {
class Process;
} // namespace chromeos
namespace vpn_manager {
// Holds the state of a running daemon process, and allows lookup via
// process id file. Provides a method for the running process to be
// terminated either expliclty via a method call or implicitly via the
// object destructor.
class Daemon {
explicit Daemon(const std::string& pid_file);
virtual ~Daemon();
// Clear any reference to a process, terminating the process if it is
// still running.
virtual void ClearProcess();
// Replace the current process with a new process instance. Returns
// the new process. The process pointer returned is still owned by
// this object.
virtual chromeos::Process* CreateProcess();
// Find a process associated with the process-id file. If one is found,
// replace the current |process_| instance with this result. Returns
// true if a process was found, false otherwise.
virtual bool FindProcess();
// Returns true if the stored process is currently running.
virtual bool IsRunning();
// Stop the running daemon "nicely", sending it a SIGTERM signal first,
// then a SIGKILL. Returns true if the process does not exist or if
// it was successfully reaped after a SIGTERM, false otherwise.
virtual bool Terminate();
// Return the process id of the running daemon.
virtual pid_t GetPid() const;
friend class DaemonTest;
// Replace the current process with |process|. Any previous process
// will be terminated if it is not the same process id as |process|.
void SetProcess(chromeos::Process* process);
// Give daemon time to shut down cleanly after a SIGTERM before killing it
// in a more decisive fashion.
static const int kTerminationTimeoutSeconds;
// Process instance associated with this process.
std::unique_ptr<chromeos::Process> process_;
// File name where the process id for this daemon is held.
std::string pid_file_;
} // namespace vpn_manager