[ci] Update macos jobs
[xapian.git] / xapian-applications / omega / assistant.cc
blob4cb444623c8b9514110aa110d757862eb8a299a6
1 /** @file
2 * @brief Worker module for putting text extraction into a separate process.
3 */
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
20 * USA
23 #include <config.h>
25 #include "worker_comms.h"
26 #include "handler.h"
27 #include "safeunistd.h"
29 #include <cerrno>
30 #include <csignal>
31 #include <iostream>
33 #include "safesysexits.h"
35 using namespace std;
37 const int FD = 3;
38 const int time_limit = 300;
40 #if defined HAVE_ALARM
42 static void
43 timeout_handler(int) {
44 _Exit(EX_TEMPFAIL);
47 static void
48 set_timeout() {
49 // Causes the system to generate a SIGALRM signal for the process after
50 // time_limit of seconds
51 alarm(time_limit);
52 // Set a signal handler for SIGALRM
53 signal(SIGALRM, timeout_handler);
56 static void
57 stop_timeout() {
58 // Set SIGALRM to be ignore
59 signal(SIGALRM, SIG_IGN);
60 // A pending alarm request, if any, is cancelled by calling alarm(0)
61 alarm(0);
64 #else
66 static void set_timeout() { }
67 static void stop_timeout() { }
69 #endif
71 static FILE* sockt;
73 void
74 send_field(Field field,
75 const char* data,
76 size_t len)
78 if (len == 0) return;
79 putc(static_cast<unsigned char>(field), sockt);
80 if (!write_string(sockt, data, len)) {
81 _Exit(OMEGA_EX_SOCKET_WRITE_ERROR);
85 static void
86 send_field_end()
88 putc(static_cast<unsigned char>(FIELD_END), sockt);
91 void
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);
101 void
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?
114 int main()
116 string filename, mimetype;
117 sockt = fdopen(FD, "r+");
119 try {
120 if (!initialise())
121 _Exit(EX_UNAVAILABLE);
122 } catch (const std::exception& e) {
123 cerr << "Initialisation failed with exception: " << e.what() << '\n';
124 _Exit(EX_UNAVAILABLE);
125 } catch (...) {
126 _Exit(EX_UNAVAILABLE);
129 while (true) {
130 // Read filename.
131 errno = 0;
132 if (!read_string(sockt, filename)) break;
133 if (!read_string(sockt, mimetype)) break;
134 // Setting a timeout for avoid infinity loops
135 set_timeout();
136 try {
137 extract(filename, mimetype);
138 } catch (...) {
139 send_field(FIELD_ERROR, "Caught C++ exception");
141 stop_timeout();
142 send_field_end();
145 _Exit(errno ? OMEGA_EX_SOCKET_READ_ERROR : EX_OK);