15 |
#include <fcntl.h> |
#include <fcntl.h> |
16 |
#include <stdlib.h> |
#include <stdlib.h> |
17 |
|
|
18 |
#include "util.h" |
#include "Util.h" |
19 |
|
|
20 |
#include "Exceptions.h" |
#include "Exceptions.h" |
21 |
|
|
22 |
const std::string spooldir = "/var/spool/smsdaemon/"; |
const std::string spooldir = "/home/torben/smstool"; |
23 |
|
|
24 |
using namespace std; |
using namespace std; |
25 |
|
|
40 |
lock(); |
lock(); |
41 |
std::string file = findSpoolFilename(); |
std::string file = findSpoolFilename(); |
42 |
std::ofstream out(file.c_str()); |
std::ofstream out(file.c_str()); |
43 |
if (out) { |
if (out) |
44 |
|
{ |
45 |
out << recipient << "\n" << message; |
out << recipient << "\n" << message; |
46 |
out.close(); |
out.close(); |
47 |
} else { |
} |
48 |
|
else |
49 |
|
{ |
50 |
error = true; |
error = true; |
51 |
} |
} |
52 |
unlock(); |
unlock(); |
53 |
|
|
54 |
if (error) |
if (error) |
55 |
throw std::runtime_error("Could not create spoolfile"); |
throw std::runtime_error("Could not create spoolfile"); |
56 |
this->filename = file; |
this->filename = file; |
65 |
errnoException(); |
errnoException(); |
66 |
|
|
67 |
string file; |
string file; |
68 |
string message; |
string message; |
69 |
dirent* entry; |
dirent* entry; |
70 |
while ( (entry = readdir(dir)) != 0) |
while ( (entry = readdir(dir)) != 0) |
71 |
{ |
{ |
72 |
if (entry->d_name[0] == '.' ) |
if (entry->d_name[0] == '.' ) |
73 |
{ |
{ |
74 |
continue; |
continue; |
75 |
} |
} |
83 |
|
|
84 |
closedir(dir); |
closedir(dir); |
85 |
|
|
86 |
if (file != "") { |
if (file != "") |
87 |
|
{ |
88 |
message = Util::readfile(file); |
message = Util::readfile(file); |
89 |
::unlink(file.c_str()); |
::unlink(file.c_str()); |
90 |
} |
} |
106 |
|
|
107 |
int retrycount = 0; |
int retrycount = 0; |
108 |
|
|
109 |
while(1) { |
while (1) |
110 |
|
{ |
111 |
ss.str(std::string()); //clear |
ss.str(std::string()); //clear |
112 |
ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); |
ss << std::setw(8) << std::setfill('0') << std::uppercase << std::hex << rand(); |
113 |
|
|
117 |
int fd = ::open(file.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH); |
int fd = ::open(file.c_str(), O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH); |
118 |
retrycount ++; |
retrycount ++; |
119 |
|
|
120 |
if (fd != -1) { |
if (fd != -1) |
121 |
|
{ |
122 |
::close(fd); |
::close(fd); |
123 |
return file; |
return file; |
124 |
} |
} |
125 |
|
int err = errno; |
126 |
|
::close(fd); |
127 |
|
|
128 |
if (errno == EEXIST) |
if (err == EEXIST) |
129 |
|
{ |
130 |
continue; |
continue; |
131 |
|
} |
132 |
|
|
133 |
if (retrycount > 20) |
if (retrycount > 20) |
134 |
throw std::runtime_error("to many retry attempt at creating spool file"); |
throw std::runtime_error("to many retry attempt at creating spool file"); |
135 |
|
|
136 |
throw std::runtime_error("no access to spool directory"); |
throw std::runtime_error("no access to spool directory"); |
137 |
|
|
138 |
} |
} |
139 |
} |
} |
140 |
|
|
141 |
void Spooler::lock() |
void Spooler::lock() |
142 |
{ |
{ |
143 |
fd = open(spooldir.c_str() , O_RDONLY); |
lockfd = open(spooldir.c_str() , O_RDONLY); |
144 |
|
|
145 |
if (fd == -1) { |
if (lockfd == -1) |
146 |
|
{ |
147 |
errnoException("Couldn open lockfile"); |
errnoException("Couldn open lockfile"); |
148 |
} |
} |
149 |
|
|
150 |
int status = flock(fd, LOCK_EX); |
int status = flock(lockfd, LOCK_EX); |
151 |
|
|
152 |
if (status == -1) { |
if (status == -1) |
153 |
|
{ |
154 |
errnoException(); |
errnoException(); |
155 |
} |
} |
156 |
} |
} |
157 |
|
|
158 |
void Spooler::unlock() |
void Spooler::unlock() |
159 |
{ |
{ |
160 |
flock(fd, LOCK_UN); |
flock(lockfd, LOCK_UN); |
161 |
|
close(lockfd); |
162 |
} |
} |
163 |
|
|
164 |
string Spooler::getFilename() |
string Spooler::getFilename() |