2 * @brief Worker module for putting text extraction into a separate process.
4 /* Copyright (C) 2011,2022,2023 Olly Betts
5 * Copyright (C) 2019 Bruno Baruffaldi
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 #include "worker_comms.h"
27 #include "safeunistd.h"
33 #include "safesysexits.h"
38 const int time_limit
= 300;
40 #if defined HAVE_ALARM
43 timeout_handler(int) {
49 // Causes the system to generate a SIGALRM signal for the process after
50 // time_limit of seconds
52 // Set a signal handler for SIGALRM
53 signal(SIGALRM
, timeout_handler
);
58 // Set SIGALRM to be ignore
59 signal(SIGALRM
, SIG_IGN
);
60 // A pending alarm request, if any, is cancelled by calling alarm(0)
66 static void set_timeout() { }
67 static void stop_timeout() { }
74 send_field(Field field
,
79 putc(static_cast<unsigned char>(field
), sockt
);
80 if (!write_string(sockt
, data
, len
)) {
81 _Exit(OMEGA_EX_SOCKET_WRITE_ERROR
);
88 putc(static_cast<unsigned char>(FIELD_END
), sockt
);
92 send_field_page_count(int value
)
94 if (value
< 0) return;
95 putc(static_cast<unsigned char>(FIELD_PAGE_COUNT
), sockt
);
96 if (!write_unsigned(sockt
, unsigned(value
))) {
97 _Exit(OMEGA_EX_SOCKET_WRITE_ERROR
);
102 send_field_created_date(time_t value
)
104 if (value
== time_t(-1)) return;
105 putc(static_cast<unsigned char>(FIELD_CREATED_DATE
), sockt
);
106 auto u_value
= static_cast<unsigned long>(value
);
107 if (!write_unsigned(sockt
, u_value
)) {
108 _Exit(OMEGA_EX_SOCKET_WRITE_ERROR
);
112 // FIXME: Restart filter every N files processed?
116 string filename
, mimetype
;
117 sockt
= fdopen(FD
, "r+");
121 _Exit(EX_UNAVAILABLE
);
122 } catch (const std::exception
& e
) {
123 cerr
<< "Initialisation failed with exception: " << e
.what() << '\n';
124 _Exit(EX_UNAVAILABLE
);
126 _Exit(EX_UNAVAILABLE
);
132 if (!read_string(sockt
, filename
)) break;
133 if (!read_string(sockt
, mimetype
)) break;
134 // Setting a timeout for avoid infinity loops
137 extract(filename
, mimetype
);
139 send_field(FIELD_ERROR
, "Caught C++ exception");
145 _Exit(errno
? OMEGA_EX_SOCKET_READ_ERROR
: EX_OK
);