summaryrefslogtreecommitdiff
path: root/cpp/lib/broker/Daemon.h
blob: cf9bf1376441392f3958e51f56b2cfcfb3ac0aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef _broker_Daemon_h
#define _broker_Daemon_h

/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <string>
#include <boost/scoped_ptr.hpp>

namespace qpid {
namespace broker {

/**
 * Tools for forking and managing a daemon process.
 * NB: Only one Daemon instance is allowed in a process.
 */
class Daemon
{
  public:

    /** Extract the daemon's name from argv[0] */
    static std::string nameFromArgv0(const char* argv0);
    
    /**
     * Creating a Daemon instance forks a daemon process.
     *@param name used to create pid files etc.
     *@param timeout in seconds for all operations that wait.
     */
    Daemon(const std::string& name, int timeout);

    ~Daemon();
    
    /** Fork the daemon, wait till it signals readiness */
    pid_t fork();

    /** Child only, send ready signal so parent fork() will return. */
    void ready();
    
    /** Child only, send failed signal so parent fork() will throw. */
    void failed();
    
    /** Kill the daemon with SIGINT. */
    void quit();
    
    /** Kill the daemon with SIGKILL. */
    void kill();

    /** Check daemon is running, throw exception if not */
    pid_t check();

    bool isParent() { return pid > 0; }

    bool isChild() { return pid == 0; }
    
    std::string getName() const { return name; }

    pid_t getPid() const {return pid; }

  private:
    class Retval;

    void notify(int);
    
    static std::string name;
    static std::string pidFile;
    static const char* getPidFile();
    boost::scoped_ptr<Retval> retval;
    pid_t pid;
    int timeout;
};

}} // namespace qpid::broker



#endif  /*!_broker_Daemon_h*/