works
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
// fifo_server_daemon.c
|
||||
// Демон: читает из FIFO_REQUEST, обрабатывает текст,
|
||||
// для каждой пары одинаковых подряд символов заменяет второй на пробел,
|
||||
// всего не более max_replacements замен, пишет результат в FIFO_RESPONSE
|
||||
// и дописывает "\nREPLACEMENTS:<n>\n". Логирует ошибки в syslog.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <syslog.h>
|
||||
#include <stdio.h> // fprintf
|
||||
#include <stdlib.h> // malloc, free, exit, strtoll
|
||||
#include <string.h> // strlen, strerror
|
||||
#include <unistd.h> // read, write, close, unlink
|
||||
#include <fcntl.h> // open, O_RDONLY, O_WRONLY
|
||||
#include <sys/types.h> // типы
|
||||
#include <sys/stat.h> // mkfifo
|
||||
#include <errno.h> // errno
|
||||
#include <signal.h> // signal, SIGINT, SIGTERM, SIGPIPE
|
||||
#include <syslog.h> // syslog, openlog, closelog
|
||||
|
||||
#define FIFO_REQUEST "/tmp/fifo_request"
|
||||
#define FIFO_RESPONSE "/tmp/fifo_response"
|
||||
@@ -22,13 +26,16 @@ void signal_handler(int sig) {
|
||||
running = 0;
|
||||
}
|
||||
|
||||
// разбор неотрицательного long long
|
||||
static long long parse_ll(const char *s) {
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
long long v = strtoll(s, &end, 10);
|
||||
|
||||
if (errno != 0 || end == s || *end != '\0' || v < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -176,7 +183,9 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
char result[64];
|
||||
snprintf(result, sizeof(result), "\nREPLACEMENTS:%lld\n", replacements);
|
||||
write(fd_resp, result, strlen(result));
|
||||
if (write(fd_resp, result, strlen(result)) < 0) {
|
||||
syslog(LOG_WARNING, "write replacements trailer failed: %s", strerror(errno));
|
||||
}
|
||||
|
||||
close(fd_resp);
|
||||
syslog(LOG_INFO, "Response sent: %zd bytes; replacements=%lld",
|
||||
|
||||
Reference in New Issue
Block a user