-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms_deliver_c2p.cpp
executable file
·122 lines (94 loc) · 2.56 KB
/
sms_deliver_c2p.cpp
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
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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/unistd.h>
#include <dirent.h>
#include <string>
#include <stdexcept>
#include "deliver.h"
namespace SMS{
#define SIGDSNOTIFY (SIGRTMIN+1)
CDeliver _myDaemon(SMSHOME "outbox/deliver","sms_deliver_c2p",LOG_LOCAL0);
void __smsDiskStorage_notify_handler(int sig, siginfo_t *si, void *data)
{
_myDaemon.OnNotify();
}
int CDeliver::OnSignalTerm(){
syslog(LOG_ERR,"Terminated by SIGTERM(kill)");
exit(0);
return 0;
}
int CDeliver::set_notifier(){
struct sigaction act;
act.sa_sigaction = __smsDiskStorage_notify_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction(SIGDSNOTIFY, &act, NULL);
int fd=open(m_IncomingDirectory.c_str(), O_RDONLY);
syslog(LOG_ERR, "start monitor: %s", m_IncomingDirectory.c_str());
fcntl(fd, F_SETSIG, SIGDSNOTIFY );
fcntl(fd, F_NOTIFY, DN_CREATE|DN_MULTISHOT);
return 0;
}
std::string CDeliver::getDest(const std::string& filename){
return SMSHOME "outbox/18dx";
}
int CDeliver::Run(){
openlog("deliver",LOG_PID,LOG_LOCAL0);
set_notifier();
for(;;){
pause();
}
return 0;
}
int CDeliver::OnNotify(){
syslog(LOG_ERR, "there is new msg!");
DIR * m_pDIR;
try{
m_pDIR=opendir(m_IncomingDirectory.c_str());
if (m_pDIR==NULL)
{
std::string errorMsg="open dir ";
errorMsg+=m_IncomingDirectory;
errorMsg+="error" ;
throw std::runtime_error(errorMsg);
}
dirent* pDirInfo;
struct stat statInfo;
for (;;) {
pDirInfo=readdir(m_pDIR);
if (pDirInfo==NULL)
{
break;
}
syslog(LOG_ERR,"check file:%s",pDirInfo->d_name);
std::string path=m_IncomingDirectory+"/"+pDirInfo->d_name;
stat(path.c_str(),&statInfo);
if (S_ISREG(statInfo.st_mode))
{
std::string oldpath,newpath;
oldpath=m_IncomingDirectory+"/"+pDirInfo->d_name;
newpath=getDest(pDirInfo->d_name)+"/"+pDirInfo->d_name;
if (link(oldpath.c_str(),newpath.c_str())){
syslog(LOG_ERR,"can't link %s to %s: errno %d",oldpath.c_str(),newpath.c_str(),errno);
}
if (unlink(oldpath.c_str())){
syslog(LOG_ERR,"can't unlink %s : errno %d",oldpath.c_str(),errno);
}
syslog(LOG_ERR,"deliver msg: %s", pDirInfo->d_name);
}
}
} catch(std::exception e ) {
syslog(LOG_ERR, "deliver SMS error: %s", e.what());
}
if (m_pDIR) closedir(m_pDIR);
return 0;
}
}