mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
write pid to ./objs/srs.pid.
This commit is contained in:
parent
0686b8fa5a
commit
f25c6558a5
8 changed files with 256 additions and 145 deletions
|
@ -1,7 +1,7 @@
|
|||
Simple-RTMP-Server
|
||||
==================
|
||||
|
||||
SRS(SIMPLE RTMP Server) over state-threads created in 2013.
|
||||
SRS(SIMPLE RTMP Server) over state-threads created in 2013.10.
|
||||
|
||||
SRS is a simple, [RTMP](https://github.com/winlinvip/simple-rtmp-server/wiki/DeliveryRTMP)/[HLS](https://github.com/winlinvip/simple-rtmp-server/wiki/DeliveryHLS),
|
||||
[high-performance](https://github.com/winlinvip/simple-rtmp-server/wiki/Performance), single/multiple(plan) processes, edge(plan)/origin live server,
|
||||
|
@ -169,6 +169,7 @@ See also: [Performance Test Guide](https://github.com/winlinvip/simple-rtmp-serv
|
|||
* nginx v1.5.0: 139524 lines <br/>
|
||||
|
||||
### History
|
||||
* v1.0, 2014-03-21, write pid to ./objs/srs.pid.
|
||||
* v1.0, 2014-03-20, refine hls code, support pure audio HLS.
|
||||
* v1.0, 2014-03-19, add vn/an for FFMPEG to drop video/audio for radio stream.
|
||||
* v1.0, 2014-03-19, refine handshake, client support coplex handshake, add utest.
|
||||
|
@ -429,7 +430,7 @@ Bandwidth Test Workflow:
|
|||
@See: class SrsBandwidth comments.
|
||||
</pre>
|
||||
|
||||
Beijing, 2013<br/>
|
||||
Beijing, 2013.10<br/>
|
||||
Winlin
|
||||
|
||||
|
||||
|
|
|
@ -2,6 +2,12 @@
|
|||
|
||||
# the listen ports, split by space.
|
||||
listen 1935;
|
||||
# the pid file
|
||||
# to ensure only one process can use a pid file
|
||||
# and provides the current running process id, for script,
|
||||
# for example, init.d script to manage the server.
|
||||
# default: ./objs/srs.pid
|
||||
pid ./objs/srs.pid;
|
||||
# the default chunk size is 128, max is 65536,
|
||||
# some client does not support chunk size change,
|
||||
# however, most clients supports it and it can improve
|
||||
|
|
|
@ -682,6 +682,7 @@ int SrsConfig::parse_file(const char* filename)
|
|||
// TODO: check forward.
|
||||
// TODO: check ffmpeg.
|
||||
// TODO: check http.
|
||||
// TODO: check pid.
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -1440,6 +1441,17 @@ SrsConfDirective* SrsConfig::get_listen()
|
|||
return root->get("listen");
|
||||
}
|
||||
|
||||
string SrsConfig::get_pid_file()
|
||||
{
|
||||
SrsConfDirective* conf = root->get("pid");
|
||||
|
||||
if (!conf) {
|
||||
return SRS_CONF_DEFAULT_PID_FILE;
|
||||
}
|
||||
|
||||
return conf->arg0();
|
||||
}
|
||||
|
||||
int SrsConfig::get_chunk_size(const std::string &vhost)
|
||||
{
|
||||
SrsConfDirective* conf = get_vhost(vhost);
|
||||
|
|
|
@ -38,6 +38,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#define RTMP_VHOST_DEFAULT "__defaultVhost__"
|
||||
|
||||
#define SRS_LOCALHOST "127.0.0.1"
|
||||
#define SRS_CONF_DEFAULT_PID_FILE "./objs/srs.pid"
|
||||
|
||||
#define SRS_CONF_DEFAULT_HLS_PATH "./objs/nginx/html"
|
||||
#define SRS_CONF_DEFAULT_HLS_FRAGMENT 10
|
||||
|
@ -162,6 +163,7 @@ public:
|
|||
virtual SrsConfDirective* get_refer_play(std::string vhost);
|
||||
virtual SrsConfDirective* get_refer_publish(std::string vhost);
|
||||
virtual SrsConfDirective* get_listen();
|
||||
virtual std::string get_pid_file();
|
||||
virtual int get_chunk_size(const std::string& vhost);
|
||||
virtual int get_pithy_print_publish();
|
||||
virtual int get_pithy_print_forwarder();
|
||||
|
|
|
@ -27,6 +27,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
@ -198,6 +201,81 @@ int SrsServer::initialize()
|
|||
return ret;
|
||||
}
|
||||
|
||||
int SrsServer::acquire_pid_file()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
std::string pid_file = _srs_config->get_pid_file();
|
||||
|
||||
// -rw-r--r--
|
||||
// 644
|
||||
int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
||||
|
||||
int fd;
|
||||
// open pid file
|
||||
if ((fd = ::open(pid_file.c_str(), O_WRONLY | O_CREAT, mode)) < 0) {
|
||||
ret = ERROR_SYSTEM_PID_ACQUIRE;
|
||||
srs_error("open pid file %s error, ret=%#x", pid_file.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// require write lock
|
||||
flock lock;
|
||||
|
||||
lock.l_type = F_WRLCK; // F_RDLCK, F_WRLCK, F_UNLCK
|
||||
lock.l_start = 0; // type offset, relative to l_whence
|
||||
lock.l_whence = SEEK_SET; // SEEK_SET, SEEK_CUR, SEEK_END
|
||||
lock.l_len = 0;
|
||||
|
||||
if (fcntl(fd, F_SETLK, &lock) < 0) {
|
||||
if(errno == EACCES || errno == EAGAIN) {
|
||||
ret = ERROR_SYSTEM_PID_ALREADY_RUNNING;
|
||||
srs_error("srs is already running! ret=%#x", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = ERROR_SYSTEM_PID_LOCK;
|
||||
srs_error("require lock for file %s error! ret=%#x", pid_file.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// truncate file
|
||||
if (ftruncate(fd, 0) < 0) {
|
||||
ret = ERROR_SYSTEM_PID_TRUNCATE_FILE;
|
||||
srs_error("truncate pid file %s error! ret=%#x", pid_file.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int pid = (int)getpid();
|
||||
|
||||
// write the pid
|
||||
char buf[512];
|
||||
snprintf(buf, sizeof(buf), "%d", pid);
|
||||
if (write(fd, buf, strlen(buf)) != (int)strlen(buf)) {
|
||||
ret = ERROR_SYSTEM_PID_WRITE_FILE;
|
||||
srs_error("write our pid error! pid=%d file=%s ret=%#x", pid, pid_file.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// auto close when fork child process.
|
||||
int val;
|
||||
if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
|
||||
ret = ERROR_SYSTEM_PID_GET_FILE_INFO;
|
||||
srs_error("fnctl F_GETFD error! file=%s ret=%#x", pid_file.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
val |= FD_CLOEXEC;
|
||||
if (fcntl(fd, F_SETFD, val) < 0) {
|
||||
ret = ERROR_SYSTEM_PID_SET_FILE_INFO;
|
||||
srs_error("fcntl F_SETFD error! file=%s ret=%#x", pid_file.c_str(), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
srs_trace("write pid=%d to %s success!", pid, pid_file.c_str());
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsServer::listen()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
|
|
@ -79,6 +79,7 @@ public:
|
|||
virtual ~SrsServer();
|
||||
public:
|
||||
virtual int initialize();
|
||||
virtual int acquire_pid_file();
|
||||
virtual int listen();
|
||||
virtual int cycle();
|
||||
virtual void remove(SrsConnection* conn);
|
||||
|
|
|
@ -92,6 +92,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#define ERROR_SYSTEM_WAITPID 413
|
||||
#define ERROR_SYSTEM_BANDWIDTH_KEY 414
|
||||
#define ERROR_SYSTEM_BANDWIDTH_DENIED 415
|
||||
#define ERROR_SYSTEM_PID_ACQUIRE 416
|
||||
#define ERROR_SYSTEM_PID_ALREADY_RUNNING 417
|
||||
#define ERROR_SYSTEM_PID_LOCK 418
|
||||
#define ERROR_SYSTEM_PID_TRUNCATE_FILE 419
|
||||
#define ERROR_SYSTEM_PID_WRITE_FILE 420
|
||||
#define ERROR_SYSTEM_PID_GET_FILE_INFO 421
|
||||
#define ERROR_SYSTEM_PID_SET_FILE_INFO 422
|
||||
|
||||
// see librtmp.
|
||||
// failed when open ssl create the dh
|
||||
|
|
|
@ -91,6 +91,10 @@ int main(int argc, char** argv)
|
|||
|
||||
// TODO: create log dir in _srs_config->get_log_dir()
|
||||
|
||||
if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = _srs_server->listen()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue