Blame view

trunk/src/main/srs_main_server.cpp 4.5 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
24
#include <srs_kernel_log.hpp>
25
#include <srs_kernel_error.hpp>
26 27 28
#include <srs_app_server.hpp>
#include <srs_app_config.hpp>
#include <srs_app_log.hpp>
winlin authored
29 30 31 32 33 34 35

// kernel module.
ISrsLog* _srs_log = new SrsFastLog();
ISrsThreadContext* _srs_context = new SrsThreadContext();
// app module.
SrsConfig* _srs_config = new SrsConfig();
SrsServer* _srs_server = new SrsServer();
winlin authored
36 37 38 39

#include <stdlib.h>
#include <signal.h>
winlin authored
40 41 42
#include <sys/types.h>
#include <sys/wait.h>
43
#ifdef SRS_GPERF_MP
44
    #include <gperftools/heap-profiler.h>
45
#endif
46
#ifdef SRS_GPERF_CP
47
    #include <gperftools/profiler.h>
48 49
#endif
winlin authored
50 51
void handler(int signo)
{
52 53
    srs_trace("get a signal, signo=%d", signo);
    _srs_server->on_signal(signo);
winlin authored
54 55
}
winlin authored
56
int run_master() 
winlin authored
57
{
58
    int ret = ERROR_SUCCESS;
59
    
60
    signal(SIGNAL_RELOAD, handler);
61
    signal(SIGTERM, handler);
62 63
    signal(SIGINT, handler);
    
winlin authored
64 65 66 67
    if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
        return ret;
    }
    
68
    if ((ret = _srs_server->initialize_st()) != ERROR_SUCCESS) {
winlin authored
69 70 71
        return ret;
    }
    
72 73 74 75 76 77 78 79
    if ((ret = _srs_server->listen()) != ERROR_SUCCESS) {
        return ret;
    }
    
    if ((ret = _srs_server->cycle()) != ERROR_SUCCESS) {
        return ret;
    }
    
winlin authored
80 81
    return 0;
}
winlin authored
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

int run() 
{
    // if not deamon, directly run master.
    if (!_srs_config->get_deamon()) {
        return run_master();
    }
    
    srs_trace("start deamon mode...");
    
    int pid = fork();
    
    if(pid == -1){
        srs_error("create process error. ret=-1"); //ret=0
        return -1;
    }

    // grandpa
    if(pid > 0){
        int status = 0;
        if(waitpid(pid, &status, 0) == -1){
            srs_error("wait child process error! ret=-1"); //ret=0
        }
        srs_trace("grandpa process exit.");
        exit(0);
        return 0;
    }

    // father
    pid = fork();
    
    if(pid == -1){
        srs_error("create process error. ret=-1");
        return -1;
    }

    if(pid > 0){
        srs_trace("father process exit. ret=-1");
        exit(0);
        return 0;
    }

    // son
    srs_trace("son(deamon) process running.");
    
    return run_master();
}

int main(int argc, char** argv) 
{
    int ret = ERROR_SUCCESS;
    
    // TODO: support both little and big endian.
    srs_assert(srs_is_little_endian());

#ifdef SRS_GPERF_MP
    HeapProfilerStart("gperf.srs.gmp");
#endif
#ifdef SRS_GPERF_CP
    ProfilerStart("gperf.srs.gcp");
#endif

#ifdef SRS_GPERF_MC
    #ifdef SRS_GPERF_MP
    srs_error("option --with-gmc confict with --with-gmp, "
        "@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html\n"
        "Note that since the heap-checker uses the heap-profiling framework internally, "
        "it is not possible to run both the heap-checker and heap profiler at the same time");
    return -1;
    #endif
#endif
    
154 155
    // never use srs log(srs_trace, srs_error, etc) before config parse the option,
    // which will load the log config and apply it.
winlin authored
156 157 158
    if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
        return ret;
    }
159 160
    
    srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
161 162 163
    srs_trace("uname: "SRS_UNAME);
    srs_trace("build: %s, %s", SRS_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
    srs_trace("configure: "SRS_CONFIGURE);
winlin authored
164 165 166
#ifdef SRS_ARM_UBUNTU12
    srs_trace("arm tool chain: "SRS_ARM_TOOL_CHAIN);
#endif
167 168 169 170 171
    
    if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {
        return ret;
    }
    
winlin authored
172 173
    return run();
}