Branch data Line data Source code
1 : : /* Debuginfo-over-http server.
2 : : Copyright (C) 2019-2021 Red Hat, Inc.
3 : : Copyright (C) 2021, 2022 Mark J. Wielaard <mark@klomp.org>
4 : : This file is part of elfutils.
5 : :
6 : : This file is free software; you can redistribute it and/or modify
7 : : it under the terms of the GNU General Public License as published by
8 : : the Free Software Foundation; either version 3 of the License, or
9 : : (at your option) any later version.
10 : :
11 : : elfutils is distributed in the hope that it will be useful, but
12 : : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : GNU General Public License for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 : :
19 : :
20 : : /* cargo-cult from libdwfl linux-kernel-modules.c */
21 : : /* In case we have a bad fts we include this before config.h because it
22 : : can't handle _FILE_OFFSET_BITS.
23 : : Everything we need here is fine if its declarations just come first.
24 : : Also, include sys/types.h before fts. On some systems fts.h is not self
25 : : contained. */
26 : : #ifdef BAD_FTS
27 : : #include <sys/types.h>
28 : : #include <fts.h>
29 : : #endif
30 : :
31 : : #ifdef HAVE_CONFIG_H
32 : : #include "config.h"
33 : : #endif
34 : :
35 : : // #define _GNU_SOURCE
36 : : #ifdef HAVE_SCHED_H
37 : : extern "C" {
38 : : #include <sched.h>
39 : : }
40 : : #endif
41 : : #ifdef HAVE_SYS_RESOURCE_H
42 : : extern "C" {
43 : : #include <sys/resource.h>
44 : : }
45 : : #endif
46 : :
47 : : #ifdef HAVE_EXECINFO_H
48 : : extern "C" {
49 : : #include <execinfo.h>
50 : : }
51 : : #endif
52 : :
53 : : #include "debuginfod.h"
54 : : #include <dwarf.h>
55 : :
56 : : #include <argp.h>
57 : : #ifdef __GNUC__
58 : : #undef __attribute__ /* glibc bug - rhbz 1763325 */
59 : : #endif
60 : :
61 : : #include <unistd.h>
62 : : #include <stdlib.h>
63 : : #include <locale.h>
64 : : #include <pthread.h>
65 : : #include <signal.h>
66 : : #include <sys/stat.h>
67 : : #include <sys/time.h>
68 : : #include <sys/vfs.h>
69 : : #include <unistd.h>
70 : : #include <fcntl.h>
71 : : #include <netdb.h>
72 : :
73 : :
74 : : /* If fts.h is included before config.h, its indirect inclusions may not
75 : : give us the right LFS aliases of these functions, so map them manually. */
76 : : #ifdef BAD_FTS
77 : : #ifdef _FILE_OFFSET_BITS
78 : : #define open open64
79 : : #define fopen fopen64
80 : : #endif
81 : : #else
82 : : #include <sys/types.h>
83 : : #include <fts.h>
84 : : #endif
85 : :
86 : : #include <cstring>
87 : : #include <vector>
88 : : #include <set>
89 : : #include <unordered_set>
90 : : #include <map>
91 : : #include <string>
92 : : #include <iostream>
93 : : #include <iomanip>
94 : : #include <ostream>
95 : : #include <sstream>
96 : : #include <mutex>
97 : : #include <deque>
98 : : #include <condition_variable>
99 : : #include <exception>
100 : : #include <thread>
101 : : // #include <regex> // on rhel7 gcc 4.8, not competent
102 : : #include <regex.h>
103 : : // #include <algorithm>
104 : : using namespace std;
105 : :
106 : : #include <gelf.h>
107 : : #include <libdwelf.h>
108 : :
109 : : #include <microhttpd.h>
110 : :
111 : : #if MHD_VERSION >= 0x00097002
112 : : // libmicrohttpd 0.9.71 broke API
113 : : #define MHD_RESULT enum MHD_Result
114 : : #else
115 : : #define MHD_RESULT int
116 : : #endif
117 : :
118 : : #include <curl/curl.h>
119 : : #include <archive.h>
120 : : #include <archive_entry.h>
121 : : #include <sqlite3.h>
122 : :
123 : : #ifdef __linux__
124 : : #include <sys/syscall.h>
125 : : #endif
126 : :
127 : : #ifdef __linux__
128 : : #define tid() syscall(SYS_gettid)
129 : : #else
130 : : #define tid() pthread_self()
131 : : #endif
132 : :
133 : : extern "C" {
134 : : #include "printversion.h"
135 : : #include "system.h"
136 : : }
137 : :
138 : :
139 : : inline bool
140 : 3034 : string_endswith(const string& haystack, const string& needle)
141 : : {
142 [ + - ]: 3034 : return (haystack.size() >= needle.size() &&
143 : 3034 : equal(haystack.end()-needle.size(), haystack.end(),
144 : 3034 : needle.begin()));
145 : : }
146 : :
147 : :
148 : : // Roll this identifier for every sqlite schema incompatibility.
149 : : #define BUILDIDS "buildids10"
150 : :
151 : : #if SQLITE_VERSION_NUMBER >= 3008000
152 : : #define WITHOUT_ROWID "without rowid"
153 : : #else
154 : : #define WITHOUT_ROWID ""
155 : : #endif
156 : :
157 : : static const char DEBUGINFOD_SQLITE_DDL[] =
158 : : "pragma foreign_keys = on;\n"
159 : : "pragma synchronous = 0;\n" // disable fsync()s - this cache is disposable across a machine crash
160 : : "pragma journal_mode = wal;\n" // https://sqlite.org/wal.html
161 : : "pragma wal_checkpoint = truncate;\n" // clean out any preexisting wal file
162 : : "pragma journal_size_limit = 0;\n" // limit steady state file (between grooming, which also =truncate's)
163 : : "pragma auto_vacuum = incremental;\n" // https://sqlite.org/pragma.html
164 : : "pragma busy_timeout = 1000;\n" // https://sqlite.org/pragma.html
165 : : // NB: all these are overridable with -D option
166 : :
167 : : // Normalization table for interning file names
168 : : "create table if not exists " BUILDIDS "_fileparts (\n"
169 : : " id integer primary key not null,\n"
170 : : " name text unique not null\n"
171 : : " );\n"
172 : : "create table if not exists " BUILDIDS "_files (\n"
173 : : " id integer primary key not null,\n"
174 : : " dirname integer not null,\n"
175 : : " basename integer not null,\n"
176 : : " unique (dirname, basename),\n"
177 : : " foreign key (dirname) references " BUILDIDS "_fileparts(id) on delete cascade,\n"
178 : : " foreign key (basename) references " BUILDIDS "_fileparts(id) on delete cascade\n"
179 : : " );\n"
180 : : "create view if not exists " BUILDIDS "_files_v as\n" // a
181 : : " select f.id, n1.name || '/' || n2.name as name\n"
182 : : " from " BUILDIDS "_files f, " BUILDIDS "_fileparts n1, " BUILDIDS "_fileparts n2\n"
183 : : " where f.dirname = n1.id and f.basename = n2.id;\n"
184 : :
185 : : // Normalization table for interning buildids
186 : : "create table if not exists " BUILDIDS "_buildids (\n"
187 : : " id integer primary key not null,\n"
188 : : " hex text unique not null);\n"
189 : : // Track the completion of scanning of a given file & sourcetype at given time
190 : : "create table if not exists " BUILDIDS "_file_mtime_scanned (\n"
191 : : " mtime integer not null,\n"
192 : : " file integer not null,\n"
193 : : " size integer not null,\n" // in bytes
194 : : " sourcetype text(1) not null\n"
195 : : " check (sourcetype IN ('F', 'R')),\n"
196 : : " foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
197 : : " primary key (file, mtime, sourcetype)\n"
198 : : " ) " WITHOUT_ROWID ";\n"
199 : : "create table if not exists " BUILDIDS "_f_de (\n"
200 : : " buildid integer not null,\n"
201 : : " debuginfo_p integer not null,\n"
202 : : " executable_p integer not null,\n"
203 : : " file integer not null,\n"
204 : : " mtime integer not null,\n"
205 : : " foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
206 : : " foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
207 : : " primary key (buildid, file, mtime)\n"
208 : : " ) " WITHOUT_ROWID ";\n"
209 : : // Index for faster delete by file identifier
210 : : "create index if not exists " BUILDIDS "_f_de_idx on " BUILDIDS "_f_de (file, mtime);\n"
211 : : "create table if not exists " BUILDIDS "_f_s (\n"
212 : : " buildid integer not null,\n"
213 : : " artifactsrc integer not null,\n"
214 : : " file integer not null,\n" // NB: not necessarily entered into _mtime_scanned
215 : : " mtime integer not null,\n"
216 : : " foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
217 : : " foreign key (artifactsrc) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
218 : : " foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
219 : : " primary key (buildid, artifactsrc, file, mtime)\n"
220 : : " ) " WITHOUT_ROWID ";\n"
221 : : "create table if not exists " BUILDIDS "_r_de (\n"
222 : : " buildid integer not null,\n"
223 : : " debuginfo_p integer not null,\n"
224 : : " executable_p integer not null,\n"
225 : : " file integer not null,\n"
226 : : " mtime integer not null,\n"
227 : : " content integer not null,\n"
228 : : " foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
229 : : " foreign key (content) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
230 : : " foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
231 : : " primary key (buildid, debuginfo_p, executable_p, file, content, mtime)\n"
232 : : " ) " WITHOUT_ROWID ";\n"
233 : : // Index for faster delete by archive file identifier
234 : : "create index if not exists " BUILDIDS "_r_de_idx on " BUILDIDS "_r_de (file, mtime);\n"
235 : : "create table if not exists " BUILDIDS "_r_sref (\n" // outgoing dwarf sourcefile references from rpm
236 : : " buildid integer not null,\n"
237 : : " artifactsrc integer not null,\n"
238 : : " foreign key (artifactsrc) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
239 : : " foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
240 : : " primary key (buildid, artifactsrc)\n"
241 : : " ) " WITHOUT_ROWID ";\n"
242 : : "create table if not exists " BUILDIDS "_r_sdef (\n" // rpm contents that may satisfy sref
243 : : " file integer not null,\n"
244 : : " mtime integer not null,\n"
245 : : " content integer not null,\n"
246 : : " foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
247 : : " foreign key (content) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
248 : : " primary key (content, file, mtime)\n"
249 : : " ) " WITHOUT_ROWID ";\n"
250 : : // create views to glue together some of the above tables, for webapi D queries
251 : : "create view if not exists " BUILDIDS "_query_d as \n"
252 : : "select\n"
253 : : " b.hex as buildid, n.mtime, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1\n"
254 : : " from " BUILDIDS "_buildids b, " BUILDIDS "_files_v f0, " BUILDIDS "_f_de n\n"
255 : : " where b.id = n.buildid and f0.id = n.file and n.debuginfo_p = 1\n"
256 : : "union all select\n"
257 : : " b.hex as buildid, n.mtime, 'R' as sourcetype, f0.name as source0, n.mtime as mtime, f1.name as source1\n"
258 : : " from " BUILDIDS "_buildids b, " BUILDIDS "_files_v f0, " BUILDIDS "_files_v f1, " BUILDIDS "_r_de n\n"
259 : : " where b.id = n.buildid and f0.id = n.file and f1.id = n.content and n.debuginfo_p = 1\n"
260 : : ";"
261 : : // ... and for E queries
262 : : "create view if not exists " BUILDIDS "_query_e as \n"
263 : : "select\n"
264 : : " b.hex as buildid, n.mtime, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1\n"
265 : : " from " BUILDIDS "_buildids b, " BUILDIDS "_files_v f0, " BUILDIDS "_f_de n\n"
266 : : " where b.id = n.buildid and f0.id = n.file and n.executable_p = 1\n"
267 : : "union all select\n"
268 : : " b.hex as buildid, n.mtime, 'R' as sourcetype, f0.name as source0, n.mtime as mtime, f1.name as source1\n"
269 : : " from " BUILDIDS "_buildids b, " BUILDIDS "_files_v f0, " BUILDIDS "_files_v f1, " BUILDIDS "_r_de n\n"
270 : : " where b.id = n.buildid and f0.id = n.file and f1.id = n.content and n.executable_p = 1\n"
271 : : ";"
272 : : // ... and for S queries
273 : : "create view if not exists " BUILDIDS "_query_s as \n"
274 : : "select\n"
275 : : " b.hex as buildid, fs.name as artifactsrc, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1, null as source0ref\n"
276 : : " from " BUILDIDS "_buildids b, " BUILDIDS "_files_v f0, " BUILDIDS "_files_v fs, " BUILDIDS "_f_s n\n"
277 : : " where b.id = n.buildid and f0.id = n.file and fs.id = n.artifactsrc\n"
278 : : "union all select\n"
279 : : " b.hex as buildid, f1.name as artifactsrc, 'R' as sourcetype, f0.name as source0, sd.mtime as mtime, f1.name as source1, fsref.name as source0ref\n"
280 : : " from " BUILDIDS "_buildids b, " BUILDIDS "_files_v f0, " BUILDIDS "_files_v f1, " BUILDIDS "_files_v fsref, "
281 : : " " BUILDIDS "_r_sdef sd, " BUILDIDS "_r_sref sr, " BUILDIDS "_r_de sde\n"
282 : : " where b.id = sr.buildid and f0.id = sd.file and fsref.id = sde.file and f1.id = sd.content\n"
283 : : " and sr.artifactsrc = sd.content and sde.buildid = sr.buildid\n"
284 : : ";"
285 : : // and for startup overview counts
286 : : "drop view if exists " BUILDIDS "_stats;\n"
287 : : "create view if not exists " BUILDIDS "_stats as\n"
288 : : " select 'file d/e' as label,count(*) as quantity from " BUILDIDS "_f_de\n"
289 : : "union all select 'file s',count(*) from " BUILDIDS "_f_s\n"
290 : : "union all select 'archive d/e',count(*) from " BUILDIDS "_r_de\n"
291 : : "union all select 'archive sref',count(*) from " BUILDIDS "_r_sref\n"
292 : : "union all select 'archive sdef',count(*) from " BUILDIDS "_r_sdef\n"
293 : : "union all select 'buildids',count(*) from " BUILDIDS "_buildids\n"
294 : : "union all select 'filenames',count(*) from " BUILDIDS "_files\n"
295 : : "union all select 'fileparts',count(*) from " BUILDIDS "_fileparts\n"
296 : : "union all select 'files scanned (#)',count(*) from " BUILDIDS "_file_mtime_scanned\n"
297 : : "union all select 'files scanned (mb)',coalesce(sum(size)/1024/1024,0) from " BUILDIDS "_file_mtime_scanned\n"
298 : : #if SQLITE_VERSION_NUMBER >= 3016000
299 : : "union all select 'index db size (mb)',page_count*page_size/1024/1024 as size FROM pragma_page_count(), pragma_page_size()\n"
300 : : #endif
301 : : ";\n"
302 : :
303 : : // schema change history & garbage collection
304 : : //
305 : : // XXX: we could have migration queries here to bring prior-schema
306 : : // data over instead of just dropping it. But that could incur
307 : : // doubled storage costs.
308 : : //
309 : : // buildids10: split the _files table into _parts
310 : : "" // <<< we are here
311 : : // buildids9: widen the mtime_scanned table
312 : : "DROP VIEW IF EXISTS buildids9_stats;\n"
313 : : "DROP INDEX IF EXISTS buildids9_r_de_idx;\n"
314 : : "DROP INDEX IF EXISTS buildids9_f_de_idx;\n"
315 : : "DROP VIEW IF EXISTS buildids9_query_s;\n"
316 : : "DROP VIEW IF EXISTS buildids9_query_e;\n"
317 : : "DROP VIEW IF EXISTS buildids9_query_d;\n"
318 : : "DROP TABLE IF EXISTS buildids9_r_sdef;\n"
319 : : "DROP TABLE IF EXISTS buildids9_r_sref;\n"
320 : : "DROP TABLE IF EXISTS buildids9_r_de;\n"
321 : : "DROP TABLE IF EXISTS buildids9_f_s;\n"
322 : : "DROP TABLE IF EXISTS buildids9_f_de;\n"
323 : : "DROP TABLE IF EXISTS buildids9_file_mtime_scanned;\n"
324 : : "DROP TABLE IF EXISTS buildids9_buildids;\n"
325 : : "DROP TABLE IF EXISTS buildids9_files;\n"
326 : : // buildids8: slim the sref table
327 : : "drop table if exists buildids8_f_de;\n"
328 : : "drop table if exists buildids8_f_s;\n"
329 : : "drop table if exists buildids8_r_de;\n"
330 : : "drop table if exists buildids8_r_sref;\n"
331 : : "drop table if exists buildids8_r_sdef;\n"
332 : : "drop table if exists buildids8_file_mtime_scanned;\n"
333 : : "drop table if exists buildids8_files;\n"
334 : : "drop table if exists buildids8_buildids;\n"
335 : : // buildids7: separate _norm table into dense subtype tables
336 : : "drop table if exists buildids7_f_de;\n"
337 : : "drop table if exists buildids7_f_s;\n"
338 : : "drop table if exists buildids7_r_de;\n"
339 : : "drop table if exists buildids7_r_sref;\n"
340 : : "drop table if exists buildids7_r_sdef;\n"
341 : : "drop table if exists buildids7_file_mtime_scanned;\n"
342 : : "drop table if exists buildids7_files;\n"
343 : : "drop table if exists buildids7_buildids;\n"
344 : : // buildids6: drop bolo/rfolo again, represent sources / rpmcontents in main table
345 : : "drop table if exists buildids6_norm;\n"
346 : : "drop table if exists buildids6_files;\n"
347 : : "drop table if exists buildids6_buildids;\n"
348 : : "drop view if exists buildids6;\n"
349 : : // buildids5: redefine srcfile1 column to be '.'-less (for rpms)
350 : : "drop table if exists buildids5_norm;\n"
351 : : "drop table if exists buildids5_files;\n"
352 : : "drop table if exists buildids5_buildids;\n"
353 : : "drop table if exists buildids5_bolo;\n"
354 : : "drop table if exists buildids5_rfolo;\n"
355 : : "drop view if exists buildids5;\n"
356 : : // buildids4: introduce rpmfile RFOLO
357 : : "drop table if exists buildids4_norm;\n"
358 : : "drop table if exists buildids4_files;\n"
359 : : "drop table if exists buildids4_buildids;\n"
360 : : "drop table if exists buildids4_bolo;\n"
361 : : "drop table if exists buildids4_rfolo;\n"
362 : : "drop view if exists buildids4;\n"
363 : : // buildids3*: split out srcfile BOLO
364 : : "drop table if exists buildids3_norm;\n"
365 : : "drop table if exists buildids3_files;\n"
366 : : "drop table if exists buildids3_buildids;\n"
367 : : "drop table if exists buildids3_bolo;\n"
368 : : "drop view if exists buildids3;\n"
369 : : // buildids2: normalized buildid and filenames into interning tables;
370 : : "drop table if exists buildids2_norm;\n"
371 : : "drop table if exists buildids2_files;\n"
372 : : "drop table if exists buildids2_buildids;\n"
373 : : "drop view if exists buildids2;\n"
374 : : // buildids1: made buildid and artifacttype NULLable, to represent cached-negative
375 : : // lookups from sources, e.g. files or rpms that contain no buildid-indexable content
376 : : "drop table if exists buildids1;\n"
377 : : // buildids: original
378 : : "drop table if exists buildids;\n"
379 : : ;
380 : :
381 : : static const char DEBUGINFOD_SQLITE_CLEANUP_DDL[] =
382 : : "pragma wal_checkpoint = truncate;\n" // clean out any preexisting wal file
383 : : ;
384 : :
385 : :
386 : :
387 : :
388 : : /* Name and version of program. */
389 : : ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
390 : :
391 : : /* Bug report address. */
392 : : ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
393 : :
394 : : /* Definitions of arguments for argp functions. */
395 : : static const struct argp_option options[] =
396 : : {
397 : : { NULL, 0, NULL, 0, "Scanners:", 1 },
398 : : { "scan-file-dir", 'F', NULL, 0, "Enable ELF/DWARF file scanning.", 0 },
399 : : { "scan-rpm-dir", 'R', NULL, 0, "Enable RPM scanning.", 0 },
400 : : { "scan-deb-dir", 'U', NULL, 0, "Enable DEB scanning.", 0 },
401 : : { "scan-archive", 'Z', "EXT=CMD", 0, "Enable arbitrary archive scanning.", 0 },
402 : : // "source-oci-imageregistry" ...
403 : :
404 : : { NULL, 0, NULL, 0, "Options:", 2 },
405 : : { "logical", 'L', NULL, 0, "Follow symlinks, default=ignore.", 0 },
406 : : { "rescan-time", 't', "SECONDS", 0, "Number of seconds to wait between rescans, 0=disable.", 0 },
407 : : { "groom-time", 'g', "SECONDS", 0, "Number of seconds to wait between database grooming, 0=disable.", 0 },
408 : : { "maxigroom", 'G', NULL, 0, "Run a complete database groom/shrink pass at startup.", 0 },
409 : : { "concurrency", 'c', "NUM", 0, "Limit scanning thread concurrency to NUM, default=#CPUs.", 0 },
410 : : { "connection-pool", 'C', "NUM", OPTION_ARG_OPTIONAL,
411 : : "Use webapi connection pool with NUM threads, default=unlim.", 0 },
412 : : { "include", 'I', "REGEX", 0, "Include files matching REGEX, default=all.", 0 },
413 : : { "exclude", 'X', "REGEX", 0, "Exclude files matching REGEX, default=none.", 0 },
414 : : { "port", 'p', "NUM", 0, "HTTP port to listen on, default 8002.", 0 },
415 : : { "database", 'd', "FILE", 0, "Path to sqlite database.", 0 },
416 : : { "ddl", 'D', "SQL", 0, "Apply extra sqlite ddl/pragma to connection.", 0 },
417 : : { "verbose", 'v', NULL, 0, "Increase verbosity.", 0 },
418 : : { "regex-groom", 'r', NULL, 0,"Uses regexes from -I and -X arguments to groom the database.",0},
419 : : #define ARGP_KEY_FDCACHE_FDS 0x1001
420 : : { "fdcache-fds", ARGP_KEY_FDCACHE_FDS, "NUM", 0, "Maximum number of archive files to keep in fdcache.", 0 },
421 : : #define ARGP_KEY_FDCACHE_MBS 0x1002
422 : : { "fdcache-mbs", ARGP_KEY_FDCACHE_MBS, "MB", 0, "Maximum total size of archive file fdcache.", 0 },
423 : : #define ARGP_KEY_FDCACHE_PREFETCH 0x1003
424 : : { "fdcache-prefetch", ARGP_KEY_FDCACHE_PREFETCH, "NUM", 0, "Number of archive files to prefetch into fdcache.", 0 },
425 : : #define ARGP_KEY_FDCACHE_MINTMP 0x1004
426 : : { "fdcache-mintmp", ARGP_KEY_FDCACHE_MINTMP, "NUM", 0, "Minimum free space% on tmpdir.", 0 },
427 : : #define ARGP_KEY_FDCACHE_PREFETCH_MBS 0x1005
428 : : { "fdcache-prefetch-mbs", ARGP_KEY_FDCACHE_PREFETCH_MBS, "MB", 0,"Megabytes allocated to the \
429 : : prefetch cache.", 0},
430 : : #define ARGP_KEY_FDCACHE_PREFETCH_FDS 0x1006
431 : : { "fdcache-prefetch-fds", ARGP_KEY_FDCACHE_PREFETCH_FDS, "NUM", 0,"Number of files allocated to the \
432 : : prefetch cache.", 0},
433 : : #define ARGP_KEY_FORWARDED_TTL_LIMIT 0x1007
434 : : {"forwarded-ttl-limit", ARGP_KEY_FORWARDED_TTL_LIMIT, "NUM", 0, "Limit of X-Forwarded-For hops, default 8.", 0},
435 : : #define ARGP_KEY_PASSIVE 0x1008
436 : : { "passive", ARGP_KEY_PASSIVE, NULL, 0, "Do not scan or groom, read-only database.", 0 },
437 : : #define ARGP_KEY_DISABLE_SOURCE_SCAN 0x1009
438 : : { "disable-source-scan", ARGP_KEY_DISABLE_SOURCE_SCAN, NULL, 0, "Do not scan dwarf source info.", 0 },
439 : : #define ARGP_SCAN_CHECKPOINT 0x100A
440 : : { "scan-checkpoint", ARGP_SCAN_CHECKPOINT, "NUM", 0, "Number of files scanned before a WAL checkpoint.", 0 },
441 : : { NULL, 0, NULL, 0, NULL, 0 },
442 : : };
443 : :
444 : : /* Short description of program. */
445 : : static const char doc[] = "Serve debuginfo-related content across HTTP from files under PATHs.";
446 : :
447 : : /* Strings for arguments in help texts. */
448 : : static const char args_doc[] = "[PATH ...]";
449 : :
450 : : /* Prototype for option handler. */
451 : : static error_t parse_opt (int key, char *arg, struct argp_state *state);
452 : :
453 : : static unsigned default_concurrency();
454 : :
455 : : /* Data structure to communicate with argp functions. */
456 : : static struct argp argp =
457 : : {
458 : : options, parse_opt, args_doc, doc, NULL, NULL, NULL
459 : : };
460 : :
461 : :
462 : : static string db_path;
463 : : static sqlite3 *db; // single connection, serialized across all our threads!
464 : : static sqlite3 *dbq; // webapi query-servicing readonly connection, serialized ditto!
465 : : static unsigned verbose;
466 : : static volatile sig_atomic_t interrupted = 0;
467 : : static volatile sig_atomic_t forced_rescan_count = 0;
468 : : static volatile sig_atomic_t sigusr1 = 0;
469 : : static volatile sig_atomic_t forced_groom_count = 0;
470 : : static volatile sig_atomic_t sigusr2 = 0;
471 : : static unsigned http_port = 8002;
472 : : static unsigned rescan_s = 300;
473 : : static unsigned groom_s = 86400;
474 : : static bool maxigroom = false;
475 : : static unsigned concurrency = default_concurrency();
476 : : static int connection_pool = 0;
477 : : static set<string> source_paths;
478 : : static bool scan_files = false;
479 : : static map<string,string> scan_archives;
480 : : static vector<string> extra_ddl;
481 : : static regex_t file_include_regex;
482 : : static regex_t file_exclude_regex;
483 : : static bool regex_groom = false;
484 : : static bool traverse_logical;
485 : : static long fdcache_fds;
486 : : static long fdcache_mbs;
487 : : static long fdcache_prefetch;
488 : : static long fdcache_mintmp;
489 : : static long fdcache_prefetch_mbs;
490 : : static long fdcache_prefetch_fds;
491 : : static unsigned forwarded_ttl_limit = 8;
492 : : static bool scan_source_info = true;
493 : : static string tmpdir;
494 : : static bool passive_p = false;
495 : : static long scan_checkpoint = 256;
496 : :
497 : : static void set_metric(const string& key, double value);
498 : : // static void inc_metric(const string& key);
499 : : static void set_metric(const string& metric,
500 : : const string& lname, const string& lvalue,
501 : : double value);
502 : : static void inc_metric(const string& metric,
503 : : const string& lname, const string& lvalue);
504 : : static void add_metric(const string& metric,
505 : : const string& lname, const string& lvalue,
506 : : double value);
507 : : static void inc_metric(const string& metric,
508 : : const string& lname, const string& lvalue,
509 : : const string& rname, const string& rvalue);
510 : : static void add_metric(const string& metric,
511 : : const string& lname, const string& lvalue,
512 : : const string& rname, const string& rvalue,
513 : : double value);
514 : :
515 : :
516 : : class tmp_inc_metric { // a RAII style wrapper for exception-safe scoped increment & decrement
517 : : string m, n, v;
518 : : public:
519 : 2080 : tmp_inc_metric(const string& mname, const string& lname, const string& lvalue):
520 [ + - + - ]: 2080 : m(mname), n(lname), v(lvalue)
521 : : {
522 [ + - ]: 2080 : add_metric (m, n, v, 1);
523 [ - - - - : 2080 : }
- - ]
524 : 2080 : ~tmp_inc_metric()
525 : : {
526 : 2080 : add_metric (m, n, v, -1);
527 [ - + - + : 2080 : }
- + ]
528 : : };
529 : :
530 : : class tmp_ms_metric { // a RAII style wrapper for exception-safe scoped timing
531 : : string m, n, v;
532 : : struct timespec ts_start;
533 : : public:
534 : 98175 : tmp_ms_metric(const string& mname, const string& lname, const string& lvalue):
535 [ + - + - ]: 98175 : m(mname), n(lname), v(lvalue)
536 : : {
537 : 98188 : clock_gettime (CLOCK_MONOTONIC, & ts_start);
538 [ - - - - ]: 98194 : }
539 : 98199 : ~tmp_ms_metric()
540 : : {
541 : 98199 : struct timespec ts_end;
542 : 98199 : clock_gettime (CLOCK_MONOTONIC, & ts_end);
543 : 98200 : double deltas = (ts_end.tv_sec - ts_start.tv_sec)
544 : 98200 : + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
545 : :
546 [ + - ]: 98200 : add_metric (m + "_milliseconds_sum", n, v, (deltas*1000.0));
547 [ + + ]: 98210 : inc_metric (m + "_milliseconds_count", n, v);
548 [ + + - + : 173032 : }
- + ]
549 : : };
550 : :
551 : :
552 : : /* Handle program arguments. */
553 : : static error_t
554 : 1124 : parse_opt (int key, char *arg,
555 : : struct argp_state *state __attribute__ ((unused)))
556 : : {
557 : 1124 : int rc;
558 [ + + + + : 1124 : switch (key)
+ + + + -
+ + - - +
+ + + + +
+ + + + +
+ + - +
+ ]
559 : : {
560 : 278 : case 'v': verbose ++; break;
561 : 72 : case 'd':
562 : : /* When using the in-memory database make sure it is shareable,
563 : : so we can open it twice as read/write and read-only. */
564 [ + + ]: 72 : if (strcmp (arg, ":memory:") == 0)
565 : 1138 : db_path = "file::memory:?cache=shared";
566 : : else
567 [ + - ]: 116 : db_path = string(arg);
568 : : break;
569 : 72 : case 'p': http_port = (unsigned) atoi(arg);
570 [ + - ]: 72 : if (http_port == 0 || http_port > 65535)
571 : 0 : argp_failure(state, 1, EINVAL, "port number");
572 : : break;
573 : 48 : case 'F': scan_files = true; break;
574 : 22 : case 'R':
575 [ + - + - : 22 : scan_archives[".rpm"]="cat"; // libarchive groks rpm natively
- + ]
576 : 22 : break;
577 : 14 : case 'U':
578 [ + - + - : 14 : scan_archives[".deb"]="(bsdtar -O -x -f - data.tar\\*)<";
- + ]
579 [ + - + - : 14 : scan_archives[".ddeb"]="(bsdtar -O -x -f - data.tar\\*)<";
- + ]
580 [ + - + - : 14 : scan_archives[".ipk"]="(bsdtar -O -x -f - data.tar\\*)<";
- + ]
581 : : // .udeb too?
582 : 14 : break;
583 : 38 : case 'Z':
584 : 38 : {
585 [ - + ]: 38 : char* extension = strchr(arg, '=');
586 [ - + ]: 38 : if (arg[0] == '\0')
587 : 0 : argp_failure(state, 1, EINVAL, "missing EXT");
588 [ + + ]: 38 : else if (extension)
589 [ + - + - : 20 : scan_archives[string(arg, (extension-arg))]=string(extension+1);
- + - + -
- ]
590 : : else
591 [ + - + - : 18 : scan_archives[string(arg)]=string("cat");
- + - + -
- ]
592 : : }
593 : : break;
594 : 8 : case 'L':
595 [ - + ]: 8 : if (passive_p)
596 : 0 : argp_failure(state, 1, EINVAL, "-L option inconsistent with passive mode");
597 : 8 : traverse_logical = true;
598 : 8 : break;
599 : 0 : case 'D':
600 [ # # ]: 0 : if (passive_p)
601 : 0 : argp_failure(state, 1, EINVAL, "-D option inconsistent with passive mode");
602 [ # # ]: 0 : extra_ddl.push_back(string(arg));
603 : 0 : break;
604 : 58 : case 't':
605 [ - + ]: 58 : if (passive_p)
606 : 0 : argp_failure(state, 1, EINVAL, "-t option inconsistent with passive mode");
607 : 58 : rescan_s = (unsigned) atoi(arg);
608 : 58 : break;
609 : 58 : case 'g':
610 [ - + ]: 58 : if (passive_p)
611 : 0 : argp_failure(state, 1, EINVAL, "-g option inconsistent with passive mode");
612 : 58 : groom_s = (unsigned) atoi(arg);
613 : 58 : break;
614 : 0 : case 'G':
615 [ # # ]: 0 : if (passive_p)
616 : 0 : argp_failure(state, 1, EINVAL, "-G option inconsistent with passive mode");
617 : 0 : maxigroom = true;
618 : 0 : break;
619 : 0 : case 'c':
620 [ # # ]: 0 : if (passive_p)
621 : 0 : argp_failure(state, 1, EINVAL, "-c option inconsistent with passive mode");
622 : 0 : concurrency = (unsigned) atoi(arg);
623 [ # # ]: 0 : if (concurrency < 1) concurrency = 1;
624 : : break;
625 : 6 : case 'C':
626 [ + + ]: 6 : if (arg)
627 : : {
628 : 4 : connection_pool = atoi(arg);
629 [ + - ]: 4 : if (connection_pool < 2)
630 : 0 : argp_failure(state, 1, EINVAL, "-C NUM minimum 2");
631 : : }
632 : : break;
633 : 4 : case 'I':
634 : : // NB: no problem with unconditional free here - an earlier failed regcomp would exit program
635 [ - + ]: 4 : if (passive_p)
636 : 0 : argp_failure(state, 1, EINVAL, "-I option inconsistent with passive mode");
637 : 4 : regfree (&file_include_regex);
638 : 4 : rc = regcomp (&file_include_regex, arg, REG_EXTENDED|REG_NOSUB);
639 [ + - ]: 4 : if (rc != 0)
640 : 0 : argp_failure(state, 1, EINVAL, "regular expression");
641 : : break;
642 : 6 : case 'X':
643 [ - + ]: 6 : if (passive_p)
644 : 0 : argp_failure(state, 1, EINVAL, "-X option inconsistent with passive mode");
645 : 6 : regfree (&file_exclude_regex);
646 : 6 : rc = regcomp (&file_exclude_regex, arg, REG_EXTENDED|REG_NOSUB);
647 [ + - ]: 6 : if (rc != 0)
648 : 0 : argp_failure(state, 1, EINVAL, "regular expression");
649 : : break;
650 : 4 : case 'r':
651 [ - + ]: 4 : if (passive_p)
652 : 0 : argp_failure(state, 1, EINVAL, "-r option inconsistent with passive mode");
653 : 4 : regex_groom = true;
654 : 4 : break;
655 : 10 : case ARGP_KEY_FDCACHE_FDS:
656 : 10 : fdcache_fds = atol (arg);
657 : 10 : break;
658 : 4 : case ARGP_KEY_FDCACHE_MBS:
659 : 4 : fdcache_mbs = atol (arg);
660 : 4 : break;
661 : 2 : case ARGP_KEY_FDCACHE_PREFETCH:
662 : 2 : fdcache_prefetch = atol (arg);
663 : 2 : break;
664 : 2 : case ARGP_KEY_FDCACHE_MINTMP:
665 : 2 : fdcache_mintmp = atol (arg);
666 [ + - ]: 2 : if( fdcache_mintmp > 100 || fdcache_mintmp < 0 )
667 : 0 : argp_failure(state, 1, EINVAL, "fdcache mintmp percent");
668 : : break;
669 : 4 : case ARGP_KEY_FORWARDED_TTL_LIMIT:
670 : 4 : forwarded_ttl_limit = (unsigned) atoi(arg);
671 : 4 : break;
672 : 100 : case ARGP_KEY_ARG:
673 [ + - ]: 100 : source_paths.insert(string(arg));
674 : 100 : break;
675 : 10 : case ARGP_KEY_FDCACHE_PREFETCH_FDS:
676 : 10 : fdcache_prefetch_fds = atol(arg);
677 [ + - ]: 10 : if ( fdcache_prefetch_fds < 0)
678 : 0 : argp_failure(state, 1, EINVAL, "fdcache prefetch fds");
679 : : break;
680 : 2 : case ARGP_KEY_FDCACHE_PREFETCH_MBS:
681 : 2 : fdcache_prefetch_mbs = atol(arg);
682 [ + - ]: 2 : if ( fdcache_prefetch_mbs < 0)
683 : 0 : argp_failure(state, 1, EINVAL, "fdcache prefetch mbs");
684 : : break;
685 : 2 : case ARGP_KEY_PASSIVE:
686 : 2 : passive_p = true;
687 [ + - ]: 2 : if (source_paths.size() > 0
688 [ + - ]: 2 : || maxigroom
689 [ + - ]: 2 : || extra_ddl.size() > 0
690 [ + - + - ]: 4 : || traverse_logical)
691 : : // other conflicting options tricky to check
692 : 0 : argp_failure(state, 1, EINVAL, "inconsistent options with passive mode");
693 : : break;
694 : 0 : case ARGP_KEY_DISABLE_SOURCE_SCAN:
695 : 0 : scan_source_info = false;
696 : 0 : break;
697 : 2 : case ARGP_SCAN_CHECKPOINT:
698 : 2 : scan_checkpoint = atol (arg);
699 [ + - ]: 2 : if (scan_checkpoint < 0)
700 : 0 : argp_failure(state, 1, EINVAL, "scan checkpoint");
701 : : break;
702 : : // case 'h': argp_state_help (state, stderr, ARGP_HELP_LONG|ARGP_HELP_EXIT_OK);
703 : : default: return ARGP_ERR_UNKNOWN;
704 : : }
705 : :
706 : : return 0;
707 : : }
708 : :
709 : :
710 : : ////////////////////////////////////////////////////////////////////////
711 : :
712 : :
713 : : static void add_mhd_response_header (struct MHD_Response *r,
714 : : const char *h, const char *v);
715 : :
716 : : // represent errors that may get reported to an ostream and/or a libmicrohttpd connection
717 : :
718 : 8 : struct reportable_exception
719 : : {
720 : : int code;
721 : : string message;
722 : :
723 [ - - + - : 86 : reportable_exception(int c, const string& m): code(c), message(m) {}
- - + - +
- ]
724 [ - - - - : 604 : reportable_exception(const string& m): code(503), message(m) {}
- - - - +
- - - - -
+ - - - -
- - - + -
- - ]
725 : : reportable_exception(): code(503), message() {}
726 : :
727 : : void report(ostream& o) const; // defined under obatched() class below
728 : :
729 : 634 : MHD_RESULT mhd_send_response(MHD_Connection* c) const {
730 : 1268 : MHD_Response* r = MHD_create_response_from_buffer (message.size(),
731 : 634 : (void*) message.c_str(),
732 : : MHD_RESPMEM_MUST_COPY);
733 : 634 : add_mhd_response_header (r, "Content-Type", "text/plain");
734 : 634 : MHD_RESULT rc = MHD_queue_response (c, code, r);
735 : 634 : MHD_destroy_response (r);
736 : 634 : return rc;
737 : : }
738 : : };
739 : :
740 : :
741 : : struct sqlite_exception: public reportable_exception
742 : : {
743 : 0 : sqlite_exception(int rc, const string& msg):
744 [ # # # # : 0 : reportable_exception(string("sqlite3 error: ") + msg + ": " + string(sqlite3_errstr(rc) ?: "?")) {
# # # # #
# # # # #
# # # # #
# ]
745 [ # # # # : 0 : inc_metric("error_count","sqlite3",sqlite3_errstr(rc));
# # # # #
# # # # #
# # # # ]
746 [ # # ]: 0 : }
747 : : };
748 : :
749 [ + - - - ]: 4 : struct libc_exception: public reportable_exception
750 : : {
751 : 594 : libc_exception(int rc, const string& msg):
752 [ - + + - : 2376 : reportable_exception(string("libc error: ") + msg + ": " + string(strerror(rc) ?: "?")) {
+ - + - +
- - + - +
- + + - -
- ]
753 [ + - + - : 1188 : inc_metric("error_count","libc",strerror(rc));
+ - + - -
+ + - - -
- - ]
754 [ - - ]: 594 : }
755 : : };
756 : :
757 : :
758 : : struct archive_exception: public reportable_exception
759 : : {
760 : 0 : archive_exception(const string& msg):
761 [ # # # # : 0 : reportable_exception(string("libarchive error: ") + msg) {
# # ]
762 [ # # # # : 0 : inc_metric("error_count","libarchive",msg);
# # # # #
# ]
763 [ # # ]: 0 : }
764 : 0 : archive_exception(struct archive* a, const string& msg):
765 [ # # # # : 0 : reportable_exception(string("libarchive error: ") + msg + ": " + string(archive_error_string(a) ?: "?")) {
# # # # #
# # # # #
# # # # #
# ]
766 [ # # # # : 0 : inc_metric("error_count","libarchive",msg + ": " + string(archive_error_string(a) ?: "?"));
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
767 [ # # ]: 0 : }
768 : : };
769 : :
770 : :
771 : : struct elfutils_exception: public reportable_exception
772 : : {
773 : 0 : elfutils_exception(int rc, const string& msg):
774 [ # # # # : 0 : reportable_exception(string("elfutils error: ") + msg + ": " + string(elf_errmsg(rc) ?: "?")) {
# # # # #
# # # # #
# # # # #
# ]
775 [ # # # # : 0 : inc_metric("error_count","elfutils",elf_errmsg(rc));
# # # # #
# # # # #
# # # # ]
776 [ # # ]: 0 : }
777 : : };
778 : :
779 : :
780 : : ////////////////////////////////////////////////////////////////////////
781 : :
782 : : template <typename Payload>
783 : : class workq
784 : : {
785 : : unordered_set<Payload> q; // eliminate duplicates
786 : : mutex mtx;
787 : : condition_variable cv;
788 : : bool dead;
789 : : unsigned idlers; // number of threads busy with wait_idle / done_idle
790 : : unsigned fronters; // number of threads busy with wait_front / done_front
791 : :
792 : : public:
793 : 72 : workq() { dead = false; idlers = 0; fronters = 0; }
794 : 72 : ~workq() {}
795 : :
796 : 940 : void push_back(const Payload& p)
797 : : {
798 : 940 : unique_lock<mutex> lock(mtx);
799 [ + - ]: 940 : q.insert (p);
800 [ + - + - : 1880 : set_metric("thread_work_pending","role","scan", q.size());
+ - + - -
+ - + - -
- - ]
801 : 940 : cv.notify_all();
802 : 940 : }
803 : :
804 : : // kill this workqueue, wake up all idlers / scanners
805 : 72 : void nuke() {
806 : 72 : unique_lock<mutex> lock(mtx);
807 : : // optional: q.clear();
808 : 72 : dead = true;
809 : 72 : cv.notify_all();
810 : 72 : }
811 : :
812 : : // clear the workqueue, when scanning is interrupted with USR2
813 : 0 : void clear() {
814 : 0 : unique_lock<mutex> lock(mtx);
815 : 0 : q.clear();
816 [ # # # # : 0 : set_metric("thread_work_pending","role","scan", q.size());
# # # # #
# # # # #
# # ]
817 : : // NB: there may still be some live fronters
818 : 0 : cv.notify_all(); // maybe wake up waiting idlers
819 : 0 : }
820 : :
821 : : // block this scanner thread until there is work to do and no active idler
822 : 1196 : bool wait_front (Payload& p)
823 : : {
824 : 1196 : unique_lock<mutex> lock(mtx);
825 [ + + + + : 4672 : while (!dead && (q.size() == 0 || idlers > 0))
+ + ]
826 [ + - ]: 3476 : cv.wait(lock);
827 [ + + ]: 1196 : if (dead)
828 : : return false;
829 : : else
830 : : {
831 [ + - ]: 940 : p = * q.begin();
832 : 940 : q.erase (q.begin());
833 : 940 : fronters ++; // prevent idlers from starting awhile, even if empty q
834 [ + - + - : 1880 : set_metric("thread_work_pending","role","scan", q.size());
+ - + - -
+ - + - -
- - - - ]
835 : : // NB: don't wake up idlers yet! The consumer is busy
836 : : // processing this element until it calls done_front().
837 : 940 : return true;
838 : : }
839 : 1196 : }
840 : :
841 : : // notify waitq that scanner thread is done with that last item
842 : 940 : void done_front ()
843 : : {
844 : 940 : unique_lock<mutex> lock(mtx);
845 : 940 : fronters --;
846 [ + + + + ]: 940 : if (q.size() == 0 && fronters == 0)
847 : 96 : cv.notify_all(); // maybe wake up waiting idlers
848 : 940 : }
849 : :
850 : : // block this idler thread until there is no work to do
851 : 570 : void wait_idle ()
852 : : {
853 : 570 : unique_lock<mutex> lock(mtx);
854 : 570 : cv.notify_all(); // maybe wake up waiting scanners
855 [ + + + + : 634 : while (!dead && ((q.size() != 0) || fronters > 0))
+ + ]
856 [ + - ]: 64 : cv.wait(lock);
857 [ + - ]: 570 : idlers ++;
858 : 570 : }
859 : :
860 : 500 : void done_idle ()
861 : : {
862 : 500 : unique_lock<mutex> lock(mtx);
863 : 500 : idlers --;
864 : 500 : cv.notify_all(); // maybe wake up waiting scanners, but probably not (shutting down)
865 : 500 : }
866 : : };
867 : :
868 : : typedef struct stat stat_t;
869 : : typedef pair<string,stat_t> scan_payload;
870 : : inline bool operator< (const scan_payload& a, const scan_payload& b)
871 : : {
872 : : return a.first < b.first; // don't bother compare the stat fields
873 : : }
874 : :
875 : : namespace std { // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
876 : : template<> struct hash<::scan_payload>
877 : : {
878 : 3565 : std::size_t operator() (const ::scan_payload& p) const noexcept
879 : : {
880 [ + + + + ]: 3565 : return hash<string>()(p.first);
881 : : }
882 : : };
883 : : template<> struct equal_to<::scan_payload>
884 : : {
885 : 307 : std::size_t operator() (const ::scan_payload& a, const ::scan_payload& b) const noexcept
886 : : {
887 [ - + - - ]: 307 : return a.first == b.first;
888 : : }
889 : : };
890 : : }
891 : :
892 : : static workq<scan_payload> scanq; // just a single one
893 : : // producer & idler: thread_main_fts_source_paths()
894 : : // consumer: thread_main_scanner()
895 : : // idler: thread_main_groom()
896 : :
897 : :
898 : : ////////////////////////////////////////////////////////////////////////
899 : :
900 : : // Unique set is a thread-safe structure that lends 'ownership' of a value
901 : : // to a thread. Other threads requesting the same thing are made to wait.
902 : : // It's like a semaphore-on-demand.
903 : : template <typename T>
904 : : class unique_set
905 : : {
906 : : private:
907 : : set<T> values;
908 : : mutex mtx;
909 : : condition_variable cv;
910 : : public:
911 : 56 : unique_set() {}
912 : 56 : ~unique_set() {}
913 : :
914 : 1388 : void acquire(const T& value)
915 : : {
916 : 1388 : unique_lock<mutex> lock(mtx);
917 [ + + ]: 5788 : while (values.find(value) != values.end())
918 [ + - ]: 4400 : cv.wait(lock);
919 [ + - ]: 1388 : values.insert(value);
920 : 1388 : }
921 : :
922 : 1388 : void release(const T& value)
923 : : {
924 : 1388 : unique_lock<mutex> lock(mtx);
925 : : // assert (values.find(value) != values.end());
926 : 1388 : values.erase(value);
927 : 1388 : cv.notify_all();
928 : 1388 : }
929 : : };
930 : :
931 : :
932 : : // This is the object that's instantiate to uniquely hold a value in a
933 : : // RAII-pattern way.
934 : : template <typename T>
935 : : class unique_set_reserver
936 : : {
937 : : private:
938 : : unique_set<T>& please_hold;
939 : : T mine;
940 : : public:
941 : 1388 : unique_set_reserver(unique_set<T>& t, const T& value):
942 [ + - - - ]: 1388 : please_hold(t), mine(value) { please_hold.acquire(mine); }
943 [ + - ]: 1388 : ~unique_set_reserver() { please_hold.release(mine); }
944 : : };
945 : :
946 : :
947 : : ////////////////////////////////////////////////////////////////////////
948 : :
949 : : // periodic_barrier is a concurrency control object that lets N threads
950 : : // periodically (based on counter value) agree to wait at a barrier,
951 : : // let one of them carry out some work, then be set free
952 : :
953 : : class periodic_barrier
954 : : {
955 : : private:
956 : : unsigned period; // number of count() reports to trigger barrier activation
957 : : unsigned threads; // number of threads participating
958 : : mutex mtx; // protects all the following fields
959 : : unsigned counter; // count of count() reports in the current generation
960 : : unsigned generation; // barrier activation generation
961 : : unsigned waiting; // number of threads waiting for barrier
962 : : bool dead; // bring out your
963 : : condition_variable cv;
964 : : public:
965 : 64 : periodic_barrier(unsigned t, unsigned p):
966 : 64 : period(p), threads(t), counter(0), generation(0), waiting(0), dead(false) { }
967 : : virtual ~periodic_barrier() {}
968 : :
969 : : virtual void periodic_barrier_work() noexcept = 0;
970 : 64 : void nuke() {
971 : 64 : unique_lock<mutex> lock(mtx);
972 : 64 : dead = true;
973 : 64 : cv.notify_all();
974 : 64 : }
975 : :
976 : 1196 : void count()
977 : : {
978 : 1196 : unique_lock<mutex> lock(mtx);
979 : 1196 : unsigned prev_generation = this->generation;
980 [ + + ]: 1196 : if (counter < period-1) // normal case: counter just freely running
981 : : {
982 : 1080 : counter ++;
983 : 1080 : return;
984 : : }
985 [ + + ]: 116 : else if (counter == period-1) // we're the doer
986 : : {
987 : 30 : counter = period; // entering barrier holding phase
988 : 30 : cv.notify_all();
989 [ + + + + ]: 147 : while (waiting < threads-1 && !dead)
990 [ + - ]: 87 : cv.wait(lock);
991 : : // all other threads are now stuck in the barrier
992 : 30 : this->periodic_barrier_work(); // NB: we're holding the mutex the whole time
993 : : // reset for next barrier, releasing other waiters
994 : 30 : counter = 0;
995 : 30 : generation ++;
996 : 30 : cv.notify_all();
997 : 30 : return;
998 : : }
999 [ + - ]: 86 : else if (counter == period) // we're a waiter, in holding phase
1000 : : {
1001 : 86 : waiting ++;
1002 : 86 : cv.notify_all();
1003 [ + + + - : 318 : while (counter == period && generation == prev_generation && !dead)
+ - ]
1004 [ + - ]: 146 : cv.wait(lock);
1005 : 86 : waiting --;
1006 : 86 : return;
1007 : : }
1008 : 1196 : }
1009 : : };
1010 : :
1011 : :
1012 : :
1013 : : ////////////////////////////////////////////////////////////////////////
1014 : :
1015 : :
1016 : : // Print a standard timestamp.
1017 : : static ostream&
1018 : 22036 : timestamp (ostream &o)
1019 : : {
1020 : 22036 : char datebuf[80];
1021 : 22036 : char *now2 = NULL;
1022 : 22036 : time_t now_t = time(NULL);
1023 : 22037 : struct tm now;
1024 : 22037 : struct tm *nowp = gmtime_r (&now_t, &now);
1025 [ + - ]: 22037 : if (nowp)
1026 : : {
1027 : 22037 : (void) strftime (datebuf, sizeof (datebuf), "%c", nowp);
1028 : 22037 : now2 = datebuf;
1029 : : }
1030 : :
1031 : 22037 : return o << "[" << (now2 ? now2 : "") << "] "
1032 [ - + ]: 22037 : << "(" << getpid () << "/" << tid() << "): ";
1033 : : }
1034 : :
1035 : :
1036 : : // A little class that impersonates an ostream to the extent that it can
1037 : : // take << streaming operations. It batches up the bits into an internal
1038 : : // stringstream until it is destroyed; then flushes to the original ostream.
1039 : : // It adds a timestamp
1040 : : class obatched
1041 : : {
1042 : : private:
1043 : : ostream& o;
1044 : : stringstream stro;
1045 : : static mutex lock;
1046 : : public:
1047 : 22038 : obatched(ostream& oo, bool timestamp_p = true): o(oo)
1048 : : {
1049 [ + - ]: 22037 : if (timestamp_p)
1050 [ + - ]: 22037 : timestamp(stro);
1051 : 22038 : }
1052 : 22038 : ~obatched()
1053 : : {
1054 : 22038 : unique_lock<mutex> do_not_cross_the_streams(obatched::lock);
1055 [ + - ]: 22039 : o << stro.str();
1056 : 22039 : o.flush();
1057 : 22039 : }
1058 : : operator ostream& () { return stro; }
1059 [ - - + - : 16628 : template <typename T> ostream& operator << (const T& t) { stro << t; return stro; }
+ - + - +
- + - + -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - + -
+ - + - +
- + - + -
+ - + - -
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
- - - - -
- + - - -
+ - - - -
- - - + -
- - + - -
- + - - -
- - + - +
- + - + -
- - + - -
- - - ]
1060 : : };
1061 : : mutex obatched::lock; // just the one, since cout/cerr iostreams are not thread-safe
1062 : :
1063 : :
1064 : 696 : void reportable_exception::report(ostream& o) const {
1065 [ + - + - ]: 696 : obatched(o) << message << endl;
1066 : 696 : }
1067 : :
1068 : :
1069 : : ////////////////////////////////////////////////////////////////////////
1070 : :
1071 : :
1072 : : // RAII style sqlite prepared-statement holder that matches { } block lifetime
1073 : :
1074 : : struct sqlite_ps
1075 : : {
1076 : : private:
1077 : : sqlite3* db;
1078 : : const string nickname;
1079 : : const string sql;
1080 : : sqlite3_stmt *pp;
1081 : :
1082 : : sqlite_ps(const sqlite_ps&); // make uncopyable
1083 : : sqlite_ps& operator=(const sqlite_ps &); // make unassignable
1084 : :
1085 : : public:
1086 [ + - - - ]: 6400 : sqlite_ps (sqlite3* d, const string& n, const string& s): db(d), nickname(n), sql(s) {
1087 : : // tmp_ms_metric tick("sqlite3","prep",nickname);
1088 [ + + ]: 6400 : if (verbose > 4)
1089 [ + - + - : 348 : obatched(clog) << nickname << " prep " << sql << endl;
+ - + - +
- - - ]
1090 [ + - ]: 6400 : int rc = sqlite3_prepare_v2 (db, sql.c_str(), -1 /* to \0 */, & this->pp, NULL);
1091 [ - + ]: 6400 : if (rc != SQLITE_OK)
1092 [ # # # # ]: 0 : throw sqlite_exception(rc, "prepare " + sql);
1093 : 6400 : }
1094 : :
1095 : 53979 : sqlite_ps& reset()
1096 : : {
1097 [ + - + - : 107960 : tmp_ms_metric tick("sqlite3","reset",nickname);
- + - - ]
1098 [ + - ]: 53981 : sqlite3_reset(this->pp);
1099 : 53983 : return *this;
1100 : 53980 : }
1101 : :
1102 : 57726 : sqlite_ps& bind(int parameter, const string& str)
1103 : : {
1104 [ + + ]: 57726 : if (verbose > 4)
1105 [ + - + - : 1418 : obatched(clog) << nickname << " bind " << parameter << "=" << str << endl;
+ - + - +
- + - ]
1106 : 57726 : int rc = sqlite3_bind_text (this->pp, parameter, str.c_str(), -1, SQLITE_TRANSIENT);
1107 [ - + ]: 57720 : if (rc != SQLITE_OK)
1108 [ # # # # ]: 0 : throw sqlite_exception(rc, "sqlite3 bind");
1109 : 57720 : return *this;
1110 : : }
1111 : :
1112 : 17721 : sqlite_ps& bind(int parameter, int64_t value)
1113 : : {
1114 [ + + ]: 17721 : if (verbose > 4)
1115 [ + - + - : 636 : obatched(clog) << nickname << " bind " << parameter << "=" << value << endl;
+ - + - +
- + - ]
1116 : 17721 : int rc = sqlite3_bind_int64 (this->pp, parameter, value);
1117 [ - + ]: 17718 : if (rc != SQLITE_OK)
1118 [ # # # # ]: 0 : throw sqlite_exception(rc, "sqlite3 bind");
1119 : 17718 : return *this;
1120 : : }
1121 : :
1122 : : sqlite_ps& bind(int parameter)
1123 : : {
1124 : : if (verbose > 4)
1125 : : obatched(clog) << nickname << " bind " << parameter << "=" << "NULL" << endl;
1126 : : int rc = sqlite3_bind_null (this->pp, parameter);
1127 : : if (rc != SQLITE_OK)
1128 : : throw sqlite_exception(rc, "sqlite3 bind");
1129 : : return *this;
1130 : : }
1131 : :
1132 : :
1133 : 31656 : void step_ok_done() {
1134 [ + - + - : 63312 : tmp_ms_metric tick("sqlite3","step_done",nickname);
- + - - ]
1135 [ + - ]: 31656 : int rc = sqlite3_step (this->pp);
1136 [ + + ]: 31658 : if (verbose > 4)
1137 [ + - + - : 832 : obatched(clog) << nickname << " step-ok-done(" << sqlite3_errstr(rc) << ") " << sql << endl;
+ - + - +
- + - + -
+ - ]
1138 [ + + - + ]: 31658 : if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW)
1139 [ # # # # ]: 0 : throw sqlite_exception(rc, "sqlite3 step");
1140 [ + - ]: 31658 : (void) sqlite3_reset (this->pp);
1141 : 31658 : }
1142 : :
1143 : :
1144 : 12568 : int step() {
1145 [ + - + - : 25134 : tmp_ms_metric tick("sqlite3","step",nickname);
- + - - ]
1146 [ + - ]: 12566 : int rc = sqlite3_step (this->pp);
1147 [ + + ]: 12568 : if (verbose > 4)
1148 [ + - + - : 378 : obatched(clog) << nickname << " step(" << sqlite3_errstr(rc) << ") " << sql << endl;
+ - + - +
- + - + -
+ - ]
1149 : 12568 : return rc;
1150 : 12568 : }
1151 : :
1152 [ + + + + ]: 12716 : ~sqlite_ps () { sqlite3_finalize (this->pp); }
1153 [ + - + - : 2580 : operator sqlite3_stmt* () { return this->pp; }
+ - ]
1154 : : };
1155 : :
1156 : :
1157 : : ////////////////////////////////////////////////////////////////////////
1158 : :
1159 : :
1160 : : struct sqlite_checkpoint_pb: public periodic_barrier
1161 : : {
1162 : : // NB: don't use sqlite_ps since it can throw exceptions during ctor etc.
1163 : 64 : sqlite_checkpoint_pb(unsigned t, unsigned p):
1164 : 128 : periodic_barrier(t, p) { }
1165 : :
1166 : 30 : void periodic_barrier_work() noexcept
1167 : : {
1168 : 30 : (void) sqlite3_exec (db, "pragma wal_checkpoint(truncate);", NULL, NULL, NULL);
1169 : 30 : }
1170 : : };
1171 : :
1172 : : static periodic_barrier* scan_barrier = 0; // initialized in main()
1173 : :
1174 : :
1175 : : ////////////////////////////////////////////////////////////////////////
1176 : :
1177 : : // RAII style templated autocloser
1178 : :
1179 : : template <class Payload, class Ignore>
1180 : : struct defer_dtor
1181 : : {
1182 : : public:
1183 : : typedef Ignore (*dtor_fn) (Payload);
1184 : :
1185 : : private:
1186 : : Payload p;
1187 : : dtor_fn fn;
1188 : :
1189 : : public:
1190 : 5342 : defer_dtor(Payload _p, dtor_fn _fn): p(_p), fn(_fn) {}
1191 : 2230 : ~defer_dtor() { (void) (*fn)(p); }
1192 : :
1193 : : private:
1194 : : defer_dtor(const defer_dtor<Payload,Ignore>&); // make uncopyable
1195 : : defer_dtor& operator=(const defer_dtor<Payload,Ignore> &); // make unassignable
1196 : : };
1197 : :
1198 : :
1199 : :
1200 : : ////////////////////////////////////////////////////////////////////////
1201 : :
1202 : :
1203 : : static string
1204 : 4172 : header_censor(const string& str)
1205 : : {
1206 : 4172 : string y;
1207 [ + + ]: 35278 : for (auto&& x : str)
1208 : : {
1209 [ + + ]: 31106 : if (isalnum(x) || x == '/' || x == '.' || x == ',' || x == '_' || x == ':')
1210 [ + - ]: 62206 : y += x;
1211 : : }
1212 : 4172 : return y;
1213 : 0 : }
1214 : :
1215 : :
1216 : : static string
1217 : 2086 : conninfo (struct MHD_Connection * conn)
1218 : : {
1219 : 2086 : char hostname[256]; // RFC1035
1220 : 2086 : char servname[256];
1221 : 2086 : int sts = -1;
1222 : :
1223 [ - + ]: 2086 : if (conn == 0)
1224 : 0 : return "internal";
1225 : :
1226 : : /* Look up client address data. */
1227 : 2086 : const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
1228 : : MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1229 [ + - ]: 2086 : struct sockaddr *so = u ? u->client_addr : 0;
1230 : :
1231 [ + - - + ]: 2086 : if (so && so->sa_family == AF_INET) {
1232 : 0 : sts = getnameinfo (so, sizeof (struct sockaddr_in),
1233 : : hostname, sizeof (hostname),
1234 : : servname, sizeof (servname),
1235 : : NI_NUMERICHOST | NI_NUMERICSERV);
1236 [ + - ]: 2086 : } else if (so && so->sa_family == AF_INET6) {
1237 : 2086 : struct sockaddr_in6* addr6 = (struct sockaddr_in6*) so;
1238 [ + - + - : 2086 : if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
- + ]
1239 : 2086 : struct sockaddr_in addr4;
1240 : 2086 : memset (&addr4, 0, sizeof(addr4));
1241 : 2086 : addr4.sin_family = AF_INET;
1242 : 2086 : addr4.sin_port = addr6->sin6_port;
1243 : 2086 : memcpy (&addr4.sin_addr.s_addr, addr6->sin6_addr.s6_addr+12, sizeof(addr4.sin_addr.s_addr));
1244 : 2086 : sts = getnameinfo ((struct sockaddr*) &addr4, sizeof (addr4),
1245 : : hostname, sizeof (hostname),
1246 : : servname, sizeof (servname),
1247 : : NI_NUMERICHOST | NI_NUMERICSERV);
1248 : : } else {
1249 : 0 : sts = getnameinfo (so, sizeof (struct sockaddr_in6),
1250 : : hostname, sizeof (hostname),
1251 : : servname, sizeof (servname),
1252 : : NI_NUMERICHOST | NI_NUMERICSERV);
1253 : : }
1254 : : }
1255 : :
1256 [ - + ]: 2086 : if (sts != 0) {
1257 : 0 : hostname[0] = servname[0] = '\0';
1258 : : }
1259 : :
1260 : : // extract headers relevant to administration
1261 [ - + ]: 2086 : const char* user_agent = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
1262 [ + + ]: 2086 : const char* x_forwarded_for = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
1263 : : // NB: these are untrustworthy, beware if machine-processing log files
1264 : :
1265 [ + - + - : 6258 : return string(hostname) + string(":") + string(servname) +
+ - + - +
- + - - +
- + - + -
+ - + - +
- - - - -
- ]
1266 [ + - + - : 8650 : string(" UA:") + header_censor(string(user_agent)) +
+ - + - +
- - + + +
- + + + -
+ - - -
- ]
1267 [ + - + - : 6270 : string(" XFF:") + header_censor(string(x_forwarded_for));
+ - + + +
+ - - ]
1268 : : }
1269 : :
1270 : :
1271 : :
1272 : : ////////////////////////////////////////////////////////////////////////
1273 : :
1274 : : /* Wrapper for MHD_add_response_header that logs an error if we
1275 : : couldn't add the specified header. */
1276 : : static void
1277 : 6062 : add_mhd_response_header (struct MHD_Response *r,
1278 : : const char *h, const char *v)
1279 : : {
1280 [ - + ]: 6062 : if (MHD_add_response_header (r, h, v) == MHD_NO)
1281 [ # # # # : 0 : obatched(clog) << "Error: couldn't add '" << h << "' header" << endl;
# # ]
1282 : 6062 : }
1283 : :
1284 : : static void
1285 : 802 : add_mhd_last_modified (struct MHD_Response *resp, time_t mtime)
1286 : : {
1287 : 802 : struct tm now;
1288 : 802 : struct tm *nowp = gmtime_r (&mtime, &now);
1289 [ + - ]: 802 : if (nowp != NULL)
1290 : : {
1291 : 802 : char datebuf[80];
1292 : 802 : size_t rc = strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %T GMT",
1293 : : nowp);
1294 [ + - ]: 802 : if (rc > 0 && rc < sizeof (datebuf))
1295 : 802 : add_mhd_response_header (resp, "Last-Modified", datebuf);
1296 : : }
1297 : :
1298 : 802 : add_mhd_response_header (resp, "Cache-Control", "public");
1299 : 802 : }
1300 : :
1301 : : // quote all questionable characters of str for safe passage through a sh -c expansion.
1302 : : static string
1303 : 560 : shell_escape(const string& str)
1304 : : {
1305 : 560 : string y;
1306 [ + + ]: 78876 : for (auto&& x : str)
1307 : : {
1308 [ + + + + ]: 78316 : if (! isalnum(x) && x != '/')
1309 [ + - ]: 6622 : y += "\\";
1310 [ + - ]: 156632 : y += x;
1311 : : }
1312 : 560 : return y;
1313 : 0 : }
1314 : :
1315 : :
1316 : : // PR25548: Perform POSIX / RFC3986 style path canonicalization on the input string.
1317 : : //
1318 : : // Namely:
1319 : : // // -> /
1320 : : // /foo/../ -> /
1321 : : // /./ -> /
1322 : : //
1323 : : // This mapping is done on dwarf-side source path names, which may
1324 : : // include these constructs, so we can deal with debuginfod clients
1325 : : // that accidentally canonicalize the paths.
1326 : : //
1327 : : // realpath(3) is close but not quite right, because it also resolves
1328 : : // symbolic links. Symlinks at the debuginfod server have nothing to
1329 : : // do with the build-time symlinks, thus they must not be considered.
1330 : : //
1331 : : // see also curl Curl_dedotdotify() aka RFC3986, which we mostly follow here
1332 : : // see also libc __realpath()
1333 : : // see also llvm llvm::sys::path::remove_dots()
1334 : : static string
1335 : 3558 : canon_pathname (const string& input)
1336 : : {
1337 : 3558 : string i = input; // 5.2.4 (1)
1338 : 3558 : string o;
1339 : :
1340 : 33248 : while (i.size() != 0)
1341 : : {
1342 : : // 5.2.4 (2) A
1343 [ + - - + : 59380 : if (i.substr(0,3) == "../")
- + ]
1344 [ # # # # ]: 0 : i = i.substr(3);
1345 [ + - - + : 59380 : else if(i.substr(0,2) == "./")
- + ]
1346 [ # # # # ]: 0 : i = i.substr(2);
1347 : :
1348 : : // 5.2.4 (2) B
1349 [ + - - + : 59380 : else if (i.substr(0,3) == "/./")
+ + ]
1350 [ + - + + ]: 622 : i = i.substr(2);
1351 [ - + ]: 29308 : else if (i == "/.")
1352 [ # # ]: 0 : i = ""; // no need to handle "/." complete-path-segment case; we're dealing with file names
1353 : :
1354 : : // 5.2.4 (2) C
1355 [ + - - + : 58616 : else if (i.substr(0,4) == "/../") {
+ + ]
1356 [ + - + + ]: 472 : i = i.substr(3);
1357 : 472 : string::size_type sl = o.rfind("/");
1358 [ + - ]: 472 : if (sl != string::npos)
1359 [ + - + - ]: 944 : o = o.substr(0, sl);
1360 : : else
1361 [ # # ]: 0 : o = "";
1362 [ - + ]: 28836 : } else if (i == "/..")
1363 [ # # ]: 0 : i = ""; // no need to handle "/.." complete-path-segment case; we're dealing with file names
1364 : :
1365 : : // 5.2.4 (2) D
1366 : : // no need to handle these cases; we're dealing with file names
1367 [ - + ]: 28836 : else if (i == ".")
1368 [ # # ]: 0 : i = "";
1369 [ - + ]: 28836 : else if (i == "..")
1370 [ # # ]: 0 : i = "";
1371 : :
1372 : : // POSIX special: map // to /
1373 [ + - - + : 57672 : else if (i.substr(0,2) == "//")
+ + ]
1374 [ + - + + ]: 152 : i = i.substr(1);
1375 : :
1376 : : // 5.2.4 (2) E
1377 : : else {
1378 [ - + ]: 28700 : string::size_type next_slash = i.find("/", (i[0]=='/' ? 1 : 0)); // skip first slash
1379 [ + - + + ]: 57400 : o += i.substr(0, next_slash);
1380 [ + + ]: 28700 : if (next_slash == string::npos)
1381 [ + - + + ]: 36806 : i = "";
1382 : : else
1383 [ + - + + : 47392 : i = i.substr(next_slash);
- - ]
1384 : : }
1385 : : }
1386 : :
1387 [ + - ]: 3558 : return o;
1388 : 3558 : }
1389 : :
1390 : :
1391 : : // Estimate available free space for a given filesystem via statfs(2).
1392 : : // Return true if the free fraction is known to be smaller than the
1393 : : // given minimum percentage. Also update a related metric.
1394 : 3908 : bool statfs_free_enough_p(const string& path, const string& label, long minfree = 0)
1395 : : {
1396 : 3908 : struct statfs sfs;
1397 : 3908 : int rc = statfs(path.c_str(), &sfs);
1398 [ + + ]: 3907 : if (rc == 0)
1399 : : {
1400 : 3858 : double s = (double) sfs.f_bavail / (double) sfs.f_blocks;
1401 [ + - + - : 7716 : set_metric("filesys_free_ratio","purpose",label, s);
- + - - ]
1402 : 3858 : return ((s * 100.0) < minfree);
1403 : : }
1404 : : return false;
1405 : : }
1406 : :
1407 : :
1408 : :
1409 : : // A map-like class that owns a cache of file descriptors (indexed by
1410 : : // file / content names).
1411 : : //
1412 : : // If only it could use fd's instead of file names ... but we can't
1413 : : // dup(2) to create independent descriptors for the same unlinked
1414 : : // files, so would have to use some goofy linux /proc/self/fd/%d
1415 : : // hack such as the following
1416 : :
1417 : : #if 0
1418 : : int superdup(int fd)
1419 : : {
1420 : : #ifdef __linux__
1421 : : char *fdpath = NULL;
1422 : : int rc = asprintf(& fdpath, "/proc/self/fd/%d", fd);
1423 : : int newfd;
1424 : : if (rc >= 0)
1425 : : newfd = open(fdpath, O_RDONLY);
1426 : : else
1427 : : newfd = -1;
1428 : : free (fdpath);
1429 : : return newfd;
1430 : : #else
1431 : : return -1;
1432 : : #endif
1433 : : }
1434 : : #endif
1435 : :
1436 : : class libarchive_fdcache
1437 : : {
1438 : : private:
1439 : : mutex fdcache_lock;
1440 : :
1441 : : struct fdcache_entry
1442 : : {
1443 : : string archive;
1444 : : string entry;
1445 : : string fd;
1446 : : double fd_size_mb; // slightly rounded up megabytes
1447 : : };
1448 : : deque<fdcache_entry> lru; // @head: most recently used
1449 : : long max_fds;
1450 : : deque<fdcache_entry> prefetch; // prefetched
1451 : : long max_mbs;
1452 : : long max_prefetch_mbs;
1453 : : long max_prefetch_fds;
1454 : :
1455 : : public:
1456 : 2166 : void set_metrics()
1457 : : {
1458 : 2166 : double fdcache_mb = 0.0;
1459 : 2166 : double prefetch_mb = 0.0;
1460 [ + + + + ]: 7872 : for (auto i = lru.begin(); i < lru.end(); i++)
1461 : 3540 : fdcache_mb += i->fd_size_mb;
1462 [ + + + + ]: 5478 : for (auto j = prefetch.begin(); j < prefetch.end(); j++)
1463 : 1146 : prefetch_mb += j->fd_size_mb;
1464 [ + - ]: 2166 : set_metric("fdcache_bytes", fdcache_mb*1024.0*1024.0);
1465 [ + - ]: 2166 : set_metric("fdcache_count", lru.size());
1466 [ + - ]: 2166 : set_metric("fdcache_prefetch_bytes", prefetch_mb*1024.0*1024.0);
1467 [ + - ]: 2166 : set_metric("fdcache_prefetch_count", prefetch.size());
1468 : 2166 : }
1469 : :
1470 : 1218 : void intern(const string& a, const string& b, string fd, off_t sz, bool front_p)
1471 : : {
1472 : 1218 : {
1473 : 1218 : unique_lock<mutex> lock(fdcache_lock);
1474 : : // nuke preexisting copy
1475 [ + + + + ]: 4788 : for (auto i = lru.begin(); i < lru.end(); i++)
1476 : : {
1477 [ + + - + ]: 1176 : if (i->archive == a && i->entry == b)
1478 : : {
1479 : 0 : unlink (i->fd.c_str());
1480 : 0 : lru.erase(i);
1481 [ # # # # : 0 : inc_metric("fdcache_op_count","op","dequeue");
# # # # #
# # # # #
# # ]
1482 : 0 : break; // must not continue iterating
1483 : : }
1484 : : }
1485 : : // nuke preexisting copy in prefetch
1486 [ + + + + ]: 3536 : for (auto i = prefetch.begin(); i < prefetch.end(); i++)
1487 : : {
1488 [ + + + - ]: 550 : if (i->archive == a && i->entry == b)
1489 : : {
1490 : 0 : unlink (i->fd.c_str());
1491 : 0 : prefetch.erase(i);
1492 [ # # # # : 0 : inc_metric("fdcache_op_count","op","prefetch_dequeue");
# # # # #
# # # # #
# # ]
1493 : 0 : break; // must not continue iterating
1494 : : }
1495 : : }
1496 : 1218 : double mb = (sz+65535)/1048576.0; // round up to 64K block
1497 [ + - + - : 1218 : fdcache_entry n = { a, b, fd, mb };
+ - ]
1498 [ + + ]: 1218 : if (front_p)
1499 : : {
1500 [ + - + - : 1332 : inc_metric("fdcache_op_count","op","enqueue");
+ - + - -
+ - + - -
- - ]
1501 [ + - ]: 666 : lru.push_front(n);
1502 : : }
1503 : : else
1504 : : {
1505 [ + - + - : 1104 : inc_metric("fdcache_op_count","op","prefetch_enqueue");
+ - + - -
+ + - - -
- - ]
1506 [ + - ]: 552 : prefetch.push_front(n);
1507 : : }
1508 [ + + ]: 1218 : if (verbose > 3)
1509 [ + - + - ]: 3564 : obatched(clog) << "fdcache interned a=" << a << " b=" << b
1510 [ + - + - : 1188 : << " fd=" << fd << " mb=" << mb << " front=" << front_p << endl;
+ - + - +
- + - + -
+ - + - ]
1511 : :
1512 [ + - ]: 1218 : set_metrics();
1513 : 1218 : }
1514 : :
1515 : : // NB: we age the cache at lookup time too
1516 [ + - - + : 1218 : if (statfs_free_enough_p(tmpdir, "tmpdir", fdcache_mintmp))
- + ]
1517 : : {
1518 [ # # # # : 0 : inc_metric("fdcache_op_count","op","emerg-flush");
# # # # #
# # # #
# ]
1519 [ # # ]: 0 : obatched(clog) << "fdcache emergency flush for filling tmpdir" << endl;
1520 : 0 : this->limit(0, 0, 0, 0); // emergency flush
1521 : : }
1522 [ + + ]: 1218 : else if (front_p)
1523 : 666 : this->limit(max_fds, max_mbs, max_prefetch_fds, max_prefetch_mbs); // age cache if required
1524 : 1218 : }
1525 : :
1526 : 740 : int lookup(const string& a, const string& b)
1527 : : {
1528 : 740 : int fd = -1;
1529 : 740 : {
1530 : 740 : unique_lock<mutex> lock(fdcache_lock);
1531 [ + + + + ]: 4008 : for (auto i = lru.begin(); i < lru.end(); i++)
1532 : : {
1533 [ + + + + ]: 1320 : if (i->archive == a && i->entry == b)
1534 : : { // found it; move it to head of lru
1535 [ + - ]: 56 : fdcache_entry n = *i;
1536 : 56 : lru.erase(i); // invalidates i, so no more iteration!
1537 [ + - ]: 56 : lru.push_front(n);
1538 [ + - + - : 112 : inc_metric("fdcache_op_count","op","requeue_front");
+ - + - -
+ - + - -
- - ]
1539 [ + - ]: 56 : fd = open(n.fd.c_str(), O_RDONLY);
1540 : 56 : break;
1541 : 56 : }
1542 : : }
1543 : : // Iterate through prefetch while fd == -1 to ensure that no duplication between lru and
1544 : : // prefetch occurs.
1545 [ + + + + : 2492 : for ( auto i = prefetch.begin(); fd == -1 && i < prefetch.end(); ++i)
+ + ]
1546 : : {
1547 [ + + + + ]: 548 : if (i->archive == a && i->entry == b)
1548 : : { // found it; take the entry from the prefetch deque to the lru deque, since it has now been accessed.
1549 [ + - ]: 14 : fdcache_entry n = *i;
1550 : 14 : prefetch.erase(i);
1551 [ + - ]: 14 : lru.push_front(n);
1552 [ + - + - : 28 : inc_metric("fdcache_op_count","op","prefetch_access");
+ - + - -
+ - + - -
- - ]
1553 [ + - ]: 14 : fd = open(n.fd.c_str(), O_RDONLY);
1554 : 14 : break;
1555 : 14 : }
1556 : : }
1557 : 0 : }
1558 : :
1559 [ + - - + : 740 : if (statfs_free_enough_p(tmpdir, "tmpdir", fdcache_mintmp))
- + ]
1560 : : {
1561 [ # # # # : 0 : inc_metric("fdcache_op_count","op","emerg-flush");
# # # # #
# # # #
# ]
1562 [ # # ]: 0 : obatched(clog) << "fdcache emergency flush for filling tmpdir" << endl;
1563 : 0 : this->limit(0, 0, 0, 0); // emergency flush
1564 : : }
1565 [ + + ]: 740 : else if (fd >= 0)
1566 : 70 : this->limit(max_fds, max_mbs, max_prefetch_fds, max_prefetch_mbs); // age cache if required
1567 : :
1568 : 740 : return fd;
1569 : : }
1570 : :
1571 : 1236 : int probe(const string& a, const string& b) // just a cache residency check - don't modify LRU state, don't open
1572 : : {
1573 : 1236 : unique_lock<mutex> lock(fdcache_lock);
1574 [ + + + + ]: 4948 : for (auto i = lru.begin(); i < lru.end(); i++)
1575 : : {
1576 [ + + + + ]: 1264 : if (i->archive == a && i->entry == b)
1577 : : {
1578 [ + - + - : 52 : inc_metric("fdcache_op_count","op","probe_hit");
+ - + - -
+ - + - -
- - ]
1579 : 26 : return true;
1580 : : }
1581 : : }
1582 [ + + + + ]: 3512 : for (auto i = prefetch.begin(); i < prefetch.end(); i++)
1583 : : {
1584 [ + + + - ]: 546 : if (i->archive == a && i->entry == b)
1585 : : {
1586 [ # # # # : 0 : inc_metric("fdcache_op_count","op","prefetch_probe_hit");
# # # # #
# # # # #
# # ]
1587 : 0 : return true;
1588 : : }
1589 : : }
1590 [ + - + - : 2420 : inc_metric("fdcache_op_count","op","probe_miss");
+ - + - -
+ - + - -
- - ]
1591 : 1210 : return false;
1592 : 1236 : }
1593 : :
1594 : 0 : void clear(const string& a, const string& b)
1595 : : {
1596 : 0 : unique_lock<mutex> lock(fdcache_lock);
1597 [ # # # # ]: 0 : for (auto i = lru.begin(); i < lru.end(); i++)
1598 : : {
1599 [ # # # # ]: 0 : if (i->archive == a && i->entry == b)
1600 : : { // found it; erase it from lru
1601 [ # # ]: 0 : fdcache_entry n = *i;
1602 : 0 : lru.erase(i); // invalidates i, so no more iteration!
1603 [ # # # # : 0 : inc_metric("fdcache_op_count","op","clear");
# # # # #
# # # # #
# # ]
1604 : 0 : unlink (n.fd.c_str());
1605 [ # # ]: 0 : set_metrics();
1606 : 0 : return;
1607 : 0 : }
1608 : : }
1609 [ # # # # ]: 0 : for (auto i = prefetch.begin(); i < prefetch.end(); i++)
1610 : : {
1611 [ # # # # ]: 0 : if (i->archive == a && i->entry == b)
1612 : : { // found it; erase it from lru
1613 [ # # ]: 0 : fdcache_entry n = *i;
1614 : 0 : prefetch.erase(i); // invalidates i, so no more iteration!
1615 [ # # # # : 0 : inc_metric("fdcache_op_count","op","prefetch_clear");
# # # # #
# # # # #
# # ]
1616 : 0 : unlink (n.fd.c_str());
1617 [ # # ]: 0 : set_metrics();
1618 : 0 : return;
1619 : 0 : }
1620 : : }
1621 : 0 : }
1622 : :
1623 : 1020 : void limit(long maxfds, long maxmbs, long maxprefetchfds, long maxprefetchmbs , bool metrics_p = true)
1624 : : {
1625 [ + + + + : 1020 : if (verbose > 3 && (this->max_fds != maxfds || this->max_mbs != maxmbs))
+ + ]
1626 [ + - + - : 376 : obatched(clog) << "fdcache limited to maxfds=" << maxfds << " maxmbs=" << maxmbs << endl;
+ - + - ]
1627 : :
1628 : 1020 : unique_lock<mutex> lock(fdcache_lock);
1629 : 1020 : this->max_fds = maxfds;
1630 : 1020 : this->max_mbs = maxmbs;
1631 : 1020 : this->max_prefetch_fds = maxprefetchfds;
1632 : 1020 : this->max_prefetch_mbs = maxprefetchmbs;
1633 : 1020 : long total_fd = 0;
1634 : 1020 : double total_mb = 0.0;
1635 [ + + + + ]: 5436 : for (auto i = lru.begin(); i < lru.end(); i++)
1636 : : {
1637 : : // accumulate totals from most recently used one going backward
1638 : 2240 : total_fd ++;
1639 [ + + ]: 2240 : total_mb += i->fd_size_mb;
1640 [ + + + + ]: 2240 : if (total_fd > this->max_fds || total_mb > this->max_mbs)
1641 : : {
1642 : : // found the cut here point!
1643 : :
1644 [ + + + + ]: 2444 : for (auto j = i; j < lru.end(); j++) // close all the fds from here on in
1645 : : {
1646 [ + + ]: 680 : if (verbose > 3)
1647 [ + - + - ]: 2004 : obatched(clog) << "fdcache evicted a=" << j->archive << " b=" << j->entry
1648 [ + - + - : 668 : << " fd=" << j->fd << " mb=" << j->fd_size_mb << endl;
+ - + - +
- + - +
- ]
1649 [ + + ]: 680 : if (metrics_p)
1650 [ + - + - : 1120 : inc_metric("fdcache_op_count","op","evict");
+ - + - -
+ - + - -
- - ]
1651 : 680 : unlink (j->fd.c_str());
1652 : : }
1653 : :
1654 : 542 : lru.erase(i, lru.end()); // erase the nodes generally
1655 : 542 : break;
1656 : : }
1657 : : }
1658 : 1020 : total_fd = 0;
1659 : 1020 : total_mb = 0.0;
1660 [ + + + + ]: 2128 : for(auto i = prefetch.begin(); i < prefetch.end(); i++){
1661 : : // accumulate totals from most recently used one going backward
1662 : 568 : total_fd ++;
1663 [ + + ]: 568 : total_mb += i->fd_size_mb;
1664 [ + + + + ]: 568 : if (total_fd > this->max_prefetch_fds || total_mb > this->max_prefetch_mbs)
1665 : : {
1666 : : // found the cut here point!
1667 [ + + + + ]: 2124 : for (auto j = i; j < prefetch.end(); j++) // close all the fds from here on in
1668 : : {
1669 [ + + ]: 538 : if (verbose > 3)
1670 [ + - + - ]: 1560 : obatched(clog) << "fdcache evicted from prefetch a=" << j->archive << " b=" << j->entry
1671 [ + - + - : 520 : << " fd=" << j->fd << " mb=" << j->fd_size_mb << endl;
+ - + - +
- + - +
- ]
1672 [ + + ]: 538 : if (metrics_p)
1673 [ + - + - : 1012 : inc_metric("fdcache_op_count","op","prefetch_evict");
+ - + - -
+ - + - -
- - - - ]
1674 : 538 : unlink (j->fd.c_str());
1675 : : }
1676 : :
1677 : 524 : prefetch.erase(i, prefetch.end()); // erase the nodes generally
1678 : 524 : break;
1679 : : }
1680 : : }
1681 [ + + + - ]: 1020 : if (metrics_p) set_metrics();
1682 : 1020 : }
1683 : :
1684 : :
1685 : 72 : ~libarchive_fdcache()
1686 : : {
1687 : : // unlink any fdcache entries in $TMPDIR
1688 : : // don't update metrics; those globals may be already destroyed
1689 : 72 : limit(0, 0, 0, 0, false);
1690 : 72 : }
1691 : : };
1692 : : static libarchive_fdcache fdcache;
1693 : :
1694 : : /* Search ELF_FD for an ELF/DWARF section with name SECTION.
1695 : : If found copy the section to a temporary file and return
1696 : : its file descriptor, otherwise return -1.
1697 : :
1698 : : The temporary file's mtime will be set to PARENT_MTIME.
1699 : : B_SOURCE should be a description of the parent file suitable
1700 : : for printing to the log. */
1701 : :
1702 : : static int
1703 : 12 : extract_section (int elf_fd, int64_t parent_mtime,
1704 : : const string& b_source, const string& section)
1705 : : {
1706 : : /* Search the fdcache. */
1707 : 12 : struct stat fs;
1708 : 12 : int fd = fdcache.lookup (b_source, section);
1709 [ - + ]: 12 : if (fd >= 0)
1710 : : {
1711 [ # # ]: 0 : if (fstat (fd, &fs) != 0)
1712 : : {
1713 [ # # ]: 0 : if (verbose)
1714 [ # # ]: 0 : obatched (clog) << "cannot fstate fdcache "
1715 [ # # # # : 0 : << b_source << " " << section << endl;
# # ]
1716 : 0 : close (fd);
1717 : 0 : return -1;
1718 : : }
1719 [ # # ]: 0 : if ((int64_t) fs.st_mtime != parent_mtime)
1720 : : {
1721 [ # # ]: 0 : if (verbose)
1722 [ # # ]: 0 : obatched(clog) << "mtime mismatch for "
1723 [ # # # # : 0 : << b_source << " " << section << endl;
# # ]
1724 : 0 : close (fd);
1725 : 0 : return -1;
1726 : : }
1727 : : /* Success. */
1728 : : return fd;
1729 : : }
1730 : :
1731 : 12 : Elf *elf = elf_begin (elf_fd, ELF_C_READ_MMAP_PRIVATE, NULL);
1732 [ - + ]: 12 : if (elf == NULL)
1733 : : return -1;
1734 : :
1735 : : /* Try to find the section and copy the contents into a separate file. */
1736 : 12 : try
1737 : : {
1738 : 12 : size_t shstrndx;
1739 [ + - ]: 12 : int rc = elf_getshdrstrndx (elf, &shstrndx);
1740 [ - + ]: 12 : if (rc < 0)
1741 [ # # # # ]: 0 : throw elfutils_exception (rc, "getshdrstrndx");
1742 : :
1743 : : Elf_Scn *scn = NULL;
1744 : 432 : while (true)
1745 : : {
1746 [ + - ]: 222 : scn = elf_nextscn (elf, scn);
1747 [ + - ]: 222 : if (scn == NULL)
1748 : : break;
1749 : 222 : GElf_Shdr shdr_storage;
1750 [ + - ]: 222 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_storage);
1751 [ + - ]: 222 : if (shdr == NULL)
1752 : : break;
1753 : :
1754 [ + - ]: 222 : const char *scn_name = elf_strptr (elf, shstrndx, shdr->sh_name);
1755 [ + - ]: 222 : if (scn_name == NULL)
1756 : : break;
1757 [ + + ]: 222 : if (scn_name == section)
1758 : : {
1759 : 12 : Elf_Data *data = NULL;
1760 : :
1761 : : /* We found the desired section. */
1762 [ + - ]: 12 : data = elf_rawdata (scn, NULL);
1763 [ - + ]: 12 : if (data == NULL)
1764 [ # # # # : 0 : throw elfutils_exception (elf_errno (), "elfraw_data");
# # ]
1765 [ + + ]: 12 : if (data->d_buf == NULL)
1766 : : {
1767 [ + - + - ]: 8 : obatched(clog) << "section " << section
1768 [ + - + - ]: 4 : << " is empty" << endl;
1769 : 4 : break;
1770 : : }
1771 : :
1772 : : /* Create temporary file containing the section. */
1773 : 8 : char *tmppath = NULL;
1774 : 8 : rc = asprintf (&tmppath, "%s/debuginfod.XXXXXX", tmpdir.c_str());
1775 [ - + ]: 8 : if (rc < 0)
1776 [ # # # # ]: 0 : throw libc_exception (ENOMEM, "cannot allocate tmppath");
1777 : 8 : defer_dtor<void*,void> tmmpath_freer (tmppath, free);
1778 [ + - ]: 8 : fd = mkstemp (tmppath);
1779 [ - + ]: 8 : if (fd < 0)
1780 [ # # # # ]: 0 : throw libc_exception (errno, "cannot create temporary file");
1781 [ + - ]: 8 : ssize_t res = write_retry (fd, data->d_buf, data->d_size);
1782 [ + - - + ]: 8 : if (res < 0 || (size_t) res != data->d_size)
1783 [ # # # # ]: 0 : throw libc_exception (errno, "cannot write to temporary file");
1784 : :
1785 : : /* Set mtime to be the same as the parent file's mtime. */
1786 : 8 : struct timespec tvs[2];
1787 [ - + ]: 8 : if (fstat (elf_fd, &fs) != 0)
1788 [ # # # # ]: 0 : throw libc_exception (errno, "cannot fstat file");
1789 : :
1790 : 8 : tvs[0].tv_sec = 0;
1791 : 8 : tvs[0].tv_nsec = UTIME_OMIT;
1792 : 8 : tvs[1] = fs.st_mtim;
1793 : 8 : (void) futimens (fd, tvs);
1794 : :
1795 : : /* Add to fdcache. */
1796 [ + - + - ]: 8 : fdcache.intern (b_source, section, tmppath, data->d_size, true);
1797 : 8 : break;
1798 : 12 : }
1799 : 210 : }
1800 : : }
1801 [ - - ]: 0 : catch (const reportable_exception &e)
1802 : : {
1803 [ - - ]: 0 : e.report (clog);
1804 [ - - ]: 0 : close (fd);
1805 : 0 : fd = -1;
1806 : 0 : }
1807 : :
1808 : 12 : elf_end (elf);
1809 : : return fd;
1810 : : }
1811 : :
1812 : : static struct MHD_Response*
1813 : 74 : handle_buildid_f_match (bool internal_req_t,
1814 : : int64_t b_mtime,
1815 : : const string& b_source0,
1816 : : const string& section,
1817 : : int *result_fd)
1818 : : {
1819 : 74 : (void) internal_req_t; // ignored
1820 : 74 : int fd = open(b_source0.c_str(), O_RDONLY);
1821 [ - + ]: 74 : if (fd < 0)
1822 [ # # # # : 0 : throw libc_exception (errno, string("open ") + b_source0);
# # # # ]
1823 : :
1824 : : // NB: use manual close(2) in error case instead of defer_dtor, because
1825 : : // in the normal case, we want to hand the fd over to libmicrohttpd for
1826 : : // file transfer.
1827 : :
1828 : 74 : struct stat s;
1829 : 74 : int rc = fstat(fd, &s);
1830 [ - + ]: 74 : if (rc < 0)
1831 : : {
1832 : 0 : close(fd);
1833 [ # # # # : 0 : throw libc_exception (errno, string("fstat ") + b_source0);
# # # # ]
1834 : : }
1835 : :
1836 [ - + ]: 74 : if ((int64_t) s.st_mtime != b_mtime)
1837 : : {
1838 [ # # ]: 0 : if (verbose)
1839 [ # # # # ]: 0 : obatched(clog) << "mtime mismatch for " << b_source0 << endl;
1840 : 0 : close(fd);
1841 : 0 : return 0;
1842 : : }
1843 : :
1844 [ + + ]: 74 : if (!section.empty ())
1845 : : {
1846 : 6 : int scn_fd = extract_section (fd, s.st_mtime, b_source0, section);
1847 : 6 : close (fd);
1848 : :
1849 [ + + ]: 6 : if (scn_fd >= 0)
1850 : 4 : fd = scn_fd;
1851 : : else
1852 : : {
1853 [ + - ]: 2 : if (verbose)
1854 [ + - ]: 6 : obatched (clog) << "cannot find section " << section
1855 [ + - + - : 2 : << " for " << b_source0 << endl;
+ - ]
1856 : 2 : return 0;
1857 : : }
1858 : :
1859 : 4 : rc = fstat(fd, &s);
1860 [ - + ]: 4 : if (rc < 0)
1861 : : {
1862 : 0 : close (fd);
1863 [ # # # # : 0 : throw libc_exception (errno, string ("fstat ") + b_source0
# # # # #
# # # #
# ]
1864 [ # # # # : 0 : + string (" ") + section);
# # # # #
# ]
1865 : : }
1866 : : }
1867 : :
1868 : 72 : struct MHD_Response* r = MHD_create_response_from_fd ((uint64_t) s.st_size, fd);
1869 [ + - + - : 144 : inc_metric ("http_responses_total","result","file");
+ - - + -
+ - - -
- ]
1870 [ - + ]: 72 : if (r == 0)
1871 : : {
1872 [ # # ]: 0 : if (verbose)
1873 [ # # ]: 0 : obatched(clog) << "cannot create fd-response for " << b_source0
1874 [ # # # # : 0 : << " section=" << section << endl;
# # ]
1875 : 0 : close(fd);
1876 : : }
1877 : : else
1878 : : {
1879 : 72 : add_mhd_response_header (r, "Content-Type", "application/octet-stream");
1880 [ + - ]: 72 : add_mhd_response_header (r, "X-DEBUGINFOD-SIZE",
1881 : 72 : to_string(s.st_size).c_str());
1882 : 72 : add_mhd_response_header (r, "X-DEBUGINFOD-FILE", b_source0.c_str());
1883 : 72 : add_mhd_last_modified (r, s.st_mtime);
1884 [ + - ]: 72 : if (verbose > 1)
1885 [ + - + - : 144 : obatched(clog) << "serving file " << b_source0 << " section=" << section << endl;
+ - + - ]
1886 : : /* libmicrohttpd will close it. */
1887 [ - + ]: 72 : if (result_fd)
1888 : 72 : *result_fd = fd;
1889 : : }
1890 : :
1891 : : return r;
1892 : : }
1893 : :
1894 : : // For security/portability reasons, many distro-package archives have
1895 : : // a "./" in front of path names; others have nothing, others have
1896 : : // "/". Canonicalize them all to a single leading "/", with the
1897 : : // assumption that this matches the dwarf-derived file names too.
1898 : 3502 : string canonicalized_archive_entry_pathname(struct archive_entry *e)
1899 : : {
1900 : 3502 : string fn = archive_entry_pathname(e);
1901 [ - + ]: 3502 : if (fn.size() == 0)
1902 : 0 : return fn;
1903 [ - + ]: 3502 : if (fn[0] == '/')
1904 : 0 : return fn;
1905 [ + + ]: 3502 : if (fn[0] == '.')
1906 [ + - ]: 2296 : return fn.substr(1);
1907 : : else
1908 [ + - + - : 2412 : return string("/")+fn;
- - ]
1909 : 3502 : }
1910 : :
1911 : :
1912 : :
1913 : : static struct MHD_Response*
1914 : 786 : handle_buildid_r_match (bool internal_req_p,
1915 : : int64_t b_mtime,
1916 : : const string& b_source0,
1917 : : const string& b_source1,
1918 : : const string& section,
1919 : : int *result_fd)
1920 : : {
1921 : 786 : struct stat fs;
1922 : 786 : int rc = stat (b_source0.c_str(), &fs);
1923 [ + + ]: 786 : if (rc != 0)
1924 [ + - + - : 116 : throw libc_exception (errno, string("stat ") + b_source0);
+ - - + ]
1925 : :
1926 [ - + ]: 728 : if ((int64_t) fs.st_mtime != b_mtime)
1927 : : {
1928 [ # # ]: 0 : if (verbose)
1929 [ # # # # ]: 0 : obatched(clog) << "mtime mismatch for " << b_source0 << endl;
1930 : 0 : return 0;
1931 : : }
1932 : :
1933 : : // check for a match in the fdcache first
1934 : 728 : int fd = fdcache.lookup(b_source0, b_source1);
1935 [ + + ]: 728 : while (fd >= 0) // got one!; NB: this is really an if() with a possible branch out to the end
1936 : : {
1937 : 70 : rc = fstat(fd, &fs);
1938 [ - + ]: 70 : if (rc < 0) // disappeared?
1939 : : {
1940 [ # # ]: 0 : if (verbose)
1941 [ # # # # ]: 0 : obatched(clog) << "cannot fstat fdcache " << b_source0 << endl;
1942 : 0 : close(fd);
1943 : 0 : fdcache.clear(b_source0, b_source1);
1944 : : break; // branch out of if "loop", to try new libarchive fetch attempt
1945 : : }
1946 : :
1947 [ + + ]: 70 : if (!section.empty ())
1948 : : {
1949 [ + - + - ]: 2 : int scn_fd = extract_section (fd, fs.st_mtime,
1950 [ + - - + ]: 4 : b_source0 + ":" + b_source1,
1951 : : section);
1952 : 2 : close (fd);
1953 [ - + ]: 2 : if (scn_fd >= 0)
1954 : 0 : fd = scn_fd;
1955 : : else
1956 : : {
1957 [ + - ]: 2 : if (verbose)
1958 [ + - ]: 6 : obatched (clog) << "cannot find section " << section
1959 : : << " for archive " << b_source0
1960 [ + - + - : 2 : << " file " << b_source1 << endl;
+ - + - +
- ]
1961 : 2 : return 0;
1962 : : }
1963 : :
1964 : 0 : rc = fstat(fd, &fs);
1965 [ # # ]: 0 : if (rc < 0)
1966 : : {
1967 : 0 : close (fd);
1968 [ # # # # ]: 0 : throw libc_exception (errno,
1969 [ # # # # : 0 : string ("fstat archive ") + b_source0 + string (" file ") + b_source1
# # # # #
# # # # #
# # # # #
# # # #
# ]
1970 [ # # # # : 0 : + string (" section ") + section);
# # # # #
# ]
1971 : : }
1972 : : }
1973 : :
1974 : 68 : struct MHD_Response* r = MHD_create_response_from_fd (fs.st_size, fd);
1975 [ - + ]: 68 : if (r == 0)
1976 : : {
1977 [ # # ]: 0 : if (verbose)
1978 [ # # # # ]: 0 : obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
1979 : 0 : close(fd);
1980 : : break; // branch out of if "loop", to try new libarchive fetch attempt
1981 : : }
1982 : :
1983 [ + - + - : 136 : inc_metric ("http_responses_total","result","archive fdcache");
+ - - + -
+ - - -
- ]
1984 : :
1985 : 68 : add_mhd_response_header (r, "Content-Type", "application/octet-stream");
1986 [ + - ]: 68 : add_mhd_response_header (r, "X-DEBUGINFOD-SIZE",
1987 : 68 : to_string(fs.st_size).c_str());
1988 : 68 : add_mhd_response_header (r, "X-DEBUGINFOD-ARCHIVE", b_source0.c_str());
1989 : 68 : add_mhd_response_header (r, "X-DEBUGINFOD-FILE", b_source1.c_str());
1990 : 68 : add_mhd_last_modified (r, fs.st_mtime);
1991 [ + - ]: 68 : if (verbose > 1)
1992 [ + - ]: 204 : obatched(clog) << "serving fdcache archive " << b_source0
1993 : : << " file " << b_source1
1994 [ + - + - : 68 : << " section=" << section << endl;
+ - + - +
- ]
1995 : : /* libmicrohttpd will close it. */
1996 [ + - ]: 68 : if (result_fd)
1997 : 68 : *result_fd = fd;
1998 : : return r;
1999 : : // NB: see, we never go around the 'loop' more than once
2000 : : }
2001 : :
2002 : : // no match ... grumble, must process the archive
2003 : 658 : string archive_decoder = "/dev/null";
2004 [ + - - - ]: 658 : string archive_extension = "";
2005 [ + + ]: 1858 : for (auto&& arch : scan_archives)
2006 [ + + ]: 1200 : if (string_endswith(b_source0, arch.first))
2007 : : {
2008 [ + - ]: 658 : archive_extension = arch.first;
2009 [ + - ]: 1858 : archive_decoder = arch.second;
2010 : : }
2011 : 658 : FILE* fp;
2012 : 658 : defer_dtor<FILE*,int>::dtor_fn dfn;
2013 [ + + ]: 658 : if (archive_decoder != "cat")
2014 : : {
2015 [ + - + - : 1056 : string popen_cmd = archive_decoder + " " + shell_escape(b_source0);
+ - - + -
- - - ]
2016 [ + - ]: 528 : fp = popen (popen_cmd.c_str(), "r"); // "e" O_CLOEXEC?
2017 : 528 : dfn = pclose;
2018 [ - + ]: 528 : if (fp == NULL)
2019 [ # # # # : 0 : throw libc_exception (errno, string("popen ") + popen_cmd);
# # # # ]
2020 : 528 : }
2021 : : else
2022 : : {
2023 [ + - ]: 130 : fp = fopen (b_source0.c_str(), "r");
2024 : 130 : dfn = fclose;
2025 [ - + ]: 130 : if (fp == NULL)
2026 [ # # # # : 0 : throw libc_exception (errno, string("fopen ") + b_source0);
# # # # ]
2027 : : }
2028 : 658 : defer_dtor<FILE*,int> fp_closer (fp, dfn);
2029 : :
2030 : 658 : struct archive *a;
2031 [ + - ]: 658 : a = archive_read_new();
2032 [ - + ]: 658 : if (a == NULL)
2033 [ # # # # ]: 0 : throw archive_exception("cannot create archive reader");
2034 : 658 : defer_dtor<struct archive*,int> archive_closer (a, archive_read_free);
2035 : :
2036 [ + - ]: 658 : rc = archive_read_support_format_all(a);
2037 [ - + ]: 658 : if (rc != ARCHIVE_OK)
2038 [ # # # # ]: 0 : throw archive_exception(a, "cannot select all format");
2039 [ + - ]: 658 : rc = archive_read_support_filter_all(a);
2040 [ - + ]: 658 : if (rc != ARCHIVE_OK)
2041 [ # # # # ]: 0 : throw archive_exception(a, "cannot select all filters");
2042 : :
2043 [ + - ]: 658 : rc = archive_read_open_FILE (a, fp);
2044 [ - + ]: 658 : if (rc != ARCHIVE_OK)
2045 : : {
2046 [ # # # # : 0 : obatched(clog) << "cannot open archive from pipe " << b_source0 << endl;
# # ]
2047 [ # # # # ]: 0 : throw archive_exception(a, "cannot open archive from pipe");
2048 : : }
2049 : :
2050 : : // archive traversal is in three stages, no, four stages:
2051 : : // 1) skip entries whose names do not match the requested one
2052 : : // 2) extract the matching entry name (set r = result)
2053 : : // 3) extract some number of prefetched entries (just into fdcache)
2054 : : // 4) abort any further processing
2055 : 658 : struct MHD_Response* r = 0; // will set in stage 2
2056 [ + + ]: 658 : unsigned prefetch_count =
2057 : : internal_req_p ? 0 : fdcache_prefetch; // will decrement in stage 3
2058 : :
2059 [ + + ]: 10144 : while(r == 0 || prefetch_count > 0) // stage 1, 2, or 3
2060 : : {
2061 [ + - ]: 10124 : if (interrupted)
2062 : : break;
2063 : :
2064 : 10124 : struct archive_entry *e;
2065 [ + - ]: 10124 : rc = archive_read_next_header (a, &e);
2066 [ + + ]: 10124 : if (rc != ARCHIVE_OK)
2067 : : break;
2068 : :
2069 [ + - + + ]: 9486 : if (! S_ISREG(archive_entry_mode (e))) // skip non-files completely
2070 : 6658 : continue;
2071 : :
2072 [ + - ]: 2828 : string fn = canonicalized_archive_entry_pathname (e);
2073 [ + + + + ]: 2828 : if ((r == 0) && (fn != b_source1)) // stage 1
2074 : 1594 : continue;
2075 : :
2076 [ + - + + ]: 1234 : if (fdcache.probe (b_source0, fn) && // skip if already interned
2077 [ - + ]: 24 : fn != b_source1) // but only if we'd just be prefetching, PR29474
2078 : 24 : continue;
2079 : :
2080 : : // extract this file to a temporary file
2081 : 1210 : char* tmppath = NULL;
2082 : 1210 : rc = asprintf (&tmppath, "%s/debuginfod.XXXXXX", tmpdir.c_str());
2083 [ - + ]: 1210 : if (rc < 0)
2084 [ # # # # ]: 0 : throw libc_exception (ENOMEM, "cannot allocate tmppath");
2085 : 1210 : defer_dtor<void*,void> tmmpath_freer (tmppath, free);
2086 [ + - ]: 1210 : fd = mkstemp (tmppath);
2087 [ - + ]: 1210 : if (fd < 0)
2088 [ # # # # ]: 0 : throw libc_exception (errno, "cannot create temporary file");
2089 : : // NB: don't unlink (tmppath), as fdcache will take charge of it.
2090 : :
2091 : : // NB: this can take many uninterruptible seconds for a huge file
2092 [ + - ]: 1210 : rc = archive_read_data_into_fd (a, fd);
2093 [ - + ]: 1210 : if (rc != ARCHIVE_OK) // e.g. ENOSPC!
2094 : : {
2095 [ # # ]: 0 : close (fd);
2096 : 0 : unlink (tmppath);
2097 [ # # # # ]: 0 : throw archive_exception(a, "cannot extract file");
2098 : : }
2099 : :
2100 : : // Set the mtime so the fdcache file mtimes, even prefetched ones,
2101 : : // propagate to future webapi clients.
2102 : 1210 : struct timespec tvs[2];
2103 : 1210 : tvs[0].tv_sec = 0;
2104 : 1210 : tvs[0].tv_nsec = UTIME_OMIT;
2105 [ + - ]: 1210 : tvs[1].tv_sec = archive_entry_mtime(e);
2106 [ + - ]: 1210 : tvs[1].tv_nsec = archive_entry_mtime_nsec(e);
2107 : 1210 : (void) futimens (fd, tvs); /* best effort */
2108 : :
2109 [ + + ]: 1210 : if (r != 0) // stage 3
2110 : : {
2111 : : // NB: now we know we have a complete reusable file; make fdcache
2112 : : // responsible for unlinking it later.
2113 [ + - + - : 552 : fdcache.intern(b_source0, fn,
+ - ]
2114 : : tmppath, archive_entry_size(e),
2115 : : false); // prefetched ones go to the prefetch cache
2116 : 552 : prefetch_count --;
2117 [ + - ]: 552 : close (fd); // we're not saving this fd to make a mhd-response from!
2118 : 552 : continue;
2119 : : }
2120 : :
2121 : : // NB: now we know we have a complete reusable file; make fdcache
2122 : : // responsible for unlinking it later.
2123 [ + - + - : 658 : fdcache.intern(b_source0, b_source1,
+ - ]
2124 : : tmppath, archive_entry_size(e),
2125 : : true); // requested ones go to the front of lru
2126 : :
2127 [ + + ]: 658 : if (!section.empty ())
2128 : : {
2129 [ + - + - ]: 4 : int scn_fd = extract_section (fd, b_mtime,
2130 [ + - + - : 8 : b_source0 + ":" + b_source1,
- + ]
2131 : : section);
2132 [ + - ]: 4 : close (fd);
2133 [ + - ]: 4 : if (scn_fd >= 0)
2134 : 4 : fd = scn_fd;
2135 : : else
2136 : : {
2137 [ # # ]: 0 : if (verbose)
2138 [ # # # # ]: 0 : obatched (clog) << "cannot find section " << section
2139 : : << " for archive " << b_source0
2140 [ # # # # : 0 : << " file " << b_source1 << endl;
# # # # #
# ]
2141 : 0 : return 0;
2142 : : }
2143 : :
2144 : 4 : rc = fstat(fd, &fs);
2145 [ - + ]: 4 : if (rc < 0)
2146 : : {
2147 [ # # ]: 0 : close (fd);
2148 [ # # # # ]: 0 : throw libc_exception (errno,
2149 [ # # # # : 0 : string ("fstat ") + b_source0 + string (" ") + section);
# # # # #
# # # # #
# # # # #
# ]
2150 : : }
2151 [ + - ]: 4 : r = MHD_create_response_from_fd (fs.st_size, fd);
2152 : : }
2153 : : else
2154 [ + - + - ]: 654 : r = MHD_create_response_from_fd (archive_entry_size(e), fd);
2155 : :
2156 [ + - + - : 1316 : inc_metric ("http_responses_total","result",archive_extension + " archive");
+ - + - -
+ + + - -
- - ]
2157 [ - + ]: 658 : if (r == 0)
2158 : : {
2159 [ # # ]: 0 : if (verbose)
2160 [ # # # # : 0 : obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
# # ]
2161 [ # # ]: 0 : close(fd);
2162 : 0 : break; // assume no chance of better luck around another iteration; no other copies of same file
2163 : : }
2164 : : else
2165 : : {
2166 [ + - ]: 658 : add_mhd_response_header (r, "Content-Type",
2167 : : "application/octet-stream");
2168 [ + - ]: 658 : add_mhd_response_header (r, "X-DEBUGINFOD-SIZE",
2169 [ + - + - ]: 658 : to_string(archive_entry_size(e)).c_str());
2170 [ + - ]: 658 : add_mhd_response_header (r, "X-DEBUGINFOD-ARCHIVE", b_source0.c_str());
2171 [ + - ]: 658 : add_mhd_response_header (r, "X-DEBUGINFOD-FILE", b_source1.c_str());
2172 [ + - + - ]: 658 : add_mhd_last_modified (r, archive_entry_mtime(e));
2173 [ + - ]: 658 : if (verbose > 1)
2174 [ + - + - ]: 1974 : obatched(clog) << "serving archive " << b_source0
2175 : : << " file " << b_source1
2176 [ + - + - : 658 : << " section=" << section << endl;
+ - + - +
- ]
2177 : : /* libmicrohttpd will close it. */
2178 [ + - ]: 658 : if (result_fd)
2179 : 658 : *result_fd = fd;
2180 : 658 : continue;
2181 : : }
2182 [ - - - - : 12952 : }
+ + ]
2183 : :
2184 : : // XXX: rpm/file not found: delete this R entry?
2185 : : return r;
2186 [ - + + + ]: 1386 : }
2187 : :
2188 : :
2189 : : static struct MHD_Response*
2190 : 860 : handle_buildid_match (bool internal_req_p,
2191 : : int64_t b_mtime,
2192 : : const string& b_stype,
2193 : : const string& b_source0,
2194 : : const string& b_source1,
2195 : : const string& section,
2196 : : int *result_fd)
2197 : : {
2198 : 860 : try
2199 : : {
2200 [ + + ]: 860 : if (b_stype == "F")
2201 [ + - ]: 74 : return handle_buildid_f_match(internal_req_p, b_mtime, b_source0,
2202 : : section, result_fd);
2203 [ + - ]: 786 : else if (b_stype == "R")
2204 [ + + ]: 786 : return handle_buildid_r_match(internal_req_p, b_mtime, b_source0,
2205 : : b_source1, section, result_fd);
2206 : : }
2207 [ - + ]: 58 : catch (const reportable_exception &e)
2208 : : {
2209 [ + - ]: 58 : e.report(clog);
2210 : : // Report but swallow libc etc. errors here; let the caller
2211 : : // iterate to other matches of the content.
2212 : 58 : }
2213 : :
2214 : : return 0;
2215 : : }
2216 : :
2217 : :
2218 : : static int
2219 : 3236 : debuginfod_find_progress (debuginfod_client *, long a, long b)
2220 : : {
2221 [ - + ]: 3236 : if (verbose > 4)
2222 [ # # # # : 0 : obatched(clog) << "federated debuginfod progress=" << a << "/" << b << endl;
# # # # ]
2223 : :
2224 : 3236 : return interrupted;
2225 : : }
2226 : :
2227 : :
2228 : : // a little lru pool of debuginfod_client*s for reuse between query threads
2229 : :
2230 : : mutex dc_pool_lock;
2231 : : deque<debuginfod_client*> dc_pool;
2232 : :
2233 : 620 : debuginfod_client* debuginfod_pool_begin()
2234 : : {
2235 : 620 : unique_lock<mutex> lock(dc_pool_lock);
2236 [ + + ]: 620 : if (dc_pool.size() > 0)
2237 : : {
2238 [ + - + - : 1188 : inc_metric("dc_pool_op_count","op","begin-reuse");
+ - + - -
+ - + - -
- - ]
2239 : 594 : debuginfod_client *c = dc_pool.front();
2240 : 594 : dc_pool.pop_front();
2241 : 594 : return c;
2242 : : }
2243 [ + - + - : 52 : inc_metric("dc_pool_op_count","op","begin-new");
+ - + - -
+ - + - -
- - ]
2244 [ + - ]: 26 : return debuginfod_begin();
2245 : 620 : }
2246 : :
2247 : :
2248 : 142 : void debuginfod_pool_groom()
2249 : : {
2250 : 142 : unique_lock<mutex> lock(dc_pool_lock);
2251 [ + + ]: 168 : while (dc_pool.size() > 0)
2252 : : {
2253 [ + - + - : 52 : inc_metric("dc_pool_op_count","op","end");
+ - + - -
+ - + - -
- - ]
2254 [ + - ]: 26 : debuginfod_end(dc_pool.front());
2255 : 26 : dc_pool.pop_front();
2256 : : }
2257 : 142 : }
2258 : :
2259 : :
2260 : 620 : void debuginfod_pool_end(debuginfod_client* c)
2261 : : {
2262 : 620 : unique_lock<mutex> lock(dc_pool_lock);
2263 [ + - + - : 1240 : inc_metric("dc_pool_op_count","op","end-save");
+ - + - -
+ - + - -
- - ]
2264 [ + - ]: 620 : dc_pool.push_front(c); // accelerate reuse, vs. push_back
2265 : 620 : }
2266 : :
2267 : :
2268 : : static struct MHD_Response*
2269 : 1428 : handle_buildid (MHD_Connection* conn,
2270 : : const string& buildid /* unsafe */,
2271 : : string& artifacttype /* unsafe, cleanse on exception/return */,
2272 : : const string& suffix /* unsafe */,
2273 : : int *result_fd)
2274 : : {
2275 : : // validate artifacttype
2276 : 1428 : string atype_code;
2277 [ + + + - ]: 1428 : if (artifacttype == "debuginfo") atype_code = "D";
2278 [ + + + - ]: 690 : else if (artifacttype == "executable") atype_code = "E";
2279 [ + + + - ]: 68 : else if (artifacttype == "source") atype_code = "S";
2280 [ + + + - ]: 12 : else if (artifacttype == "section") atype_code = "I";
2281 : : else {
2282 [ + - ]: 4 : artifacttype = "invalid"; // PR28242 ensure http_resposes metrics don't propagate unclean user data
2283 [ + - + - ]: 12 : throw reportable_exception("invalid artifacttype");
2284 : : }
2285 : :
2286 [ + + ]: 1424 : if (conn != 0)
2287 [ + - + - : 3394 : inc_metric("http_requests_total", "type", artifacttype);
+ - - + -
- - + ]
2288 : :
2289 : 1424 : string section;
2290 [ + + ]: 1424 : if (atype_code == "I")
2291 : : {
2292 [ - + ]: 8 : if (suffix.size () < 2)
2293 [ # # # # ]: 0 : throw reportable_exception ("invalid section suffix");
2294 : :
2295 : : // Remove leading '/'
2296 [ + - - + ]: 8 : section = suffix.substr(1);
2297 : : }
2298 : :
2299 [ + + - + ]: 1480 : if (atype_code == "S" && suffix == "")
2300 [ # # # # ]: 0 : throw reportable_exception("invalid source suffix");
2301 : :
2302 : : // validate buildid
2303 [ + + ]: 1424 : if ((buildid.size() < 2) || // not empty
2304 [ + + + - : 2842 : (buildid.size() % 2) || // even number
+ - ]
2305 : 1418 : (buildid.find_first_not_of("0123456789abcdef") != string::npos)) // pure tasty lowercase hex
2306 [ + - - + ]: 12 : throw reportable_exception("invalid buildid");
2307 : :
2308 [ + - ]: 1418 : if (verbose > 1)
2309 [ + - + - ]: 4254 : obatched(clog) << "searching for buildid=" << buildid << " artifacttype=" << artifacttype
2310 [ + - + - : 1418 : << " suffix=" << suffix << endl;
+ - + - +
- ]
2311 : :
2312 : : // If invoked from the scanner threads, use the scanners' read-write
2313 : : // connection. Otherwise use the web query threads' read-only connection.
2314 [ + + ]: 1418 : sqlite3 *thisdb = (conn == 0) ? db : dbq;
2315 : :
2316 : 1418 : sqlite_ps *pp = 0;
2317 : :
2318 [ + + ]: 1418 : if (atype_code == "D")
2319 : : {
2320 [ - + ]: 738 : pp = new sqlite_ps (thisdb, "mhd-query-d",
2321 : : "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_d where buildid = ? "
2322 [ + - + - : 1476 : "order by mtime desc");
+ - + - +
- - - ]
2323 [ + - ]: 738 : pp->reset();
2324 [ + - ]: 738 : pp->bind(1, buildid);
2325 : : }
2326 [ + + ]: 680 : else if (atype_code == "E")
2327 : : {
2328 [ - + ]: 616 : pp = new sqlite_ps (thisdb, "mhd-query-e",
2329 : : "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_e where buildid = ? "
2330 [ + - + - : 1232 : "order by mtime desc");
+ - + - +
- - - ]
2331 [ + - ]: 616 : pp->reset();
2332 [ + - ]: 616 : pp->bind(1, buildid);
2333 : : }
2334 [ + + ]: 64 : else if (atype_code == "S")
2335 : : {
2336 : : // PR25548
2337 : : // Incoming source queries may come in with either dwarf-level OR canonicalized paths.
2338 : : // We let the query pass with either one.
2339 : :
2340 [ - + ]: 56 : pp = new sqlite_ps (thisdb, "mhd-query-s",
2341 : : "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_s where buildid = ? and artifactsrc in (?,?) "
2342 [ + - + - : 112 : "order by sharedprefix(source0,source0ref) desc, mtime desc");
+ - + - +
- - - ]
2343 [ + - ]: 56 : pp->reset();
2344 [ + - ]: 56 : pp->bind(1, buildid);
2345 : : // NB: we don't store the non-canonicalized path names any more, but old databases
2346 : : // might have them (and no canon ones), so we keep searching for both.
2347 [ + - ]: 56 : pp->bind(2, suffix);
2348 [ + - + - : 734 : pp->bind(3, canon_pathname(suffix));
- + ]
2349 : : }
2350 [ + - ]: 8 : else if (atype_code == "I")
2351 : : {
2352 [ - + ]: 8 : pp = new sqlite_ps (thisdb, "mhd-query-i",
2353 : : "select mtime, sourcetype, source0, source1, 1 as debug_p from " BUILDIDS "_query_d where buildid = ? "
2354 : : "union all "
2355 : : "select mtime, sourcetype, source0, source1, 0 as debug_p from " BUILDIDS "_query_e where buildid = ? "
2356 [ + - + - : 16 : "order by debug_p desc, mtime desc");
+ - + - +
- - - ]
2357 [ + - ]: 8 : pp->reset();
2358 [ + - ]: 8 : pp->bind(1, buildid);
2359 [ + - ]: 8 : pp->bind(2, buildid);
2360 : : }
2361 : 1418 : unique_ptr<sqlite_ps> ps_closer(pp); // release pp if exception or return
2362 : :
2363 : 1418 : bool do_upstream_section_query = true;
2364 : :
2365 : : // consume all the rows
2366 : 1480 : while (1)
2367 : : {
2368 [ + - ]: 1480 : int rc = pp->step();
2369 [ + + ]: 1480 : if (rc == SQLITE_DONE) break;
2370 [ - + ]: 860 : if (rc != SQLITE_ROW)
2371 [ # # # # ]: 0 : throw sqlite_exception(rc, "step");
2372 : :
2373 [ + - ]: 860 : int64_t b_mtime = sqlite3_column_int64 (*pp, 0);
2374 [ + - - + : 860 : string b_stype = string((const char*) sqlite3_column_text (*pp, 1) ?: ""); /* by DDL may not be NULL */
+ - ]
2375 [ + - - + : 860 : string b_source0 = string((const char*) sqlite3_column_text (*pp, 2) ?: ""); /* may be NULL */
+ - - - ]
2376 [ + - + + : 934 : string b_source1 = string((const char*) sqlite3_column_text (*pp, 3) ?: ""); /* may be NULL */
+ - - - ]
2377 : :
2378 [ + - ]: 860 : if (verbose > 1)
2379 [ + - + - : 2580 : obatched(clog) << "found mtime=" << b_mtime << " stype=" << b_stype
- - ]
2380 [ + - + - : 860 : << " source0=" << b_source0 << " source1=" << b_source1 << endl;
+ - + - +
- + - +
- ]
2381 : :
2382 : : // Try accessing the located match.
2383 : : // XXX: in case of multiple matches, attempt them in parallel?
2384 [ + - ]: 860 : auto r = handle_buildid_match (conn ? false : true,
2385 : : b_mtime, b_stype, b_source0, b_source1,
2386 : : section, result_fd);
2387 [ + + ]: 860 : if (r)
2388 [ + + ]: 798 : return r;
2389 : :
2390 : : // If a debuginfo file matching BUILDID was found but didn't contain
2391 : : // the desired section, then the section should not exist. Don't
2392 : : // bother querying upstream servers.
2393 [ + + + - : 62 : if (!section.empty () && (sqlite3_column_int (*pp, 4) == 1))
- + ]
2394 : : {
2395 : 4 : struct stat st;
2396 : :
2397 : : // For "F" sourcetype, check if the debuginfo exists. For "R"
2398 : : // sourcetype, check if the debuginfo was interned into the fdcache.
2399 [ - + ]: 6 : if ((b_stype == "F" && (stat (b_source0.c_str (), &st) == 0))
2400 [ + + + - : 6 : || (b_stype == "R" && fdcache.probe (b_source0, b_source1)))
+ - - + ]
2401 : : do_upstream_section_query = false;
2402 : : }
2403 [ + - - + : 1720 : }
+ - - + ]
2404 [ + - ]: 620 : pp->reset();
2405 : :
2406 [ - + ]: 620 : if (!do_upstream_section_query)
2407 [ # # # # ]: 0 : throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found");
2408 : :
2409 : : // We couldn't find it in the database. Last ditch effort
2410 : : // is to defer to other debuginfo servers.
2411 : :
2412 : 620 : int fd = -1;
2413 [ + - ]: 620 : debuginfod_client *client = debuginfod_pool_begin ();
2414 [ - + ]: 620 : if (client == NULL)
2415 [ # # # # ]: 0 : throw libc_exception(errno, "debuginfod client pool alloc");
2416 : 620 : defer_dtor<debuginfod_client*,void> client_closer (client, debuginfod_pool_end);
2417 : :
2418 [ + - ]: 620 : debuginfod_set_progressfn (client, & debuginfod_find_progress);
2419 : :
2420 [ + - ]: 620 : if (conn)
2421 : : {
2422 : : // Transcribe incoming User-Agent:
2423 [ + - - + : 620 : string ua = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
+ - ]
2424 [ + - + - : 624 : string ua_complete = string("User-Agent: ") + ua;
+ - ]
2425 [ + - ]: 620 : debuginfod_add_http_header (client, ua_complete.c_str());
2426 : :
2427 : : // Compute larger XFF:, for avoiding info loss during
2428 : : // federation, and for future cyclicity detection.
2429 [ + - + + : 1226 : string xff = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
+ - + - ]
2430 [ + + ]: 620 : if (xff != "")
2431 [ + - - + ]: 36 : xff += string(", "); // comma separated list
2432 : :
2433 : 620 : unsigned int xff_count = 0;
2434 [ + + ]: 860 : for (auto&& i : xff){
2435 [ + + ]: 240 : if (i == ',') xff_count++;
2436 : : }
2437 : :
2438 : : // if X-Forwarded-For: exceeds N hops,
2439 : : // do not delegate a local lookup miss to upstream debuginfods.
2440 [ + + ]: 620 : if (xff_count >= forwarded_ttl_limit)
2441 [ + - + - ]: 8 : throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found, --forwared-ttl-limit reached \
2442 : 8 : and will not query the upstream servers");
2443 : :
2444 : : // Compute the client's numeric IP address only - so can't merge with conninfo()
2445 [ + - ]: 616 : const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
2446 : : MHD_CONNECTION_INFO_CLIENT_ADDRESS);
2447 [ + - ]: 616 : struct sockaddr *so = u ? u->client_addr : 0;
2448 : 616 : char hostname[256] = ""; // RFC1035
2449 [ + - - + ]: 616 : if (so && so->sa_family == AF_INET) {
2450 [ # # ]: 0 : (void) getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), NULL, 0,
2451 : : NI_NUMERICHOST);
2452 [ + - ]: 616 : } else if (so && so->sa_family == AF_INET6) {
2453 : 616 : struct sockaddr_in6* addr6 = (struct sockaddr_in6*) so;
2454 [ + - + - : 616 : if (IN6_IS_ADDR_V4MAPPED(&addr6->sin6_addr)) {
- + ]
2455 : 616 : struct sockaddr_in addr4;
2456 [ + - ]: 616 : memset (&addr4, 0, sizeof(addr4));
2457 : 616 : addr4.sin_family = AF_INET;
2458 : 616 : addr4.sin_port = addr6->sin6_port;
2459 [ + - ]: 616 : memcpy (&addr4.sin_addr.s_addr, addr6->sin6_addr.s6_addr+12, sizeof(addr4.sin_addr.s_addr));
2460 [ + - ]: 616 : (void) getnameinfo ((struct sockaddr*) &addr4, sizeof (addr4),
2461 : : hostname, sizeof (hostname), NULL, 0,
2462 : : NI_NUMERICHOST);
2463 : : } else {
2464 [ # # ]: 0 : (void) getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname), NULL, 0,
2465 : : NI_NUMERICHOST);
2466 : : }
2467 : : }
2468 : :
2469 [ + - + - : 1236 : string xff_complete = string("X-Forwarded-For: ")+xff+string(hostname);
+ - + - -
+ - + - -
- + ]
2470 [ + - ]: 616 : debuginfod_add_http_header (client, xff_complete.c_str());
2471 [ + + + - : 1300 : }
+ + ]
2472 : :
2473 [ + + ]: 616 : if (artifacttype == "debuginfo")
2474 [ + - ]: 68 : fd = debuginfod_find_debuginfo (client,
2475 [ + - ]: 68 : (const unsigned char*) buildid.c_str(),
2476 : : 0, NULL);
2477 [ + + ]: 548 : else if (artifacttype == "executable")
2478 [ + - ]: 546 : fd = debuginfod_find_executable (client,
2479 [ + - ]: 546 : (const unsigned char*) buildid.c_str(),
2480 : : 0, NULL);
2481 [ + - ]: 2 : else if (artifacttype == "source")
2482 [ + - ]: 2 : fd = debuginfod_find_source (client,
2483 [ + - ]: 2 : (const unsigned char*) buildid.c_str(),
2484 : : 0, suffix.c_str(), NULL);
2485 [ # # ]: 0 : else if (artifacttype == "section")
2486 [ # # ]: 0 : fd = debuginfod_find_section (client,
2487 [ # # ]: 0 : (const unsigned char*) buildid.c_str(),
2488 : : 0, section.c_str(), NULL);
2489 : :
2490 [ + + ]: 616 : if (fd >= 0)
2491 : : {
2492 [ + - ]: 4 : if (conn != 0)
2493 [ + - + - : 624 : inc_metric ("http_responses_total","result","upstream");
+ - + - -
+ - + - -
- - ]
2494 : 4 : struct stat s;
2495 : 4 : int rc = fstat (fd, &s);
2496 [ + - ]: 4 : if (rc == 0)
2497 : : {
2498 [ + - ]: 4 : auto r = MHD_create_response_from_fd ((uint64_t) s.st_size, fd);
2499 [ + - ]: 4 : if (r)
2500 : : {
2501 [ + - ]: 4 : add_mhd_response_header (r, "Content-Type",
2502 : : "application/octet-stream");
2503 : : // Copy the incoming headers
2504 [ + - ]: 4 : const char * hdrs = debuginfod_get_headers(client);
2505 [ + - ]: 4 : string header_dup;
2506 [ + - ]: 4 : if (hdrs)
2507 [ + - - + ]: 4 : header_dup = string(hdrs);
2508 : : // Parse the "header: value\n" lines into (h,v) tuples and pass on
2509 : 20 : while(1)
2510 : : {
2511 : 12 : size_t newline = header_dup.find('\n');
2512 [ + + ]: 12 : if (newline == string::npos) break;
2513 : 8 : size_t colon = header_dup.find(':');
2514 [ + - ]: 8 : if (colon == string::npos) break;
2515 [ + - ]: 8 : string header = header_dup.substr(0,colon);
2516 [ + - ]: 8 : string value = header_dup.substr(colon+1,newline-colon-1);
2517 : : // strip leading spaces from value
2518 : 8 : size_t nonspace = value.find_first_not_of(" ");
2519 [ + - ]: 8 : if (nonspace != string::npos)
2520 [ + - + + ]: 12 : value = value.substr(nonspace);
2521 [ + - ]: 8 : add_mhd_response_header(r, header.c_str(), value.c_str());
2522 [ + - + + : 12 : header_dup = header_dup.substr(newline+1);
+ + - - ]
2523 [ + - ]: 16 : }
2524 : :
2525 [ + - ]: 4 : add_mhd_last_modified (r, s.st_mtime);
2526 [ + - ]: 4 : if (verbose > 1)
2527 [ + - + - : 8 : obatched(clog) << "serving file from upstream debuginfod/cache" << endl;
- - ]
2528 [ + - ]: 4 : if (result_fd)
2529 : 4 : *result_fd = fd;
2530 [ + - ]: 4 : return r; // NB: don't close fd; libmicrohttpd will
2531 : 4 : }
2532 : : }
2533 [ # # ]: 0 : close (fd);
2534 : : }
2535 : : else
2536 [ + + ]: 612 : switch(fd)
2537 : : {
2538 : : case -ENOSYS:
2539 : : break;
2540 : : case -ENOENT:
2541 : : break;
2542 : 532 : default: // some more tricky error
2543 [ + - + - ]: 1064 : throw libc_exception(-fd, "upstream debuginfod query failed");
2544 : : }
2545 : :
2546 [ + - - + ]: 160 : throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found");
2547 [ - + - + ]: 2040 : }
2548 : :
2549 : :
2550 : : ////////////////////////////////////////////////////////////////////////
2551 : :
2552 : : static map<string,double> metrics; // arbitrary data for /metrics query
2553 : : // NB: store int64_t since all our metrics are integers; prometheus accepts double
2554 : : static mutex metrics_lock;
2555 : : // NB: these objects get released during the process exit via global dtors
2556 : : // do not call them from within other global dtors
2557 : :
2558 : : // utility function for assembling prometheus-compatible
2559 : : // name="escaped-value" strings
2560 : : // https://prometheus.io/docs/instrumenting/exposition_formats/
2561 : : static string
2562 : 256413 : metric_label(const string& name, const string& value)
2563 : : {
2564 : 256413 : string x = name + "=\"";
2565 [ + + ]: 4121826 : for (auto&& c : value)
2566 [ - - - + ]: 3865426 : switch(c)
2567 : : {
2568 [ # # ]: 0 : case '\\': x += "\\\\"; break;
2569 [ # # ]: 0 : case '\"': x += "\\\""; break;
2570 [ # # ]: 0 : case '\n': x += "\\n"; break;
2571 [ + - ]: 7730844 : default: x += c; break;
2572 : : }
2573 [ + - ]: 256400 : x += "\"";
2574 : 256446 : return x;
2575 : 0 : }
2576 : :
2577 : :
2578 : : // add prometheus-format metric name + label tuple (if any) + value
2579 : :
2580 : : static void
2581 : 8808 : set_metric(const string& metric, double value)
2582 : : {
2583 : 8808 : unique_lock<mutex> lock(metrics_lock);
2584 [ + - ]: 8808 : metrics[metric] = value;
2585 : 8808 : }
2586 : : #if 0 /* unused */
2587 : : static void
2588 : : inc_metric(const string& metric)
2589 : : {
2590 : : unique_lock<mutex> lock(metrics_lock);
2591 : : metrics[metric] ++;
2592 : : }
2593 : : #endif
2594 : : static void
2595 : 7123 : set_metric(const string& metric,
2596 : : const string& lname, const string& lvalue,
2597 : : double value)
2598 : : {
2599 [ + - + - : 14247 : string key = (metric + "{" + metric_label(lname, lvalue) + "}");
- + - + +
+ - - ]
2600 [ + - ]: 7123 : unique_lock<mutex> lock(metrics_lock);
2601 [ + - ]: 7125 : metrics[key] = value;
2602 [ + - ]: 14250 : }
2603 : :
2604 : : static void
2605 : 114157 : inc_metric(const string& metric,
2606 : : const string& lname, const string& lvalue)
2607 : : {
2608 [ + - + - : 245732 : string key = (metric + "{" + metric_label(lname, lvalue) + "}");
- + + + +
+ - - ]
2609 [ + - ]: 114156 : unique_lock<mutex> lock(metrics_lock);
2610 [ + - ]: 114160 : metrics[key] ++;
2611 [ + - ]: 228316 : }
2612 : : static void
2613 : 110221 : add_metric(const string& metric,
2614 : : const string& lname, const string& lvalue,
2615 : : double value)
2616 : : {
2617 [ + - + - : 237878 : string key = (metric + "{" + metric_label(lname, lvalue) + "}");
- + + + +
+ - - ]
2618 [ + - ]: 110239 : unique_lock<mutex> lock(metrics_lock);
2619 [ + - ]: 110248 : metrics[key] += value;
2620 [ + - ]: 220496 : }
2621 : : #if 0
2622 : : static void
2623 : : add_metric(const string& metric,
2624 : : double value)
2625 : : {
2626 : : unique_lock<mutex> lock(metrics_lock);
2627 : : metrics[metric] += value;
2628 : : }
2629 : : #endif
2630 : :
2631 : :
2632 : : // and more for higher arity labels if needed
2633 : :
2634 : : static void
2635 : 6258 : inc_metric(const string& metric,
2636 : : const string& lname, const string& lvalue,
2637 : : const string& rname, const string& rvalue)
2638 : : {
2639 [ + - - + : 12516 : string key = (metric + "{"
- - ]
2640 [ + - + - : 25032 : + metric_label(lname, lvalue) + ","
- + - + +
+ - - ]
2641 [ + - - + : 18774 : + metric_label(rname, rvalue) + "}");
- + ]
2642 [ + - ]: 6258 : unique_lock<mutex> lock(metrics_lock);
2643 [ + - ]: 6258 : metrics[key] ++;
2644 [ + - ]: 12516 : }
2645 : : static void
2646 : 6258 : add_metric(const string& metric,
2647 : : const string& lname, const string& lvalue,
2648 : : const string& rname, const string& rvalue,
2649 : : double value)
2650 : : {
2651 [ + - - + : 12516 : string key = (metric + "{"
- - ]
2652 [ + - + - : 25032 : + metric_label(lname, lvalue) + ","
- + - + +
+ - - ]
2653 [ + - - + : 18774 : + metric_label(rname, rvalue) + "}");
- + ]
2654 [ + - ]: 6258 : unique_lock<mutex> lock(metrics_lock);
2655 [ + - ]: 6258 : metrics[key] += value;
2656 [ + - ]: 12516 : }
2657 : :
2658 : : static struct MHD_Response*
2659 : 692 : handle_metrics (off_t* size)
2660 : : {
2661 : 692 : stringstream o;
2662 : 692 : {
2663 [ + - ]: 692 : unique_lock<mutex> lock(metrics_lock);
2664 [ + + ]: 64731 : for (auto&& i : metrics)
2665 [ + - ]: 64039 : o << i.first
2666 : : << " "
2667 [ + - + - ]: 64039 : << std::setprecision(std::numeric_limits<double>::digits10 + 1)
2668 [ + - + - ]: 64039 : << i.second
2669 : 64039 : << endl;
2670 : 0 : }
2671 [ + - ]: 692 : const string& os = o.str();
2672 [ + - ]: 692 : MHD_Response* r = MHD_create_response_from_buffer (os.size(),
2673 [ + - ]: 692 : (void*) os.c_str(),
2674 : : MHD_RESPMEM_MUST_COPY);
2675 [ + - ]: 692 : if (r != NULL)
2676 : : {
2677 [ + - ]: 692 : *size = os.size();
2678 [ + - ]: 692 : add_mhd_response_header (r, "Content-Type", "text/plain");
2679 : : }
2680 [ + - ]: 1384 : return r;
2681 : 692 : }
2682 : :
2683 : : static struct MHD_Response*
2684 : 0 : handle_root (off_t* size)
2685 : : {
2686 [ # # # # : 0 : static string version = "debuginfod (" + string (PACKAGE_NAME) + ") "
# # # # #
# # # ]
2687 [ # # # # : 0 : + string (PACKAGE_VERSION);
# # # # #
# ]
2688 : 0 : MHD_Response* r = MHD_create_response_from_buffer (version.size (),
2689 : 0 : (void *) version.c_str (),
2690 : : MHD_RESPMEM_PERSISTENT);
2691 [ # # ]: 0 : if (r != NULL)
2692 : : {
2693 : 0 : *size = version.size ();
2694 : 0 : add_mhd_response_header (r, "Content-Type", "text/plain");
2695 : : }
2696 : 0 : return r;
2697 : : }
2698 : :
2699 : :
2700 : : ////////////////////////////////////////////////////////////////////////
2701 : :
2702 : :
2703 : : /* libmicrohttpd callback */
2704 : : static MHD_RESULT
2705 : 4172 : handler_cb (void * /*cls*/,
2706 : : struct MHD_Connection *connection,
2707 : : const char *url,
2708 : : const char *method,
2709 : : const char * /*version*/,
2710 : : const char * /*upload_data*/,
2711 : : size_t * /*upload_data_size*/,
2712 : : void ** ptr)
2713 : : {
2714 : 4172 : struct MHD_Response *r = NULL;
2715 : 4172 : string url_copy = url;
2716 : :
2717 : : /* libmicrohttpd always makes (at least) two callbacks: once just
2718 : : past the headers, and one after the request body is finished
2719 : : being received. If we process things early (first callback) and
2720 : : queue a response, libmicrohttpd would suppress http keep-alive
2721 : : (via connection->read_closed = true). */
2722 : 4171 : static int aptr; /* just some random object to use as a flag */
2723 [ + + ]: 4171 : if (&aptr != *ptr)
2724 : : {
2725 : : /* do never respond on first call */
2726 : 2085 : *ptr = &aptr;
2727 : 2085 : return MHD_YES;
2728 : : }
2729 : 2086 : *ptr = NULL; /* reset when done */
2730 : :
2731 [ + - ]: 2086 : const char *maxsize_string = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "X-DEBUGINFOD-MAXSIZE");
2732 : 2086 : long maxsize = 0;
2733 [ + + + - ]: 2086 : if (maxsize_string != NULL && maxsize_string[0] != '\0')
2734 : 2 : maxsize = atol(maxsize_string);
2735 : : else
2736 : : maxsize = 0;
2737 : :
2738 : : #if MHD_VERSION >= 0x00097002
2739 : 2086 : enum MHD_Result rc;
2740 : : #else
2741 : : int rc = MHD_NO; // mhd
2742 : : #endif
2743 : 2086 : int http_code = 500;
2744 : 2086 : off_t http_size = -1;
2745 : 2086 : struct timespec ts_start, ts_end;
2746 : 2086 : clock_gettime (CLOCK_MONOTONIC, &ts_start);
2747 : 2085 : double afteryou = 0.0;
2748 [ + - ]: 2085 : string artifacttype, suffix;
2749 : :
2750 : 2085 : try
2751 : : {
2752 [ + - - + : 4802 : if (string(method) != "GET")
- + ]
2753 [ # # # # ]: 0 : throw reportable_exception(400, "we support GET only");
2754 : :
2755 : : /* Start decoding the URL. */
2756 : 2083 : size_t slash1 = url_copy.find('/', 1);
2757 [ + - ]: 2084 : string url1 = url_copy.substr(0, slash1); // ok even if slash1 not found
2758 : :
2759 [ + + + + ]: 3472 : if (slash1 != string::npos && url1 == "/buildid")
2760 : : {
2761 : : // PR27863: block this thread awhile if another thread is already busy
2762 : : // fetching the exact same thing. This is better for Everyone.
2763 : : // The latecomer says "... after you!" and waits.
2764 [ + - + - : 3399 : add_metric ("thread_busy", "role", "http-buildid-after-you", 1);
+ - + - -
+ + - - -
- - ]
2765 : : #ifdef HAVE_PTHREAD_SETNAME_NP
2766 : 1388 : (void) pthread_setname_np (pthread_self(), "mhd-buildid-after-you");
2767 : : #endif
2768 : 1388 : struct timespec tsay_start, tsay_end;
2769 : 1388 : clock_gettime (CLOCK_MONOTONIC, &tsay_start);
2770 [ + + + - ]: 1388 : static unique_set<string> busy_urls;
2771 [ + - ]: 1388 : unique_set_reserver<string> after_you(busy_urls, url_copy);
2772 : 1388 : clock_gettime (CLOCK_MONOTONIC, &tsay_end);
2773 : 1388 : afteryou = (tsay_end.tv_sec - tsay_start.tv_sec) + (tsay_end.tv_nsec - tsay_start.tv_nsec)/1.e9;
2774 [ + - + - : 2776 : add_metric ("thread_busy", "role", "http-buildid-after-you", -1);
+ - + - -
+ + - - -
- - ]
2775 : :
2776 [ + - + - : 2776 : tmp_inc_metric m ("thread_busy", "role", "http-buildid");
+ - + - -
+ - + - -
- - ]
2777 : : #ifdef HAVE_PTHREAD_SETNAME_NP
2778 : 1388 : (void) pthread_setname_np (pthread_self(), "mhd-buildid");
2779 : : #endif
2780 : 1388 : size_t slash2 = url_copy.find('/', slash1+1);
2781 [ - + ]: 1388 : if (slash2 == string::npos)
2782 [ # # # # ]: 0 : throw reportable_exception("/buildid/ webapi error, need buildid");
2783 : :
2784 [ + - ]: 1388 : string buildid = url_copy.substr(slash1+1, slash2-slash1-1);
2785 : :
2786 : 1388 : size_t slash3 = url_copy.find('/', slash2+1);
2787 : :
2788 [ + + ]: 1388 : if (slash3 == string::npos)
2789 : : {
2790 [ + - - + ]: 1324 : artifacttype = url_copy.substr(slash2+1);
2791 [ + - ]: 1324 : suffix = "";
2792 : : }
2793 : : else
2794 : : {
2795 [ + - - + ]: 64 : artifacttype = url_copy.substr(slash2+1, slash3-slash2-1);
2796 [ + - - + : 690 : suffix = url_copy.substr(slash3); // include the slash in the suffix
+ + ]
2797 : : }
2798 : :
2799 : : // get the resulting fd so we can report its size
2800 : 1388 : int fd;
2801 [ + + ]: 1388 : r = handle_buildid(connection, buildid, artifacttype, suffix, &fd);
2802 [ + - ]: 762 : if (r)
2803 : : {
2804 : 762 : struct stat fs;
2805 [ + - ]: 762 : if (fstat(fd, &fs) == 0)
2806 : 762 : http_size = fs.st_size;
2807 : : // libmicrohttpd will close (fd);
2808 : : }
2809 : 2014 : }
2810 [ + + ]: 700 : else if (url1 == "/metrics")
2811 : : {
2812 [ + - + - : 1384 : tmp_inc_metric m ("thread_busy", "role", "http-metrics");
+ - + - -
+ - + - -
- - ]
2813 [ + - ]: 692 : artifacttype = "metrics";
2814 [ + - + - : 1384 : inc_metric("http_requests_total", "type", artifacttype);
+ - - + -
- ]
2815 [ + - ]: 692 : r = handle_metrics(& http_size);
2816 : 692 : }
2817 [ - + ]: 6 : else if (url1 == "/")
2818 : : {
2819 [ # # ]: 0 : artifacttype = "/";
2820 [ - - - - : 634 : inc_metric("http_requests_total", "type", artifacttype);
- - - - -
- - + ]
2821 [ # # ]: 0 : r = handle_root(& http_size);
2822 : : }
2823 : : else
2824 [ + - + - : 18 : throw reportable_exception("webapi error, unrecognized '" + url1 + "'");
+ - - + ]
2825 : :
2826 [ - + ]: 1454 : if (r == 0)
2827 [ # # # # ]: 0 : throw reportable_exception("internal error, missing response");
2828 : :
2829 [ + + + - ]: 1454 : if (maxsize > 0 && http_size > maxsize)
2830 : : {
2831 [ + - ]: 2 : MHD_destroy_response(r);
2832 [ + - + - : 6 : throw reportable_exception(406, "File too large, max size=" + std::to_string(maxsize));
+ - - + ]
2833 : : }
2834 : :
2835 [ + - ]: 1452 : rc = MHD_queue_response (connection, MHD_HTTP_OK, r);
2836 : 1452 : http_code = MHD_HTTP_OK;
2837 [ + - ]: 1452 : MHD_destroy_response (r);
2838 : 2086 : }
2839 [ - + ]: 634 : catch (const reportable_exception& e)
2840 : : {
2841 [ + - + - : 1268 : inc_metric("http_responses_total","result","error");
+ - + - -
+ - + - -
- - ]
2842 [ + - ]: 634 : e.report(clog);
2843 : 634 : http_code = e.code;
2844 [ + - ]: 634 : http_size = e.message.size();
2845 [ + - ]: 634 : rc = e.mhd_send_response (connection);
2846 : 634 : }
2847 : :
2848 : 2086 : clock_gettime (CLOCK_MONOTONIC, &ts_end);
2849 : 2086 : double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
2850 : : // afteryou: delay waiting for other client's identical query to complete
2851 : : // deltas: total latency, including afteryou waiting
2852 [ + - + - : 4172 : obatched(clog) << conninfo(connection)
- - ]
2853 : : << ' ' << method << ' ' << url
2854 [ + - + - : 2086 : << ' ' << http_code << ' ' << http_size
+ - + - +
- + - +
- ]
2855 [ + - + - : 2086 : << ' ' << (int)(afteryou*1000) << '+' << (int)((deltas-afteryou)*1000) << "ms"
+ - + - +
- + - +
- ]
2856 [ + - ]: 2086 : << endl;
2857 : :
2858 : : // related prometheus metrics
2859 : 2086 : string http_code_str = to_string(http_code);
2860 [ + - + - : 4172 : add_metric("http_responses_transfer_bytes_sum",
+ - + - -
+ - + - -
- - ]
2861 : : "code", http_code_str, "type", artifacttype, http_size);
2862 [ + - + - : 4172 : inc_metric("http_responses_transfer_bytes_count",
+ - + - -
+ - + - -
- - ]
2863 : : "code", http_code_str, "type", artifacttype);
2864 : :
2865 [ + - + - : 4172 : add_metric("http_responses_duration_milliseconds_sum",
+ - + - -
+ - + - -
- - ]
2866 : : "code", http_code_str, "type", artifacttype, deltas*1000); // prometheus prefers _seconds and floating point
2867 [ + - + - : 4172 : inc_metric("http_responses_duration_milliseconds_count",
+ - + - -
+ - + - -
- - ]
2868 : : "code", http_code_str, "type", artifacttype);
2869 : :
2870 [ + - + - : 4172 : add_metric("http_responses_after_you_milliseconds_sum",
+ - + - -
+ - + - -
- - ]
2871 : : "code", http_code_str, "type", artifacttype, afteryou*1000);
2872 [ + - + - : 4172 : inc_metric("http_responses_after_you_milliseconds_count",
+ - + - -
+ - + - -
- - - - ]
2873 : : "code", http_code_str, "type", artifacttype);
2874 : :
2875 [ - + ]: 2086 : return rc;
2876 [ + + - + : 9088 : }
+ + ]
2877 : :
2878 : :
2879 : : ////////////////////////////////////////////////////////////////////////
2880 : : // borrowed originally from src/nm.c get_local_names()
2881 : :
2882 : : static void
2883 : 210 : dwarf_extract_source_paths (Elf *elf, set<string>& debug_sourcefiles)
2884 : : noexcept // no exceptions - so we can simplify the altdbg resource release at end
2885 : : {
2886 : 210 : Dwarf* dbg = dwarf_begin_elf (elf, DWARF_C_READ, NULL);
2887 [ - + ]: 210 : if (dbg == NULL)
2888 : 0 : return;
2889 : :
2890 : 210 : Dwarf* altdbg = NULL;
2891 : 210 : int altdbg_fd = -1;
2892 : :
2893 : : // DWZ handling: if we have an unsatisfied debug-alt-link, add an
2894 : : // empty string into the outgoing sourcefiles set, so the caller
2895 : : // should know that our data is incomplete.
2896 : 210 : const char *alt_name_p;
2897 : 210 : const void *alt_build_id; // elfutils-owned memory
2898 : 210 : ssize_t sz = dwelf_dwarf_gnu_debugaltlink (dbg, &alt_name_p, &alt_build_id);
2899 [ + + ]: 210 : if (sz > 0) // got one!
2900 : : {
2901 : 40 : string buildid;
2902 : 40 : unsigned char* build_id_bytes = (unsigned char*) alt_build_id;
2903 [ + + ]: 840 : for (ssize_t idx=0; idx<sz; idx++)
2904 : : {
2905 : 800 : buildid += "0123456789abcdef"[build_id_bytes[idx] >> 4];
2906 : 800 : buildid += "0123456789abcdef"[build_id_bytes[idx] & 0xf];
2907 : : }
2908 : :
2909 [ + - ]: 40 : if (verbose > 3)
2910 : 40 : obatched(clog) << "Need altdebug buildid=" << buildid << endl;
2911 : :
2912 : : // but is it unsatisfied the normal elfutils ways?
2913 : 40 : Dwarf* alt = dwarf_getalt (dbg);
2914 [ + - ]: 40 : if (alt == NULL)
2915 : : {
2916 : : // Yup, unsatisfied the normal way. Maybe we can satisfy it
2917 : : // from our own debuginfod database.
2918 : 40 : int alt_fd;
2919 : 40 : struct MHD_Response *r = 0;
2920 : 40 : try
2921 : : {
2922 [ + - ]: 40 : string artifacttype = "debuginfo";
2923 [ + - + - : 40 : r = handle_buildid (0, buildid, artifacttype, "", &alt_fd);
- + - + -
- ]
2924 : 0 : }
2925 [ - - ]: 0 : catch (const reportable_exception& e)
2926 : : {
2927 : : // swallow exceptions
2928 : 0 : }
2929 : :
2930 : : // NB: this is not actually recursive! This invokes the web-query
2931 : : // path, which cannot get back into the scan code paths.
2932 [ + - ]: 40 : if (r)
2933 : : {
2934 : : // Found it!
2935 : 40 : altdbg_fd = dup(alt_fd); // ok if this fails, downstream failures ok
2936 : 40 : alt = altdbg = dwarf_begin (altdbg_fd, DWARF_C_READ);
2937 : : // NB: must close this dwarf and this fd at the bottom of the function!
2938 : 40 : MHD_destroy_response (r); // will close alt_fd
2939 [ + - ]: 40 : if (alt)
2940 : 40 : dwarf_setalt (dbg, alt);
2941 : : }
2942 : : }
2943 : : else
2944 : : {
2945 : : // NB: dwarf_setalt(alt) inappropriate - already done!
2946 : : // NB: altdbg will stay 0 so nothing tries to redundantly dealloc.
2947 : : }
2948 : :
2949 [ + - ]: 40 : if (alt)
2950 : : {
2951 [ + - ]: 40 : if (verbose > 3)
2952 : 40 : obatched(clog) << "Resolved altdebug buildid=" << buildid << endl;
2953 : : }
2954 : : else // (alt == NULL) - signal possible presence of poor debuginfo
2955 : : {
2956 [ # # ]: 0 : debug_sourcefiles.insert("");
2957 [ # # ]: 0 : if (verbose > 3)
2958 : 0 : obatched(clog) << "Unresolved altdebug buildid=" << buildid << endl;
2959 : : }
2960 : 40 : }
2961 : :
2962 : 210 : Dwarf_Off offset = 0;
2963 : 210 : Dwarf_Off old_offset;
2964 : 210 : size_t hsize;
2965 : :
2966 [ + + ]: 2310 : while (dwarf_nextcu (dbg, old_offset = offset, &offset, &hsize, NULL, NULL, NULL) == 0)
2967 : : {
2968 : 2100 : Dwarf_Die cudie_mem;
2969 : 2100 : Dwarf_Die *cudie = dwarf_offdie (dbg, old_offset + hsize, &cudie_mem);
2970 : :
2971 [ - + ]: 2100 : if (cudie == NULL)
2972 : 20 : continue;
2973 [ + + ]: 2100 : if (dwarf_tag (cudie) != DW_TAG_compile_unit)
2974 : 20 : continue;
2975 : :
2976 [ - + ]: 2080 : const char *cuname = dwarf_diename(cudie) ?: "unknown";
2977 : :
2978 : 2080 : Dwarf_Files *files;
2979 : 2080 : size_t nfiles;
2980 [ - + ]: 2080 : if (dwarf_getsrcfiles (cudie, &files, &nfiles) != 0)
2981 : 0 : continue;
2982 : :
2983 : : // extract DW_AT_comp_dir to resolve relative file names
2984 : 2080 : const char *comp_dir = "";
2985 : 2080 : const char *const *dirs;
2986 : 2080 : size_t ndirs;
2987 [ - + ]: 2080 : if (dwarf_getsrcdirs (files, &dirs, &ndirs) == 0 &&
2988 [ - + ]: 2080 : dirs[0] != NULL)
2989 : : comp_dir = dirs[0];
2990 : : if (comp_dir == NULL)
2991 : : comp_dir = "";
2992 : :
2993 [ + + ]: 2080 : if (verbose > 3)
2994 : 328 : obatched(clog) << "searching for sources for cu=" << cuname << " comp_dir=" << comp_dir
2995 : 164 : << " #files=" << nfiles << " #dirs=" << ndirs << endl;
2996 : :
2997 [ + - - - ]: 2080 : if (comp_dir[0] == '\0' && cuname[0] != '/')
2998 : : {
2999 : : // This is a common symptom for dwz-compressed debug files,
3000 : : // where the altdebug file cannot be resolved.
3001 [ # # ]: 0 : if (verbose > 3)
3002 : 0 : obatched(clog) << "skipping cu=" << cuname << " due to empty comp_dir" << endl;
3003 : 0 : continue;
3004 : : }
3005 : :
3006 [ + + ]: 32504 : for (size_t f = 1; f < nfiles; f++)
3007 : : {
3008 : 30424 : const char *hat = dwarf_filesrc (files, f, NULL, NULL);
3009 [ - + ]: 30424 : if (hat == NULL)
3010 : 0 : continue;
3011 : :
3012 [ + + - + ]: 58064 : if (string(hat) == "<built-in>") // gcc intrinsics, don't bother record
3013 : 0 : continue;
3014 : :
3015 [ + + ]: 30424 : string waldo;
3016 [ + + ]: 30424 : if (hat[0] == '/') // absolute
3017 [ - + ]: 19298 : waldo = (string (hat));
3018 [ + - ]: 11126 : else if (comp_dir[0] != '\0') // comp_dir relative
3019 [ - + - + : 19468 : waldo = (string (comp_dir) + string("/") + string (hat));
- + - + +
+ ]
3020 : : else
3021 : : {
3022 [ # # ]: 0 : if (verbose > 3)
3023 : 0 : obatched(clog) << "skipping hat=" << hat << " due to empty comp_dir" << endl;
3024 [ # # ]: 0 : continue;
3025 : : }
3026 : :
3027 : : // NB: this is the 'waldo' that a dbginfo client will have
3028 : : // to supply for us to give them the file The comp_dir
3029 : : // prefixing is a definite complication. Otherwise we'd
3030 : : // have to return a setof comp_dirs (one per CU!) with
3031 : : // corresponding filesrc[] names, instead of one absolute
3032 : : // resoved set. Maybe we'll have to do that anyway. XXX
3033 : :
3034 [ + + ]: 30424 : if (verbose > 4)
3035 [ - + ]: 100 : obatched(clog) << waldo
3036 [ - + ]: 50 : << (debug_sourcefiles.find(waldo)==debug_sourcefiles.end() ? " new" : " dup") << endl;
3037 : :
3038 [ + - ]: 30424 : debug_sourcefiles.insert (waldo);
3039 : 30424 : }
3040 : : }
3041 : :
3042 : 210 : dwarf_end(dbg);
3043 [ + + ]: 210 : if (altdbg)
3044 : 40 : dwarf_end(altdbg);
3045 [ + + ]: 210 : if (altdbg_fd >= 0)
3046 : 40 : close(altdbg_fd);
3047 : : }
3048 : :
3049 : :
3050 : :
3051 : : static void
3052 : 1033 : elf_classify (int fd, bool &executable_p, bool &debuginfo_p, string &buildid, set<string>& debug_sourcefiles)
3053 : : {
3054 : 1033 : Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
3055 [ + - ]: 1034 : if (elf == NULL)
3056 : : return;
3057 : :
3058 : 1034 : try // catch our types of errors and clean up the Elf* object
3059 : : {
3060 [ + - + + ]: 1034 : if (elf_kind (elf) != ELF_K_ELF)
3061 : : {
3062 [ + - ]: 620 : elf_end (elf);
3063 : 620 : return;
3064 : : }
3065 : :
3066 : 414 : GElf_Ehdr ehdr_storage;
3067 [ + - ]: 414 : GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_storage);
3068 [ - + ]: 414 : if (ehdr == NULL)
3069 : : {
3070 [ # # ]: 0 : elf_end (elf);
3071 : : return;
3072 : : }
3073 : 414 : auto elf_type = ehdr->e_type;
3074 : :
3075 : 414 : const void *build_id; // elfutils-owned memory
3076 [ + - ]: 414 : ssize_t sz = dwelf_elf_gnu_build_id (elf, & build_id);
3077 [ - + ]: 411 : if (sz <= 0)
3078 : : {
3079 : : // It's not a diagnostic-worthy error for an elf file to lack build-id.
3080 : : // It might just be very old.
3081 [ # # ]: 0 : elf_end (elf);
3082 : : return;
3083 : : }
3084 : :
3085 : : // build_id is a raw byte array; convert to hexadecimal *lowercase*
3086 : 411 : unsigned char* build_id_bytes = (unsigned char*) build_id;
3087 [ + + ]: 8650 : for (ssize_t idx=0; idx<sz; idx++)
3088 : : {
3089 [ + - ]: 8237 : buildid += "0123456789abcdef"[build_id_bytes[idx] >> 4];
3090 [ + - ]: 16472 : buildid += "0123456789abcdef"[build_id_bytes[idx] & 0xf];
3091 : : }
3092 : :
3093 : : // now decide whether it's an executable - namely, any allocatable section has
3094 : : // PROGBITS;
3095 [ + + ]: 413 : if (elf_type == ET_EXEC || elf_type == ET_DYN)
3096 : : {
3097 : 363 : size_t shnum;
3098 [ + - ]: 363 : int rc = elf_getshdrnum (elf, &shnum);
3099 [ - + ]: 363 : if (rc < 0)
3100 [ # # # # ]: 0 : throw elfutils_exception(rc, "getshdrnum");
3101 : :
3102 : 363 : executable_p = false;
3103 [ + + ]: 6983 : for (size_t sc = 0; sc < shnum; sc++)
3104 : : {
3105 [ + - ]: 6807 : Elf_Scn *scn = elf_getscn (elf, sc);
3106 [ - + ]: 6806 : if (scn == NULL)
3107 : 0 : continue;
3108 : :
3109 : 6806 : GElf_Shdr shdr_mem;
3110 [ + - ]: 6806 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
3111 [ - + ]: 6808 : if (shdr == NULL)
3112 : 0 : continue;
3113 : :
3114 : : // allocated (loadable / vm-addr-assigned) section with available content?
3115 [ + + + + ]: 6808 : if ((shdr->sh_type == SHT_PROGBITS) && (shdr->sh_flags & SHF_ALLOC))
3116 : : {
3117 [ + + ]: 188 : if (verbose > 4)
3118 [ + - + - : 32 : obatched(clog) << "executable due to SHF_ALLOC SHT_PROGBITS sc=" << sc << endl;
+ - ]
3119 : 188 : executable_p = true;
3120 : 188 : break; // no need to keep looking for others
3121 : : }
3122 : : } // iterate over sections
3123 : : } // executable_p classification
3124 : :
3125 : : // now decide whether it's a debuginfo - namely, if it has any .debug* or .zdebug* sections
3126 : : // logic mostly stolen from fweimer@redhat.com's elfclassify drafts
3127 : 414 : size_t shstrndx;
3128 [ + - ]: 414 : int rc = elf_getshdrstrndx (elf, &shstrndx);
3129 [ - + ]: 414 : if (rc < 0)
3130 [ # # # # ]: 0 : throw elfutils_exception(rc, "getshdrstrndx");
3131 : :
3132 : : Elf_Scn *scn = NULL;
3133 : : bool symtab_p = false;
3134 : : bool bits_alloc_p = false;
3135 : 21304 : while (true)
3136 : : {
3137 [ + - ]: 10859 : scn = elf_nextscn (elf, scn);
3138 [ + + ]: 10776 : if (scn == NULL)
3139 : : break;
3140 : 10572 : GElf_Shdr shdr_storage;
3141 [ + - ]: 10572 : GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_storage);
3142 [ - + ]: 10567 : if (shdr == NULL)
3143 : : break;
3144 [ + - ]: 10567 : const char *section_name = elf_strptr (elf, shstrndx, shdr->sh_name);
3145 [ - + ]: 10655 : if (section_name == NULL)
3146 : : break;
3147 [ + + ]: 10655 : if (startswith (section_name, ".debug_line") ||
3148 [ - + ]: 10445 : startswith (section_name, ".zdebug_line"))
3149 : : {
3150 : 210 : debuginfo_p = true;
3151 [ + - ]: 210 : if (scan_source_info)
3152 : 210 : dwarf_extract_source_paths (elf, debug_sourcefiles);
3153 : : break; // expecting only one .*debug_line, so no need to look for others
3154 : : }
3155 [ + + ]: 10445 : else if (startswith (section_name, ".debug_") ||
3156 [ + # ]: 9713 : startswith (section_name, ".zdebug_"))
3157 : : {
3158 : 668 : debuginfo_p = true;
3159 : : // NB: don't break; need to parse .debug_line for sources
3160 : : }
3161 [ + + ]: 9777 : else if (shdr->sh_type == SHT_SYMTAB)
3162 : : {
3163 : : symtab_p = true;
3164 : : }
3165 : 9753 : else if (shdr->sh_type != SHT_NOBITS
3166 [ + + ]: 9753 : && shdr->sh_type != SHT_NOTE
3167 [ + + ]: 4698 : && (shdr->sh_flags & SHF_ALLOC) != 0)
3168 : : {
3169 : 10445 : bits_alloc_p = true;
3170 : : }
3171 : 10445 : }
3172 : :
3173 : : // For more expansive elf/split-debuginfo classification, we
3174 : : // want to identify as debuginfo "strip -s"-produced files
3175 : : // without .debug_info* (like libicudata), but we don't want to
3176 : : // identify "strip -g" executables (with .symtab left there).
3177 [ - + ]: 414 : if (symtab_p && !bits_alloc_p)
3178 : 0 : debuginfo_p = true;
3179 : : }
3180 [ # # ]: 0 : catch (const reportable_exception& e)
3181 : : {
3182 [ # # ]: 0 : e.report(clog);
3183 : 0 : }
3184 : 414 : elf_end (elf);
3185 : : }
3186 : :
3187 : :
3188 : : // Intern the given file name in two parts (dirname & basename) and
3189 : : // return the resulting file's id.
3190 : : static int64_t
3191 : 8643 : register_file_name(sqlite_ps& ps_upsert_fileparts,
3192 : : sqlite_ps& ps_upsert_file,
3193 : : sqlite_ps& ps_lookup_file,
3194 : : const string& name)
3195 : : {
3196 : 8643 : std::size_t slash = name.rfind('/');
3197 [ + + ]: 8643 : string dirname, filename;
3198 [ + + ]: 8643 : if (slash == std::string::npos)
3199 : : {
3200 [ + - ]: 90 : dirname = "";
3201 [ + - ]: 90 : filename = name;
3202 : : }
3203 : : else
3204 : : {
3205 [ + - - + ]: 8553 : dirname = name.substr(0, slash);
3206 [ + - - + : 8553 : filename = name.substr(slash+1);
- - ]
3207 : : }
3208 : :
3209 : : // intern the two substrings
3210 : 8643 : ps_upsert_fileparts
3211 [ + - ]: 8643 : .reset()
3212 [ + - ]: 8644 : .bind(1, dirname)
3213 [ + - ]: 8644 : .step_ok_done();
3214 : 8644 : ps_upsert_fileparts
3215 [ + - ]: 8644 : .reset()
3216 [ + - ]: 8644 : .bind(1, filename)
3217 [ + - ]: 8644 : .step_ok_done();
3218 : :
3219 : : // intern the tuple
3220 : 8644 : ps_upsert_file
3221 [ + - ]: 8644 : .reset()
3222 [ + - ]: 8644 : .bind(1, dirname)
3223 [ + - ]: 8644 : .bind(2, filename)
3224 [ + - ]: 8644 : .step_ok_done();
3225 : :
3226 : : // look up the tuple's id
3227 : 8644 : ps_lookup_file
3228 [ + - ]: 8644 : .reset()
3229 [ + - ]: 8644 : .bind(1, dirname)
3230 [ + - ]: 8643 : .bind(2, filename);
3231 [ + - ]: 8644 : int rc = ps_lookup_file.step();
3232 [ - + - - : 8644 : if (rc != SQLITE_ROW) throw sqlite_exception(rc, "step");
- - ]
3233 : :
3234 [ + - ]: 8644 : int64_t id = sqlite3_column_int64 (ps_lookup_file, 0);
3235 [ + - ]: 8644 : ps_lookup_file.reset();
3236 [ + + ]: 8644 : return id;
3237 [ + + ]: 16560 : }
3238 : :
3239 : :
3240 : :
3241 : : static void
3242 : 728 : scan_source_file (const string& rps, const stat_t& st,
3243 : : sqlite_ps& ps_upsert_buildids,
3244 : : sqlite_ps& ps_upsert_fileparts,
3245 : : sqlite_ps& ps_upsert_file,
3246 : : sqlite_ps& ps_lookup_file,
3247 : : sqlite_ps& ps_upsert_de,
3248 : : sqlite_ps& ps_upsert_s,
3249 : : sqlite_ps& ps_query,
3250 : : sqlite_ps& ps_scan_done,
3251 : : unsigned& fts_cached,
3252 : : unsigned& fts_executable,
3253 : : unsigned& fts_debuginfo,
3254 : : unsigned& fts_sourcefiles)
3255 : : {
3256 : 728 : int64_t fileid = register_file_name(ps_upsert_fileparts, ps_upsert_file, ps_lookup_file, rps);
3257 : :
3258 : : /* See if we know of it already. */
3259 : 728 : int rc = ps_query
3260 : 728 : .reset()
3261 : 728 : .bind(1, fileid)
3262 : 728 : .bind(2, st.st_mtime)
3263 : 728 : .step();
3264 : 728 : ps_query.reset();
3265 [ + + ]: 728 : if (rc == SQLITE_ROW) // i.e., a result, as opposed to DONE (no results)
3266 : : // no need to recheck a file/version we already know
3267 : : // specifically, no need to elf-begin a file we already determined is non-elf
3268 : : // (so is stored with buildid=NULL)
3269 : : {
3270 : 368 : fts_cached++;
3271 : 368 : return;
3272 : : }
3273 : :
3274 : 360 : bool executable_p = false, debuginfo_p = false; // E and/or D
3275 [ + - ]: 360 : string buildid;
3276 [ + - ]: 360 : set<string> sourcefiles;
3277 : :
3278 [ + - ]: 360 : int fd = open (rps.c_str(), O_RDONLY);
3279 : 360 : try
3280 : : {
3281 [ + - ]: 360 : if (fd >= 0)
3282 [ + - ]: 360 : elf_classify (fd, executable_p, debuginfo_p, buildid, sourcefiles);
3283 : : else
3284 [ # # # # : 0 : throw libc_exception(errno, string("open ") + rps);
# # # # ]
3285 [ + - + - : 720 : add_metric ("scanned_bytes_total","source","file",
+ - - + -
+ - - -
- ]
3286 [ + - ]: 360 : st.st_size);
3287 [ + - + - : 720 : inc_metric ("scanned_files_total","source","file");
+ - + - -
+ - + - -
- - ]
3288 : : }
3289 : : // NB: we catch exceptions here too, so that we can
3290 : : // cache the corrupt-elf case (!executable_p &&
3291 : : // !debuginfo_p) just below, just as if we had an
3292 : : // EPERM error from open(2).
3293 [ - - ]: 0 : catch (const reportable_exception& e)
3294 : : {
3295 [ - - ]: 0 : e.report(clog);
3296 : 0 : }
3297 : :
3298 [ + - ]: 360 : if (fd >= 0)
3299 [ + - ]: 360 : close (fd);
3300 : :
3301 [ + + ]: 360 : if (buildid == "")
3302 : : {
3303 : : // no point storing an elf file without buildid
3304 : 298 : executable_p = false;
3305 : 298 : debuginfo_p = false;
3306 : : }
3307 : : else
3308 : : {
3309 : : // register this build-id in the interning table
3310 : 62 : ps_upsert_buildids
3311 [ + - ]: 62 : .reset()
3312 [ + - ]: 62 : .bind(1, buildid)
3313 [ + - ]: 62 : .step_ok_done();
3314 : : }
3315 : :
3316 [ + + ]: 360 : if (executable_p)
3317 : 38 : fts_executable ++;
3318 [ + + ]: 360 : if (debuginfo_p)
3319 : 38 : fts_debuginfo ++;
3320 [ + + + + ]: 360 : if (executable_p || debuginfo_p)
3321 : : {
3322 : 62 : ps_upsert_de
3323 [ + - ]: 62 : .reset()
3324 [ + - ]: 62 : .bind(1, buildid)
3325 [ + + + - ]: 86 : .bind(2, debuginfo_p ? 1 : 0)
3326 [ + + + - ]: 86 : .bind(3, executable_p ? 1 : 0)
3327 [ + - ]: 62 : .bind(4, fileid)
3328 [ + - ]: 62 : .bind(5, st.st_mtime)
3329 [ + - ]: 62 : .step_ok_done();
3330 : : }
3331 [ + + ]: 360 : if (executable_p)
3332 [ + - + - : 76 : inc_metric("found_executable_total","source","files");
+ - + - -
+ - + - -
- - ]
3333 [ + + ]: 360 : if (debuginfo_p)
3334 [ + - + - : 76 : inc_metric("found_debuginfo_total","source","files");
+ - + - -
+ - + - -
- - ]
3335 : :
3336 [ + + + - ]: 398 : if (sourcefiles.size() && buildid != "")
3337 : : {
3338 : 38 : fts_sourcefiles += sourcefiles.size();
3339 : :
3340 [ + + ]: 3112 : for (auto&& dwarfsrc : sourcefiles)
3341 : : {
3342 [ + - ]: 3074 : char *srp = realpath(dwarfsrc.c_str(), NULL);
3343 [ + + ]: 3074 : if (srp == NULL) // also if DWZ unresolved dwarfsrc=""
3344 : 36 : continue; // unresolvable files are not a serious problem
3345 : : // throw libc_exception(errno, "fts/file realpath " + srcpath);
3346 [ + - ]: 3038 : string srps = string(srp);
3347 : 3038 : free (srp);
3348 : :
3349 : 3038 : struct stat sfs;
3350 : 3038 : rc = stat(srps.c_str(), &sfs);
3351 [ - + ]: 3038 : if (rc != 0)
3352 [ # # ]: 0 : continue;
3353 : :
3354 [ + - ]: 3038 : if (verbose > 2)
3355 [ + - + - : 9114 : obatched(clog) << "recorded buildid=" << buildid << " file=" << srps
- - ]
3356 [ + - + - : 3038 : << " mtime=" << sfs.st_mtime
+ - + - ]
3357 [ + - + - : 3038 : << " as source " << dwarfsrc << endl;
+ - ]
3358 : :
3359 : : // PR25548: store canonicalized dwarfsrc path
3360 [ + - ]: 3038 : string dwarfsrc_canon = canon_pathname (dwarfsrc);
3361 [ + + ]: 3038 : if (dwarfsrc_canon != dwarfsrc)
3362 : : {
3363 [ + + ]: 508 : if (verbose > 3)
3364 [ + - + - : 20 : obatched(clog) << "canonicalized src=" << dwarfsrc << " alias=" << dwarfsrc_canon << endl;
+ - + - +
- ]
3365 : : }
3366 : :
3367 [ + - ]: 3038 : int64_t fileid1 = register_file_name (ps_upsert_fileparts, ps_upsert_file, ps_lookup_file, dwarfsrc_canon);
3368 [ + - ]: 3038 : int64_t fileid2 = register_file_name (ps_upsert_fileparts, ps_upsert_file, ps_lookup_file, srps);
3369 : :
3370 : 3038 : ps_upsert_s
3371 [ + - ]: 3038 : .reset()
3372 [ + - ]: 3038 : .bind(1, buildid)
3373 [ + - ]: 3038 : .bind(2, fileid1)
3374 [ + - ]: 3038 : .bind(3, fileid2)
3375 [ + - ]: 3038 : .bind(4, sfs.st_mtime)
3376 [ + - ]: 3038 : .step_ok_done();
3377 : :
3378 [ + - + - : 6076 : inc_metric("found_sourcerefs_total","source","files");
+ - + - -
+ - + + -
- - - - -
- ]
3379 [ + - ]: 6112 : }
3380 : : }
3381 : :
3382 : 360 : ps_scan_done
3383 [ + - ]: 360 : .reset()
3384 [ + - ]: 360 : .bind(1, fileid)
3385 [ + - ]: 360 : .bind(2, st.st_mtime)
3386 [ + - ]: 360 : .bind(3, st.st_size)
3387 [ + - ]: 360 : .step_ok_done();
3388 : :
3389 [ + - ]: 360 : if (verbose > 2)
3390 [ + - + - ]: 1080 : obatched(clog) << "recorded buildid=" << buildid << " file=" << rps
3391 [ + - + - : 360 : << " mtime=" << st.st_mtime << " atype="
+ - + - ]
3392 : : << (executable_p ? "E" : "")
3393 [ + - + + : 1004 : << (debuginfo_p ? "D" : "") << endl;
+ - + + +
- + - ]
3394 [ + + ]: 422 : }
3395 : :
3396 : :
3397 : :
3398 : :
3399 : :
3400 : : // Analyze given archive file of given age; record buildids / exec/debuginfo-ness of its
3401 : : // constituent files with given upsert statements.
3402 : : static void
3403 : 362 : archive_classify (const string& rps, string& archive_extension, int64_t archiveid,
3404 : : sqlite_ps& ps_upsert_buildids, sqlite_ps& ps_upsert_fileparts, sqlite_ps& ps_upsert_file,
3405 : : sqlite_ps& ps_lookup_file,
3406 : : sqlite_ps& ps_upsert_de, sqlite_ps& ps_upsert_sref, sqlite_ps& ps_upsert_sdef,
3407 : : time_t mtime,
3408 : : unsigned& fts_executable, unsigned& fts_debuginfo, unsigned& fts_sref, unsigned& fts_sdef,
3409 : : bool& fts_sref_complete_p)
3410 : : {
3411 : 362 : string archive_decoder = "/dev/null";
3412 [ + + ]: 896 : for (auto&& arch : scan_archives)
3413 [ + + ]: 534 : if (string_endswith(rps, arch.first))
3414 : : {
3415 [ + - ]: 362 : archive_extension = arch.first;
3416 [ + - ]: 896 : archive_decoder = arch.second;
3417 : : }
3418 : :
3419 : 362 : FILE* fp;
3420 : 362 : defer_dtor<FILE*,int>::dtor_fn dfn;
3421 [ + + ]: 362 : if (archive_decoder != "cat")
3422 : : {
3423 [ + - + - : 64 : string popen_cmd = archive_decoder + " " + shell_escape(rps);
+ - - + -
- - - ]
3424 [ + - ]: 32 : fp = popen (popen_cmd.c_str(), "r"); // "e" O_CLOEXEC?
3425 : 32 : dfn = pclose;
3426 [ - + ]: 32 : if (fp == NULL)
3427 [ # # # # : 0 : throw libc_exception (errno, string("popen ") + popen_cmd);
# # # # ]
3428 : 32 : }
3429 : : else
3430 : : {
3431 [ + - ]: 330 : fp = fopen (rps.c_str(), "r");
3432 : 330 : dfn = fclose;
3433 [ - + ]: 330 : if (fp == NULL)
3434 [ # # # # : 0 : throw libc_exception (errno, string("fopen ") + rps);
# # # # ]
3435 : : }
3436 : 362 : defer_dtor<FILE*,int> fp_closer (fp, dfn);
3437 : :
3438 : 362 : struct archive *a;
3439 [ + - ]: 362 : a = archive_read_new();
3440 [ - + ]: 362 : if (a == NULL)
3441 [ # # # # ]: 0 : throw archive_exception("cannot create archive reader");
3442 : 362 : defer_dtor<struct archive*,int> archive_closer (a, archive_read_free);
3443 : :
3444 [ + - ]: 362 : int rc = archive_read_support_format_all(a);
3445 [ - + ]: 362 : if (rc != ARCHIVE_OK)
3446 [ # # # # ]: 0 : throw archive_exception(a, "cannot select all formats");
3447 [ + - ]: 362 : rc = archive_read_support_filter_all(a);
3448 [ - + ]: 362 : if (rc != ARCHIVE_OK)
3449 [ # # # # ]: 0 : throw archive_exception(a, "cannot select all filters");
3450 : :
3451 [ + - ]: 362 : rc = archive_read_open_FILE (a, fp);
3452 [ - + ]: 362 : if (rc != ARCHIVE_OK)
3453 : : {
3454 [ # # # # : 0 : obatched(clog) << "cannot open archive from pipe " << rps << endl;
# # ]
3455 [ # # # # ]: 0 : throw archive_exception(a, "cannot open archive from pipe");
3456 : : }
3457 : :
3458 [ + + ]: 362 : if (verbose > 3)
3459 [ + - + - : 692 : obatched(clog) << "libarchive scanning " << rps << " id " << archiveid << endl;
+ - + - +
- ]
3460 : :
3461 : : bool any_exceptions = false;
3462 : 2564 : while(1) // parse archive entries
3463 : : {
3464 [ + - ]: 2564 : if (interrupted)
3465 : : break;
3466 : :
3467 : 2564 : try
3468 : : {
3469 : 2564 : struct archive_entry *e;
3470 [ + - ]: 2564 : rc = archive_read_next_header (a, &e);
3471 [ + + ]: 2564 : if (rc != ARCHIVE_OK)
3472 : : break;
3473 : :
3474 [ + - + + ]: 2202 : if (! S_ISREG(archive_entry_mode (e))) // skip non-files completely
3475 : 1528 : continue;
3476 : :
3477 [ + - ]: 674 : string fn = canonicalized_archive_entry_pathname (e);
3478 : :
3479 [ + + ]: 674 : if (verbose > 3)
3480 [ + - + - : 1268 : obatched(clog) << "libarchive checking " << fn << endl;
+ - - - ]
3481 : :
3482 : : // extract this file to a temporary file
3483 : 674 : char* tmppath = NULL;
3484 : 674 : rc = asprintf (&tmppath, "%s/debuginfod.XXXXXX", tmpdir.c_str());
3485 [ - + ]: 674 : if (rc < 0)
3486 [ # # # # ]: 0 : throw libc_exception (ENOMEM, "cannot allocate tmppath");
3487 : 674 : defer_dtor<void*,void> tmmpath_freer (tmppath, free);
3488 [ + - ]: 674 : int fd = mkstemp (tmppath);
3489 [ - + ]: 674 : if (fd < 0)
3490 [ # # # # ]: 0 : throw libc_exception (errno, "cannot create temporary file");
3491 : 674 : unlink (tmppath); // unlink now so OS will release the file as soon as we close the fd
3492 : 674 : defer_dtor<int,int> minifd_closer (fd, close);
3493 : :
3494 [ + - ]: 674 : rc = archive_read_data_into_fd (a, fd);
3495 [ - + ]: 673 : if (rc != ARCHIVE_OK)
3496 [ # # # # ]: 0 : throw archive_exception(a, "cannot extract file");
3497 : :
3498 : : // finally ... time to run elf_classify on this bad boy and update the database
3499 : 673 : bool executable_p = false, debuginfo_p = false;
3500 [ + - ]: 673 : string buildid;
3501 [ + - ]: 673 : set<string> sourcefiles;
3502 [ + - ]: 673 : elf_classify (fd, executable_p, debuginfo_p, buildid, sourcefiles);
3503 : : // NB: might throw
3504 : :
3505 [ + + ]: 674 : if (buildid != "") // intern buildid
3506 : : {
3507 : 352 : ps_upsert_buildids
3508 [ + - ]: 352 : .reset()
3509 [ + - ]: 352 : .bind(1, buildid)
3510 [ + - ]: 352 : .step_ok_done();
3511 : : }
3512 : :
3513 [ + - ]: 674 : int64_t fileid = register_file_name (ps_upsert_fileparts, ps_upsert_file, ps_lookup_file, fn);
3514 : :
3515 [ + + ]: 674 : if (sourcefiles.size() > 0) // sref records needed
3516 : : {
3517 : : // NB: we intern each source file once. Once raw, as it
3518 : : // appears in the DWARF file list coming back from
3519 : : // elf_classify() - because it'll end up in the
3520 : : // _norm.artifactsrc column. We don't also put another
3521 : : // version with a '.' at the front, even though that's
3522 : : // how rpm/cpio packs names, because we hide that from
3523 : : // the database for storage efficiency.
3524 : :
3525 [ + + ]: 616 : for (auto&& s : sourcefiles)
3526 : : {
3527 [ - + ]: 464 : if (s == "")
3528 : : {
3529 : 0 : fts_sref_complete_p = false;
3530 : 0 : continue;
3531 : : }
3532 : :
3533 : : // PR25548: store canonicalized source path
3534 : 464 : const string& dwarfsrc = s;
3535 [ + - ]: 464 : string dwarfsrc_canon = canon_pathname (dwarfsrc);
3536 [ + + ]: 464 : if (dwarfsrc_canon != dwarfsrc)
3537 : : {
3538 [ + - ]: 32 : if (verbose > 3)
3539 [ + - + - : 64 : obatched(clog) << "canonicalized src=" << dwarfsrc << " alias=" << dwarfsrc_canon << endl;
+ - + - +
- - - ]
3540 : : }
3541 : :
3542 [ + - ]: 464 : int64_t srcfileid = register_file_name(ps_upsert_fileparts, ps_upsert_file, ps_lookup_file,
3543 : : dwarfsrc_canon);
3544 : :
3545 : 464 : ps_upsert_sref
3546 [ + - ]: 464 : .reset()
3547 [ + - ]: 464 : .bind(1, buildid)
3548 [ + - ]: 464 : .bind(2, srcfileid)
3549 [ + - ]: 464 : .step_ok_done();
3550 : :
3551 [ + - ]: 464 : fts_sref ++;
3552 : 464 : }
3553 : : }
3554 : :
3555 [ + + ]: 674 : if (executable_p)
3556 : 150 : fts_executable ++;
3557 [ + + ]: 674 : if (debuginfo_p)
3558 : 202 : fts_debuginfo ++;
3559 : :
3560 [ + + + + ]: 674 : if (executable_p || debuginfo_p)
3561 : : {
3562 : 352 : ps_upsert_de
3563 [ + - ]: 352 : .reset()
3564 [ + - ]: 352 : .bind(1, buildid)
3565 [ + + + - ]: 502 : .bind(2, debuginfo_p ? 1 : 0)
3566 [ + + + - ]: 554 : .bind(3, executable_p ? 1 : 0)
3567 [ + - ]: 352 : .bind(4, archiveid)
3568 [ + - ]: 352 : .bind(5, mtime)
3569 [ + - ]: 352 : .bind(6, fileid)
3570 [ + - ]: 352 : .step_ok_done();
3571 : : }
3572 : : else // potential source - sdef record
3573 : : {
3574 : 322 : fts_sdef ++;
3575 : 322 : ps_upsert_sdef
3576 [ + - ]: 322 : .reset()
3577 [ + - ]: 322 : .bind(1, archiveid)
3578 [ + - ]: 322 : .bind(2, mtime)
3579 [ + - ]: 322 : .bind(3, fileid)
3580 [ + - ]: 322 : .step_ok_done();
3581 : : }
3582 : :
3583 [ + - + + : 674 : if ((verbose > 2) && (executable_p || debuginfo_p))
+ + ]
3584 [ + - + - ]: 1056 : obatched(clog) << "recorded buildid=" << buildid << " rpm=" << rps << " file=" << fn
3585 [ + - + - : 352 : << " mtime=" << mtime << " atype="
+ - + - +
- + - ]
3586 : : << (executable_p ? "E" : "")
3587 : : << (debuginfo_p ? "D" : "")
3588 [ + - + + : 704 : << " sourcefiles=" << sourcefiles.size() << endl;
+ - + + +
- + - + -
+ - ]
3589 : :
3590 [ + + + + ]: 1532 : }
3591 [ - - ]: 0 : catch (const reportable_exception& e)
3592 : : {
3593 [ - - ]: 0 : e.report(clog);
3594 : 0 : any_exceptions = true;
3595 : : // NB: but we allow the libarchive iteration to continue, in
3596 : : // case we can still gather some useful information. That
3597 : : // would allow some webapi queries to work, until later when
3598 : : // this archive is rescanned. (Its vitals won't go into the
3599 : : // _file_mtime_scanned table until after a successful scan.)
3600 : 0 : }
3601 : : }
3602 : :
3603 [ - + ]: 362 : if (any_exceptions)
3604 [ # # # # ]: 0 : throw reportable_exception("exceptions encountered during archive scan");
3605 [ + + ]: 378 : }
3606 : :
3607 : :
3608 : :
3609 : : // scan for archive files such as .rpm
3610 : : static void
3611 : 702 : scan_archive_file (const string& rps, const stat_t& st,
3612 : : sqlite_ps& ps_upsert_buildids,
3613 : : sqlite_ps& ps_upsert_fileparts,
3614 : : sqlite_ps& ps_upsert_file,
3615 : : sqlite_ps& ps_lookup_file,
3616 : : sqlite_ps& ps_upsert_de,
3617 : : sqlite_ps& ps_upsert_sref,
3618 : : sqlite_ps& ps_upsert_sdef,
3619 : : sqlite_ps& ps_query,
3620 : : sqlite_ps& ps_scan_done,
3621 : : unsigned& fts_cached,
3622 : : unsigned& fts_executable,
3623 : : unsigned& fts_debuginfo,
3624 : : unsigned& fts_sref,
3625 : : unsigned& fts_sdef)
3626 : : {
3627 : : // intern the archive file name
3628 : 702 : int64_t archiveid = register_file_name (ps_upsert_fileparts, ps_upsert_file, ps_lookup_file, rps);
3629 : :
3630 : : /* See if we know of it already. */
3631 : 702 : int rc = ps_query
3632 : 702 : .reset()
3633 : 702 : .bind(1, archiveid)
3634 : 702 : .bind(2, st.st_mtime)
3635 : 702 : .step();
3636 : 702 : ps_query.reset();
3637 [ + + ]: 702 : if (rc == SQLITE_ROW) // i.e., a result, as opposed to DONE (no results)
3638 : : // no need to recheck a file/version we already know
3639 : : // specifically, no need to parse this archive again, since we already have
3640 : : // it as a D or E or S record,
3641 : : // (so is stored with buildid=NULL)
3642 : : {
3643 : 340 : fts_cached ++;
3644 : 340 : return;
3645 : : }
3646 : :
3647 : : // extract the archive contents
3648 : 362 : unsigned my_fts_executable = 0, my_fts_debuginfo = 0, my_fts_sref = 0, my_fts_sdef = 0;
3649 : 362 : bool my_fts_sref_complete_p = true;
3650 : 362 : bool any_exceptions = false;
3651 : 362 : try
3652 : : {
3653 [ + - ]: 362 : string archive_extension;
3654 : 362 : archive_classify (rps, archive_extension, archiveid,
3655 : : ps_upsert_buildids, ps_upsert_fileparts, ps_upsert_file, ps_lookup_file,
3656 : : ps_upsert_de, ps_upsert_sref, ps_upsert_sdef, // dalt
3657 [ + - ]: 362 : st.st_mtime,
3658 : : my_fts_executable, my_fts_debuginfo, my_fts_sref, my_fts_sdef,
3659 : : my_fts_sref_complete_p);
3660 [ + - + - : 724 : add_metric ("scanned_bytes_total","source",archive_extension + " archive",
+ - - + +
+ - - -
- ]
3661 [ + - ]: 362 : st.st_size);
3662 [ + - + - : 724 : inc_metric ("scanned_files_total","source",archive_extension + " archive");
+ - + - -
+ + + - -
- - ]
3663 [ + - + - : 724 : add_metric("found_debuginfo_total","source",archive_extension + " archive",
+ - + - -
+ + + - -
- - ]
3664 : : my_fts_debuginfo);
3665 [ + - + - : 724 : add_metric("found_executable_total","source",archive_extension + " archive",
+ - + - -
+ + + - -
- - ]
3666 : : my_fts_executable);
3667 [ + - + - : 740 : add_metric("found_sourcerefs_total","source",archive_extension + " archive",
+ - + - -
+ + + - +
- - - - -
- ]
3668 : : my_fts_sref);
3669 : 362 : }
3670 [ - - ]: 0 : catch (const reportable_exception& e)
3671 : : {
3672 [ - - ]: 0 : e.report(clog);
3673 : 0 : any_exceptions = true;
3674 : 0 : }
3675 : :
3676 [ + - ]: 362 : if (verbose > 2)
3677 [ + - ]: 1086 : obatched(clog) << "scanned archive=" << rps
3678 [ + - + - ]: 362 : << " mtime=" << st.st_mtime
3679 [ + - ]: 362 : << " executables=" << my_fts_executable
3680 [ + - + - ]: 362 : << " debuginfos=" << my_fts_debuginfo
3681 [ + - + - ]: 362 : << " srefs=" << my_fts_sref
3682 [ + - + - ]: 362 : << " sdefs=" << my_fts_sdef
3683 [ + - + - : 362 : << " exceptions=" << any_exceptions
+ - + - ]
3684 : 362 : << endl;
3685 : :
3686 : 362 : fts_executable += my_fts_executable;
3687 : 362 : fts_debuginfo += my_fts_debuginfo;
3688 : 362 : fts_sref += my_fts_sref;
3689 : 362 : fts_sdef += my_fts_sdef;
3690 : :
3691 [ - + ]: 362 : if (any_exceptions)
3692 [ # # # # ]: 0 : throw reportable_exception("exceptions encountered during archive scan");
3693 : :
3694 [ + - ]: 362 : if (my_fts_sref_complete_p) // leave incomplete?
3695 : 362 : ps_scan_done
3696 : 362 : .reset()
3697 : 362 : .bind(1, archiveid)
3698 : 362 : .bind(2, st.st_mtime)
3699 : 362 : .bind(3, st.st_size)
3700 : 362 : .step_ok_done();
3701 : : }
3702 : :
3703 : :
3704 : :
3705 : : ////////////////////////////////////////////////////////////////////////
3706 : :
3707 : :
3708 : :
3709 : : // The thread that consumes file names off of the scanq. We hold
3710 : : // the persistent sqlite_ps's at this level and delegate file/archive
3711 : : // scanning to other functions.
3712 : : static void
3713 : 256 : scan ()
3714 : : {
3715 : : // all the prepared statements fit to use, the _f_ set:
3716 [ + - + - : 512 : sqlite_ps ps_f_upsert_buildids (db, "file-buildids-intern", "insert or ignore into " BUILDIDS "_buildids VALUES (NULL, ?);");
+ - - - ]
3717 [ + - + - : 512 : sqlite_ps ps_f_upsert_fileparts (db, "file-fileparts-intern", "insert or ignore into " BUILDIDS "_fileparts VALUES (NULL, ?);");
+ - + - -
- ]
3718 [ + - ]: 256 : sqlite_ps ps_f_upsert_file (db, "file-file-intern", "insert or ignore into " BUILDIDS "_files VALUES (NULL, \n"
3719 : : "(select id from " BUILDIDS "_fileparts where name = ?),\n"
3720 [ + - + - : 512 : "(select id from " BUILDIDS "_fileparts where name = ?));");
+ - + - -
- ]
3721 [ + - ]: 256 : sqlite_ps ps_f_lookup_file (db, "file-file-lookup",
3722 : : "select f.id\n"
3723 : : " from " BUILDIDS "_files f, " BUILDIDS "_fileparts p1, " BUILDIDS "_fileparts p2 \n"
3724 [ + - + - : 512 : " where f.dirname = p1.id and f.basename = p2.id and p1.name = ? and p2.name = ?;\n");
+ - + - -
- ]
3725 [ + - ]: 256 : sqlite_ps ps_f_upsert_de (db, "file-de-upsert",
3726 : : "insert or ignore into " BUILDIDS "_f_de "
3727 : : "(buildid, debuginfo_p, executable_p, file, mtime) "
3728 : : "values ((select id from " BUILDIDS "_buildids where hex = ?),"
3729 [ + - + - : 512 : " ?,?,?,?);");
+ - + - -
- ]
3730 [ + - ]: 256 : sqlite_ps ps_f_upsert_s (db, "file-s-upsert",
3731 : : "insert or ignore into " BUILDIDS "_f_s "
3732 : : "(buildid, artifactsrc, file, mtime) "
3733 : : "values ((select id from " BUILDIDS "_buildids where hex = ?),"
3734 [ + - + - : 512 : " ?,?,?);");
+ - + - -
- ]
3735 [ + - ]: 256 : sqlite_ps ps_f_query (db, "file-negativehit-find",
3736 : : "select 1 from " BUILDIDS "_file_mtime_scanned where sourcetype = 'F' "
3737 [ + - + - : 512 : "and file = ? and mtime = ?;");
+ - + - -
- ]
3738 [ + - ]: 256 : sqlite_ps ps_f_scan_done (db, "file-scanned",
3739 : : "insert or ignore into " BUILDIDS "_file_mtime_scanned (sourcetype, file, mtime, size)"
3740 [ + - + - : 512 : "values ('F', ?,?,?);");
+ - + - -
- ]
3741 : :
3742 : : // and now for the _r_ set
3743 [ + - + - : 512 : sqlite_ps ps_r_upsert_buildids (db, "rpm-buildid-intern", "insert or ignore into " BUILDIDS "_buildids VALUES (NULL, ?);");
+ - + - -
- ]
3744 [ + - + - : 512 : sqlite_ps ps_r_upsert_fileparts (db, "rpm-fileparts-intern", "insert or ignore into " BUILDIDS "_fileparts VALUES (NULL, ?);");
+ - + - -
- ]
3745 [ + - ]: 256 : sqlite_ps ps_r_upsert_file (db, "rpm-file-intern", "insert or ignore into " BUILDIDS "_files VALUES (NULL, \n"
3746 : : "(select id from " BUILDIDS "_fileparts where name = ?),\n"
3747 [ + - + - : 512 : "(select id from " BUILDIDS "_fileparts where name = ?));");
+ - + - -
- ]
3748 [ + - ]: 256 : sqlite_ps ps_r_lookup_file (db, "rpm-file-lookup",
3749 : : "select f.id\n"
3750 : : " from " BUILDIDS "_files f, " BUILDIDS "_fileparts p1, " BUILDIDS "_fileparts p2 \n"
3751 [ + - + - : 512 : " where f.dirname = p1.id and f.basename = p2.id and p1.name = ? and p2.name = ?;\n");
+ - + - -
- ]
3752 [ + - ]: 256 : sqlite_ps ps_r_upsert_de (db, "rpm-de-insert",
3753 : : "insert or ignore into " BUILDIDS "_r_de (buildid, debuginfo_p, executable_p, file, mtime, content) values ("
3754 [ + - + - : 512 : "(select id from " BUILDIDS "_buildids where hex = ?), ?, ?, ?, ?, ?);");
+ - + - -
- ]
3755 [ + - ]: 256 : sqlite_ps ps_r_upsert_sref (db, "rpm-sref-insert",
3756 : : "insert or ignore into " BUILDIDS "_r_sref (buildid, artifactsrc) values ("
3757 : : "(select id from " BUILDIDS "_buildids where hex = ?), "
3758 [ + - + - : 512 : "?);");
+ - + - -
- ]
3759 [ + - ]: 256 : sqlite_ps ps_r_upsert_sdef (db, "rpm-sdef-insert",
3760 : : "insert or ignore into " BUILDIDS "_r_sdef (file, mtime, content) values ("
3761 [ + - + - : 512 : "?, ?, ?);");
+ - + - -
- ]
3762 [ + - ]: 256 : sqlite_ps ps_r_query (db, "rpm-negativehit-query",
3763 : : "select 1 from " BUILDIDS "_file_mtime_scanned where "
3764 [ + - + - : 512 : "sourcetype = 'R' and file = ? and mtime = ?;");
+ - + - -
- ]
3765 [ + - ]: 256 : sqlite_ps ps_r_scan_done (db, "rpm-scanned",
3766 : : "insert or ignore into " BUILDIDS "_file_mtime_scanned (sourcetype, file, mtime, size)"
3767 [ + - + - : 512 : "values ('R', ?, ?, ?);");
+ - + - -
- ]
3768 : :
3769 : :
3770 : 256 : unsigned fts_cached = 0, fts_executable = 0, fts_debuginfo = 0, fts_sourcefiles = 0;
3771 : 256 : unsigned fts_sref = 0, fts_sdef = 0;
3772 : :
3773 [ + - + - : 512 : add_metric("thread_count", "role", "scan", 1);
+ - + - -
+ - + - -
- - ]
3774 [ + - + - : 512 : add_metric("thread_busy", "role", "scan", 1);
+ - + - -
+ - + - -
- - ]
3775 [ + + ]: 1452 : while (! interrupted)
3776 : : {
3777 [ + - ]: 1196 : scan_payload p;
3778 : :
3779 [ + - + - : 2392 : add_metric("thread_busy", "role", "scan", -1);
+ - + - -
+ - + - -
- - ]
3780 : : // NB: threads may be blocked within either of these two waiting
3781 : : // states, if the work queue happens to run dry. That's OK.
3782 [ + - + - ]: 1196 : if (scan_barrier) scan_barrier->count();
3783 [ + - ]: 1196 : bool gotone = scanq.wait_front(p);
3784 [ + - + - : 2392 : add_metric("thread_busy", "role", "scan", 1);
+ - + - -
+ - + - -
- - ]
3785 : :
3786 [ + + - + ]: 1196 : if (! gotone) continue; // go back to waiting
3787 : :
3788 : 940 : try
3789 : : {
3790 : 940 : bool scan_archive = false;
3791 [ + + ]: 2240 : for (auto&& arch : scan_archives)
3792 [ + + ]: 1300 : if (string_endswith(p.first, arch.first))
3793 : 702 : scan_archive = true;
3794 : :
3795 [ + + ]: 940 : if (scan_archive)
3796 [ + - ]: 702 : scan_archive_file (p.first, p.second,
3797 : : ps_r_upsert_buildids,
3798 : : ps_r_upsert_fileparts,
3799 : : ps_r_upsert_file,
3800 : : ps_r_lookup_file,
3801 : : ps_r_upsert_de,
3802 : : ps_r_upsert_sref,
3803 : : ps_r_upsert_sdef,
3804 : : ps_r_query,
3805 : : ps_r_scan_done,
3806 : : fts_cached,
3807 : : fts_executable,
3808 : : fts_debuginfo,
3809 : : fts_sref,
3810 : : fts_sdef);
3811 : :
3812 [ + + ]: 940 : if (scan_files) // NB: maybe "else if" ?
3813 [ + - ]: 728 : scan_source_file (p.first, p.second,
3814 : : ps_f_upsert_buildids,
3815 : : ps_f_upsert_fileparts,
3816 : : ps_f_upsert_file,
3817 : : ps_f_lookup_file,
3818 : : ps_f_upsert_de,
3819 : : ps_f_upsert_s,
3820 : : ps_f_query,
3821 : : ps_f_scan_done,
3822 : : fts_cached, fts_executable, fts_debuginfo, fts_sourcefiles);
3823 : : }
3824 [ - - ]: 0 : catch (const reportable_exception& e)
3825 : : {
3826 [ - - ]: 0 : e.report(cerr);
3827 : 0 : }
3828 : :
3829 [ + - ]: 940 : scanq.done_front(); // let idlers run
3830 : :
3831 : 940 : if (fts_cached || fts_executable || fts_debuginfo || fts_sourcefiles || fts_sref || fts_sdef)
3832 : : {} // NB: not just if a successful scan - we might have encountered -ENOSPC & failed
3833 [ + - + - ]: 940 : (void) statfs_free_enough_p(db_path, "database"); // report sqlite filesystem size
3834 [ + - + - ]: 940 : (void) statfs_free_enough_p(tmpdir, "tmpdir"); // this too, in case of fdcache/tmpfile usage
3835 : :
3836 : : // finished a scanning step -- not a "loop", because we just
3837 : : // consume the traversal loop's work, whenever
3838 [ + - + - : 1880 : inc_metric("thread_work_total","role","scan");
+ - + - -
+ - + + -
- - - - -
- ]
3839 : 1196 : }
3840 : :
3841 [ + - + - : 512 : add_metric("thread_busy", "role", "scan", -1);
+ - + - -
+ - + - -
- - ]
3842 : 256 : }
3843 : :
3844 : :
3845 : : // Use this function as the thread entry point, so it can catch our
3846 : : // fleet of exceptions (incl. the sqlite_ps ctors) and report.
3847 : : static void*
3848 : 256 : thread_main_scanner (void* arg)
3849 : : {
3850 : 256 : (void) arg;
3851 [ + + ]: 768 : while (! interrupted)
3852 : 256 : try
3853 : : {
3854 [ + - ]: 256 : scan();
3855 : : }
3856 [ - - ]: 0 : catch (const reportable_exception& e)
3857 : : {
3858 [ - - ]: 0 : e.report(cerr);
3859 : 0 : }
3860 : 256 : return 0;
3861 : : }
3862 : :
3863 : :
3864 : :
3865 : : // The thread that traverses all the source_paths and enqueues all the
3866 : : // matching files into the file/archive scan queue.
3867 : : static void
3868 : 118 : scan_source_paths()
3869 : : {
3870 : : // NB: fedora 31 glibc/fts(3) crashes inside fts_read() on empty
3871 : : // path list.
3872 [ + + ]: 118 : if (source_paths.empty())
3873 : 2 : return;
3874 : :
3875 : : // Turn the source_paths into an fts(3)-compatible char**. Since
3876 : : // source_paths[] does not change after argv processing, the
3877 : : // c_str()'s are safe to keep around awile.
3878 : 116 : vector<const char *> sps;
3879 [ + + ]: 314 : for (auto&& sp: source_paths)
3880 [ + - ]: 198 : sps.push_back(sp.c_str());
3881 [ + - - - ]: 116 : sps.push_back(NULL);
3882 : :
3883 [ + + + - ]: 218 : FTS *fts = fts_open ((char * const *)sps.data(),
3884 : : (traverse_logical ? FTS_LOGICAL : FTS_PHYSICAL|FTS_XDEV)
3885 : : | FTS_NOCHDIR /* multithreaded */,
3886 : : NULL);
3887 [ - + ]: 116 : if (fts == NULL)
3888 [ # # # # ]: 0 : throw libc_exception(errno, "cannot fts_open");
3889 : 116 : defer_dtor<FTS*,int> fts_cleanup (fts, fts_close);
3890 : :
3891 : 116 : struct timespec ts_start, ts_end;
3892 : 116 : clock_gettime (CLOCK_MONOTONIC, &ts_start);
3893 : 116 : unsigned fts_scanned = 0, fts_regex = 0;
3894 : :
3895 : 116 : FTSENT *f;
3896 [ + - + + ]: 2116 : while ((f = fts_read (fts)) != NULL)
3897 : : {
3898 [ + - ]: 1884 : if (interrupted) break;
3899 : :
3900 [ - + ]: 1884 : if (sigusr2 != forced_groom_count) // stop early if groom triggered
3901 : : {
3902 [ # # ]: 0 : scanq.clear(); // clear previously issued work for scanner threads
3903 : : break;
3904 : : }
3905 : :
3906 : 1884 : fts_scanned ++;
3907 : :
3908 [ + - ]: 1884 : if (verbose > 2)
3909 [ + - + - : 3768 : obatched(clog) << "fts traversing " << f->fts_path << endl;
+ - ]
3910 : :
3911 [ + + + + : 1884 : switch (f->fts_info)
+ ]
3912 : : {
3913 : 1028 : case FTS_F:
3914 : 1028 : {
3915 : : /* Found a file. Convert it to an absolute path, so
3916 : : the buildid database does not have relative path
3917 : : names that are unresolvable from a subsequent run
3918 : : in a different cwd. */
3919 [ + - ]: 1028 : char *rp = realpath(f->fts_path, NULL);
3920 [ - + ]: 1028 : if (rp == NULL)
3921 : 0 : continue; // ignore dangling symlink or such
3922 [ + - ]: 1028 : string rps = string(rp);
3923 : 1028 : free (rp);
3924 : :
3925 [ + - ]: 1028 : bool ri = !regexec (&file_include_regex, rps.c_str(), 0, 0, 0);
3926 [ + - ]: 1028 : bool rx = !regexec (&file_exclude_regex, rps.c_str(), 0, 0, 0);
3927 [ + + ]: 1028 : if (!ri || rx)
3928 : : {
3929 [ + - ]: 88 : if (verbose > 3)
3930 [ + - ]: 176 : obatched(clog) << "fts skipped by regex "
3931 [ + + + - : 96 : << (!ri ? "I" : "") << (rx ? "X" : "") << endl;
+ + + - +
- ]
3932 : 88 : fts_regex ++;
3933 [ + + ]: 88 : if (!ri)
3934 [ + - + - : 8 : inc_metric("traversed_total","type","file-skipped-I");
+ - + - -
+ - + - -
- - ]
3935 [ + + ]: 88 : if (rx)
3936 [ + - + - : 168 : inc_metric("traversed_total","type","file-skipped-X");
+ - + - -
+ - + - -
- - ]
3937 : : }
3938 : : else
3939 : : {
3940 [ + - + - ]: 940 : scanq.push_back (make_pair(rps, *f->fts_statp));
3941 [ + - + - : 1880 : inc_metric("traversed_total","type","file");
+ - + - -
+ - + - -
- - - - ]
3942 : : }
3943 : 0 : }
3944 : 1028 : break;
3945 : :
3946 : 4 : case FTS_ERR:
3947 : 4 : case FTS_NS:
3948 : : // report on some types of errors because they may reflect fixable misconfiguration
3949 : 4 : {
3950 [ + - + - : 8 : auto x = libc_exception(f->fts_errno, string("fts traversal ") + string(f->fts_path));
+ - + - -
+ - + -
- ]
3951 [ + - ]: 4 : x.report(cerr);
3952 : 0 : }
3953 [ + - + - : 8 : inc_metric("traversed_total","type","error");
+ - + - -
+ - + - -
- - ]
3954 : 4 : break;
3955 : :
3956 : 12 : case FTS_SL: // ignore, but count because debuginfod -L would traverse these
3957 [ + - + - : 24 : inc_metric("traversed_total","type","symlink");
+ - + - -
+ - + - -
- - ]
3958 : 12 : break;
3959 : :
3960 : 420 : case FTS_D: // ignore
3961 [ + - + - : 840 : inc_metric("traversed_total","type","directory");
+ - + - -
+ - + - -
- - ]
3962 : 420 : break;
3963 : :
3964 : 420 : default: // ignore
3965 [ + - + - : 840 : inc_metric("traversed_total","type","other");
+ - + - -
+ - + - -
- - ]
3966 : 420 : break;
3967 : : }
3968 : : }
3969 : 116 : clock_gettime (CLOCK_MONOTONIC, &ts_end);
3970 : 116 : double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
3971 : :
3972 [ + - + - : 348 : obatched(clog) << "fts traversed source paths in " << deltas << "s, scanned=" << fts_scanned
+ - + - ]
3973 [ + - + - : 116 : << ", regex-skipped=" << fts_regex << endl;
+ - ]
3974 [ + - ]: 232 : }
3975 : :
3976 : :
3977 : : static void*
3978 : 64 : thread_main_fts_source_paths (void* arg)
3979 : : {
3980 : 64 : (void) arg; // ignore; we operate on global data
3981 : :
3982 [ + - + - : 128 : set_metric("thread_tid", "role","traverse", tid());
+ - - + -
+ - - -
- ]
3983 [ + - + - : 128 : add_metric("thread_count", "role", "traverse", 1);
+ - - + -
+ - - -
- ]
3984 : :
3985 : 64 : time_t last_rescan = 0;
3986 : :
3987 [ + - ]: 281 : while (! interrupted)
3988 : : {
3989 : 281 : sleep (1);
3990 : 281 : scanq.wait_idle(); // don't start a new traversal while scanners haven't finished the job
3991 : 281 : scanq.done_idle(); // release the hounds
3992 [ + + ]: 281 : if (interrupted) break;
3993 : :
3994 : 217 : time_t now = time(NULL);
3995 : 217 : bool rescan_now = false;
3996 [ + + ]: 217 : if (last_rescan == 0) // at least one initial rescan is documented even for -t0
3997 : 62 : rescan_now = true;
3998 [ + + + + ]: 217 : if (rescan_s > 0 && (long)now > (long)(last_rescan + rescan_s))
3999 : 217 : rescan_now = true;
4000 [ + + ]: 217 : if (sigusr1 != forced_rescan_count)
4001 : : {
4002 : 60 : forced_rescan_count = sigusr1;
4003 : 60 : rescan_now = true;
4004 : : }
4005 [ + + ]: 217 : if (rescan_now)
4006 : : {
4007 [ + - + - : 236 : set_metric("thread_busy", "role","traverse", 1);
+ - - + -
+ - - -
- ]
4008 : 118 : try
4009 : : {
4010 [ + - ]: 118 : scan_source_paths();
4011 : : }
4012 [ - - ]: 0 : catch (const reportable_exception& e)
4013 : : {
4014 [ - - ]: 0 : e.report(cerr);
4015 : 0 : }
4016 : 118 : last_rescan = time(NULL); // NB: now was before scanning
4017 : : // finished a traversal loop
4018 [ + - + - : 236 : inc_metric("thread_work_total", "role","traverse");
+ - - + -
+ - - -
- ]
4019 [ + - + - : 236 : set_metric("thread_busy", "role","traverse", 0);
+ - - + -
+ - - -
- ]
4020 : : }
4021 : : }
4022 : :
4023 : 64 : return 0;
4024 : : }
4025 : :
4026 : :
4027 : :
4028 : : ////////////////////////////////////////////////////////////////////////
4029 : :
4030 : : static void
4031 : 70 : database_stats_report()
4032 : : {
4033 : 70 : sqlite_ps ps_query (db, "database-overview",
4034 [ + - + - : 140 : "select label,quantity from " BUILDIDS "_stats");
+ - - - ]
4035 : :
4036 [ + - + - ]: 140 : obatched(clog) << "database record counts:" << endl;
4037 : 1588 : while (1)
4038 : : {
4039 [ + - ]: 829 : if (interrupted) break;
4040 [ + + ]: 829 : if (sigusr1 != forced_rescan_count) // stop early if scan triggered
4041 : : break;
4042 : :
4043 [ + - ]: 828 : int rc = ps_query.step();
4044 [ + + ]: 828 : if (rc == SQLITE_DONE) break;
4045 [ - + ]: 759 : if (rc != SQLITE_ROW)
4046 [ # # # # ]: 0 : throw sqlite_exception(rc, "step");
4047 : :
4048 [ + - ]: 759 : obatched(clog)
4049 [ + - - + : 759 : << ((const char*) sqlite3_column_text(ps_query, 0) ?: (const char*) "NULL")
+ - ]
4050 : : << " "
4051 [ + - + - : 1518 : << (sqlite3_column_text(ps_query, 1) ?: (const unsigned char*) "NULL")
- + + - ]
4052 : 759 : << endl;
4053 : :
4054 [ + - + - : 1518 : set_metric("groom", "statistic",
- + + - +
- + - + -
- + + + -
- - - ]
4055 [ + - ]: 759 : ((const char*) sqlite3_column_text(ps_query, 0) ?: (const char*) "NULL"),
4056 : : (sqlite3_column_double(ps_query, 1)));
4057 : 759 : }
4058 : 70 : }
4059 : :
4060 : :
4061 : : // Do a round of database grooming that might take many minutes to run.
4062 : 70 : void groom()
4063 : : {
4064 [ + - ]: 140 : obatched(clog) << "grooming database" << endl;
4065 : :
4066 : 70 : struct timespec ts_start, ts_end;
4067 : 70 : clock_gettime (CLOCK_MONOTONIC, &ts_start);
4068 : :
4069 : : // scan for files that have disappeared
4070 : 70 : sqlite_ps files (db, "check old files",
4071 : : "select distinct s.mtime, s.file, f.name from "
4072 : : BUILDIDS "_file_mtime_scanned s, " BUILDIDS "_files_v f "
4073 [ + - + - : 140 : "where f.id = s.file");
+ - - - ]
4074 : : // NB: Because _ftime_mtime_scanned can contain both F and
4075 : : // R records for the same file, this query would return duplicates if the
4076 : : // DISTINCT qualifier were not there.
4077 [ + - ]: 70 : files.reset();
4078 : :
4079 : : // DECISION TIME - we enumerate stale fileids/mtimes
4080 [ + - ]: 70 : deque<pair<int64_t,int64_t> > stale_fileid_mtime;
4081 : :
4082 : 70 : time_t time_start = time(NULL);
4083 : 302 : while(1)
4084 : : {
4085 : : // PR28514: limit grooming iteration to O(rescan time), to avoid
4086 : : // slow filesystem tests over many files locking out rescans for
4087 : : // too long.
4088 [ + + - + ]: 186 : if (rescan_s > 0 && (long)time(NULL) > (long)(time_start + rescan_s))
4089 : : {
4090 [ # # # # : 0 : inc_metric("groomed_total", "decision", "aborted");
# # # # #
# # # # #
# # ]
4091 : 0 : break;
4092 : : }
4093 : :
4094 [ + - ]: 186 : if (interrupted) break;
4095 : :
4096 [ + - ]: 186 : int rc = files.step();
4097 [ + + ]: 186 : if (rc != SQLITE_ROW)
4098 : : break;
4099 : :
4100 [ + - ]: 116 : int64_t mtime = sqlite3_column_int64 (files, 0);
4101 [ + - ]: 116 : int64_t fileid = sqlite3_column_int64 (files, 1);
4102 [ + - - + ]: 116 : const char* filename = ((const char*) sqlite3_column_text (files, 2) ?: "");
4103 : 116 : struct stat s;
4104 : 116 : bool regex_file_drop = 0;
4105 : :
4106 [ + + ]: 116 : if (regex_groom)
4107 : : {
4108 [ + - ]: 16 : bool reg_include = !regexec (&file_include_regex, filename, 0, 0, 0);
4109 [ + - ]: 16 : bool reg_exclude = !regexec (&file_exclude_regex, filename, 0, 0, 0);
4110 : 16 : regex_file_drop = !reg_include || reg_exclude; // match logic of scan_source_paths
4111 : : }
4112 : :
4113 : 116 : rc = stat(filename, &s);
4114 [ + + - + ]: 116 : if ( regex_file_drop || rc < 0 || (mtime != (int64_t) s.st_mtime) )
4115 : : {
4116 [ + - ]: 24 : if (verbose > 2)
4117 [ + - + - : 48 : obatched(clog) << "groom: stale file=" << filename << " mtime=" << mtime << endl;
+ - + - +
- ]
4118 [ + - ]: 24 : stale_fileid_mtime.push_back(make_pair(fileid,mtime));
4119 [ + - + - : 48 : inc_metric("groomed_total", "decision", "stale");
+ - + - -
+ - + - -
- - ]
4120 [ + - + - : 48 : set_metric("thread_work_pending","role","groom", stale_fileid_mtime.size());
+ - + - -
+ - + - -
- - ]
4121 : : }
4122 : : else
4123 [ + - + - : 184 : inc_metric("groomed_total", "decision", "fresh");
+ - + - -
+ - + - -
- - ]
4124 : :
4125 [ + - ]: 116 : if (sigusr1 != forced_rescan_count) // stop early if scan triggered
4126 : : break;
4127 : 116 : }
4128 [ + - ]: 70 : files.reset();
4129 : :
4130 : : // ACTION TIME
4131 : :
4132 : : // Now that we know which file/mtime tuples are stale, actually do
4133 : : // the deletion from the database. Doing this during the SELECT
4134 : : // iteration above results in undefined behaviour in sqlite, as per
4135 : : // https://www.sqlite.org/isolation.html
4136 : :
4137 : : // We could shuffle stale_fileid_mtime[] here. It'd let aborted
4138 : : // sequences of nuke operations resume at random locations, instead
4139 : : // of just starting over. But it doesn't matter much either way,
4140 : : // as long as we make progress.
4141 : :
4142 [ + - + - : 140 : sqlite_ps files_del_f_de (db, "nuke f_de", "delete from " BUILDIDS "_f_de where file = ? and mtime = ?");
+ - + - -
- ]
4143 [ + - + - : 140 : sqlite_ps files_del_r_de (db, "nuke r_de", "delete from " BUILDIDS "_r_de where file = ? and mtime = ?");
+ - + - -
- ]
4144 [ + - ]: 70 : sqlite_ps files_del_scan (db, "nuke f_m_s", "delete from " BUILDIDS "_file_mtime_scanned "
4145 [ + - + - : 140 : "where file = ? and mtime = ?");
+ - + - -
- ]
4146 : :
4147 [ + + ]: 94 : while (! stale_fileid_mtime.empty())
4148 : : {
4149 : 24 : auto stale = stale_fileid_mtime.front();
4150 : 24 : stale_fileid_mtime.pop_front();
4151 [ + - + - : 48 : set_metric("thread_work_pending","role","groom", stale_fileid_mtime.size());
+ - + - -
+ - + - -
- - ]
4152 : :
4153 : : // PR28514: limit grooming iteration to O(rescan time), to avoid
4154 : : // slow nuke_* queries over many files locking out rescans for too
4155 : : // long. We iterate over the files in random() sequence to avoid
4156 : : // partial checks going over the same set.
4157 [ - + - - ]: 24 : if (rescan_s > 0 && (long)time(NULL) > (long)(time_start + rescan_s))
4158 : : {
4159 [ # # # # : 0 : inc_metric("groomed_total", "action", "aborted");
# # # # #
# # # # #
# # ]
4160 : 0 : break;
4161 : : }
4162 : :
4163 [ + - ]: 24 : if (interrupted) break;
4164 : :
4165 : 24 : int64_t fileid = stale.first;
4166 : 24 : int64_t mtime = stale.second;
4167 [ + - + - : 24 : files_del_f_de.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
+ - + - ]
4168 [ + - + - : 24 : files_del_r_de.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
+ - + - ]
4169 [ + - + - : 24 : files_del_scan.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
+ - + - ]
4170 [ + - + - : 48 : inc_metric("groomed_total", "action", "cleaned");
+ - + - -
+ - + - -
- - ]
4171 : :
4172 [ + - ]: 24 : if (sigusr1 != forced_rescan_count) // stop early if scan triggered
4173 : : break;
4174 : : }
4175 : 70 : stale_fileid_mtime.clear(); // no need for this any longer
4176 [ + - + - : 140 : set_metric("thread_work_pending","role","groom", stale_fileid_mtime.size());
+ - + - -
+ - + - -
- - ]
4177 : :
4178 : : // delete buildids with no references in _r_de or _f_de tables;
4179 : : // cascades to _r_sref & _f_s records
4180 [ + - ]: 70 : sqlite_ps buildids_del (db, "nuke orphan buildids",
4181 : : "delete from " BUILDIDS "_buildids "
4182 : : "where not exists (select 1 from " BUILDIDS "_f_de d where " BUILDIDS "_buildids.id = d.buildid) "
4183 [ + - + - : 140 : "and not exists (select 1 from " BUILDIDS "_r_de d where " BUILDIDS "_buildids.id = d.buildid)");
+ - + - -
- ]
4184 [ + - + - ]: 70 : buildids_del.reset().step_ok_done();
4185 : :
4186 [ - + ]: 70 : if (interrupted) return;
4187 : :
4188 : : // NB: "vacuum" is too heavy for even daily runs: it rewrites the entire db, so is done as maxigroom -G
4189 [ + - + - : 140 : sqlite_ps g1 (db, "incremental vacuum", "pragma incremental_vacuum");
+ - + - -
- ]
4190 [ + - + - ]: 70 : g1.reset().step_ok_done();
4191 [ + - + - : 140 : sqlite_ps g2 (db, "optimize", "pragma optimize");
+ - - + -
- ]
4192 [ + - + - ]: 70 : g2.reset().step_ok_done();
4193 [ + - + - : 140 : sqlite_ps g3 (db, "wal checkpoint", "pragma wal_checkpoint=truncate");
+ - + - -
- ]
4194 [ + - + - ]: 70 : g3.reset().step_ok_done();
4195 : :
4196 [ + - ]: 70 : database_stats_report();
4197 : :
4198 [ + - + - ]: 70 : (void) statfs_free_enough_p(db_path, "database"); // report sqlite filesystem size
4199 : :
4200 [ + - ]: 70 : sqlite3_db_release_memory(db); // shrink the process if possible
4201 [ + - ]: 70 : sqlite3_db_release_memory(dbq); // ... for both connections
4202 [ + - ]: 70 : debuginfod_pool_groom(); // and release any debuginfod_client objects we've been holding onto
4203 : :
4204 [ + - ]: 70 : fdcache.limit(0,0,0,0); // release the fdcache contents
4205 [ + - ]: 70 : fdcache.limit(fdcache_fds, fdcache_mbs, fdcache_prefetch_fds, fdcache_prefetch_mbs); // restore status quo parameters
4206 : :
4207 : 70 : clock_gettime (CLOCK_MONOTONIC, &ts_end);
4208 : 70 : double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
4209 : :
4210 [ + - + - : 140 : obatched(clog) << "groomed database in " << deltas << "s" << endl;
+ - + - ]
4211 : 70 : }
4212 : :
4213 : :
4214 : : static void*
4215 : 70 : thread_main_groom (void* /*arg*/)
4216 : : {
4217 [ + - + - : 140 : set_metric("thread_tid", "role", "groom", tid());
+ - - + -
+ - - -
- ]
4218 [ + - + - : 140 : add_metric("thread_count", "role", "groom", 1);
+ - - + -
+ - - -
- ]
4219 : :
4220 : 70 : time_t last_groom = 0;
4221 : :
4222 : 508 : while (1)
4223 : : {
4224 : 289 : sleep (1);
4225 : 289 : scanq.wait_idle(); // PR25394: block scanners during grooming!
4226 [ + + ]: 289 : if (interrupted) break;
4227 : :
4228 : 219 : time_t now = time(NULL);
4229 : 219 : bool groom_now = false;
4230 [ + + ]: 219 : if (last_groom == 0) // at least one initial groom is documented even for -g0
4231 : 64 : groom_now = true;
4232 [ + + + + ]: 219 : if (groom_s > 0 && (long)now > (long)(last_groom + groom_s))
4233 : 219 : groom_now = true;
4234 [ + + ]: 219 : if (sigusr2 != forced_groom_count)
4235 : : {
4236 : 6 : forced_groom_count = sigusr2;
4237 : 6 : groom_now = true;
4238 : : }
4239 [ + + ]: 219 : if (groom_now)
4240 : : {
4241 [ + - + - : 140 : set_metric("thread_busy", "role", "groom", 1);
+ - - + -
+ - - -
- ]
4242 : 70 : try
4243 : : {
4244 [ + - ]: 70 : groom ();
4245 : : }
4246 [ - - ]: 0 : catch (const sqlite_exception& e)
4247 : : {
4248 [ - - - - : 0 : obatched(cerr) << e.message << endl;
- - ]
4249 : 0 : }
4250 : 70 : last_groom = time(NULL); // NB: now was before grooming
4251 : : // finished a grooming loop
4252 [ + - + - : 140 : inc_metric("thread_work_total", "role", "groom");
+ - - + -
+ - - -
- ]
4253 [ + - + - : 140 : set_metric("thread_busy", "role", "groom", 0);
+ - - + -
+ - - -
- ]
4254 : : }
4255 : :
4256 : 219 : scanq.done_idle();
4257 : 219 : }
4258 : :
4259 : 70 : return 0;
4260 : : }
4261 : :
4262 : :
4263 : : ////////////////////////////////////////////////////////////////////////
4264 : :
4265 : :
4266 : : static void
4267 : 72 : signal_handler (int /* sig */)
4268 : : {
4269 : 72 : interrupted ++;
4270 : :
4271 [ + + ]: 72 : if (db)
4272 : 70 : sqlite3_interrupt (db);
4273 [ + - ]: 72 : if (dbq)
4274 : 72 : sqlite3_interrupt (dbq);
4275 : :
4276 : : // NB: don't do anything else in here
4277 : 72 : }
4278 : :
4279 : : static void
4280 : 60 : sigusr1_handler (int /* sig */)
4281 : : {
4282 : 60 : sigusr1 ++;
4283 : : // NB: don't do anything else in here
4284 : 60 : }
4285 : :
4286 : : static void
4287 : 6 : sigusr2_handler (int /* sig */)
4288 : : {
4289 : 6 : sigusr2 ++;
4290 : : // NB: don't do anything else in here
4291 : 6 : }
4292 : :
4293 : :
4294 : : static void // error logging callback from libmicrohttpd internals
4295 : 0 : error_cb (void *arg, const char *fmt, va_list ap)
4296 : : {
4297 : 0 : (void) arg;
4298 [ # # # # : 0 : inc_metric("error_count","libmicrohttpd",fmt);
# # # # #
# # # #
# ]
4299 : 0 : char errmsg[512];
4300 : 0 : (void) vsnprintf (errmsg, sizeof(errmsg), fmt, ap); // ok if slightly truncated
4301 [ # # ]: 0 : obatched(cerr) << "libmicrohttpd error: " << errmsg; // MHD_DLOG calls already include \n
4302 : 0 : }
4303 : :
4304 : :
4305 : : // A user-defined sqlite function, to score the sharedness of the
4306 : : // prefix of two strings. This is used to compare candidate debuginfo
4307 : : // / source-rpm names, so that the closest match
4308 : : // (directory-topology-wise closest) is found. This is important in
4309 : : // case the same sref (source file name) is in many -debuginfo or
4310 : : // -debugsource RPMs, such as when multiple versions/releases of the
4311 : : // same package are in the database.
4312 : :
4313 : 296 : static void sqlite3_sharedprefix_fn (sqlite3_context* c, int argc, sqlite3_value** argv)
4314 : : {
4315 [ - + ]: 296 : if (argc != 2)
4316 : 0 : sqlite3_result_error(c, "expect 2 string arguments", -1);
4317 [ + - + + ]: 592 : else if ((sqlite3_value_type(argv[0]) != SQLITE_TEXT) ||
4318 : 296 : (sqlite3_value_type(argv[1]) != SQLITE_TEXT))
4319 : 6 : sqlite3_result_null(c);
4320 : : else
4321 : : {
4322 : 290 : const unsigned char* a = sqlite3_value_text (argv[0]);
4323 : 290 : const unsigned char* b = sqlite3_value_text (argv[1]);
4324 : 290 : int i = 0;
4325 [ + + + - : 35878 : while (*a != '\0' && *b != '\0' && *a++ == *b++)
+ + + + ]
4326 : 35048 : i++;
4327 : 290 : sqlite3_result_int (c, i);
4328 : : }
4329 : 296 : }
4330 : :
4331 : :
4332 : : static unsigned
4333 : 140 : default_concurrency() // guaranteed >= 1
4334 : : {
4335 : : // Prior to PR29975 & PR29976, we'd just use this:
4336 : 140 : unsigned sth = std::thread::hardware_concurrency();
4337 : : // ... but on many-CPU boxes, admins or distros may throttle
4338 : : // resources in such a way that debuginfod would mysteriously fail.
4339 : : // So we reduce the defaults:
4340 : :
4341 : 140 : unsigned aff = 0;
4342 : : #ifdef HAVE_SCHED_GETAFFINITY
4343 : 140 : {
4344 : 140 : int ret;
4345 : 140 : cpu_set_t mask;
4346 : 140 : CPU_ZERO(&mask);
4347 : 140 : ret = sched_getaffinity(0, sizeof(mask), &mask);
4348 [ + - ]: 140 : if (ret == 0)
4349 : 140 : aff = CPU_COUNT(&mask);
4350 : : }
4351 : : #endif
4352 : :
4353 : 140 : unsigned fn = 0;
4354 : : #ifdef HAVE_GETRLIMIT
4355 : 140 : {
4356 : 140 : struct rlimit rlim;
4357 : 140 : int rc = getrlimit(RLIMIT_NOFILE, &rlim);
4358 [ + - ]: 140 : if (rc == 0)
4359 [ + - ]: 280 : fn = max((rlim_t)1, (rlim.rlim_cur - 100) / 4);
4360 : : // at least 2 fds are used by each listener thread etc.
4361 : : // plus a bunch to account for shared libraries and such
4362 : : }
4363 : : #endif
4364 : :
4365 [ - + - + : 140 : unsigned d = min(max(sth, 1U),
- + ]
4366 [ - + ]: 140 : min(max(aff, 1U),
4367 [ - + ]: 140 : max(fn, 1U)));
4368 : 140 : return d;
4369 : : }
4370 : :
4371 : :
4372 : : // 30879: Something to help out in case of an uncaught exception.
4373 : 0 : void my_terminate_handler()
4374 : : {
4375 : : #if defined(__GLIBC__)
4376 : 0 : void *array[40];
4377 : 0 : int size = backtrace (array, 40);
4378 : 0 : backtrace_symbols_fd (array, size, STDERR_FILENO);
4379 : : #endif
4380 : : #if defined(__GLIBCXX__) || defined(__GLIBCPP__)
4381 : 0 : __gnu_cxx::__verbose_terminate_handler();
4382 : : #endif
4383 : 0 : abort();
4384 : : }
4385 : :
4386 : :
4387 : : int
4388 : 72 : main (int argc, char *argv[])
4389 : : {
4390 : 72 : (void) setlocale (LC_ALL, "");
4391 : 72 : (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
4392 : 72 : (void) textdomain (PACKAGE_TARNAME);
4393 : :
4394 : 72 : std::set_terminate(& my_terminate_handler);
4395 : :
4396 : : /* Tell the library which version we are expecting. */
4397 : 72 : elf_version (EV_CURRENT);
4398 : :
4399 [ + - - + ]: 144 : tmpdir = string(getenv("TMPDIR") ?: "/tmp");
4400 : :
4401 : : /* Set computed default values. */
4402 [ - + + - : 72 : db_path = string(getenv("HOME") ?: "/") + string("/.debuginfod.sqlite"); /* XDG? */
+ - - + -
+ + - -
- ]
4403 : 72 : int rc = regcomp (& file_include_regex, ".*", REG_EXTENDED|REG_NOSUB); // match everything
4404 [ - + ]: 72 : if (rc != 0)
4405 : 0 : error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
4406 : 72 : rc = regcomp (& file_exclude_regex, "^$", REG_EXTENDED|REG_NOSUB); // match nothing
4407 [ - + ]: 72 : if (rc != 0)
4408 : 0 : error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
4409 : :
4410 : : // default parameters for fdcache are computed from system stats
4411 : 72 : struct statfs sfs;
4412 : 72 : rc = statfs(tmpdir.c_str(), &sfs);
4413 [ - + ]: 72 : if (rc < 0)
4414 : 0 : fdcache_mbs = 1024; // 1 gigabyte
4415 : : else
4416 : 72 : fdcache_mbs = sfs.f_bavail * sfs.f_bsize / 1024 / 1024 / 4; // 25% of free space
4417 : 72 : fdcache_mintmp = 25; // emergency flush at 25% remaining (75% full)
4418 : 72 : fdcache_prefetch = 64; // guesstimate storage is this much less costly than re-decompression
4419 : 72 : fdcache_fds = (concurrency + fdcache_prefetch) * 2;
4420 : :
4421 : : /* Parse and process arguments. */
4422 : 72 : int remaining;
4423 : 72 : (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
4424 [ - + ]: 72 : if (remaining != argc)
4425 : 0 : error (EXIT_FAILURE, 0,
4426 : 0 : "unexpected argument: %s", argv[remaining]);
4427 : :
4428 : : // Make the prefetch cache spaces a fraction of the main fdcache if
4429 : : // unspecified.
4430 [ + + ]: 72 : if (fdcache_prefetch_fds == 0)
4431 : 70 : fdcache_prefetch_fds = fdcache_fds / 2;
4432 [ + + ]: 72 : if (fdcache_prefetch_mbs == 0)
4433 : 70 : fdcache_prefetch_mbs = fdcache_mbs / 2;
4434 : :
4435 [ + + + + : 72 : if (scan_archives.size()==0 && !scan_files && source_paths.size()>0)
- + ]
4436 [ # # ]: 0 : obatched(clog) << "warning: without -F -R -U -Z, ignoring PATHs" << endl;
4437 : :
4438 : 72 : fdcache.limit(fdcache_fds, fdcache_mbs, fdcache_prefetch_fds, fdcache_prefetch_mbs);
4439 : :
4440 : 72 : (void) signal (SIGPIPE, SIG_IGN); // microhttpd can generate it incidentally, ignore
4441 : 72 : (void) signal (SIGINT, signal_handler); // ^C
4442 : 72 : (void) signal (SIGHUP, signal_handler); // EOF
4443 : 72 : (void) signal (SIGTERM, signal_handler); // systemd
4444 : 72 : (void) signal (SIGUSR1, sigusr1_handler); // end-user
4445 : 72 : (void) signal (SIGUSR2, sigusr2_handler); // end-user
4446 : :
4447 : : /* Get database ready. */
4448 [ + + ]: 72 : if (! passive_p)
4449 : : {
4450 : 70 : rc = sqlite3_open_v2 (db_path.c_str(), &db, (SQLITE_OPEN_READWRITE
4451 : : |SQLITE_OPEN_URI
4452 : : |SQLITE_OPEN_PRIVATECACHE
4453 : : |SQLITE_OPEN_CREATE
4454 : : |SQLITE_OPEN_FULLMUTEX), /* thread-safe */
4455 : : NULL);
4456 [ - + ]: 70 : if (rc == SQLITE_CORRUPT)
4457 : : {
4458 : 0 : (void) unlink (db_path.c_str());
4459 : 0 : error (EXIT_FAILURE, 0,
4460 : : "cannot open %s, deleted database: %s", db_path.c_str(), sqlite3_errmsg(db));
4461 : : }
4462 [ - + ]: 70 : else if (rc)
4463 : : {
4464 : 0 : error (EXIT_FAILURE, 0,
4465 : : "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(db));
4466 : : }
4467 : : }
4468 : :
4469 : : // open the readonly query variant
4470 : : // NB: PRIVATECACHE allows web queries to operate in parallel with
4471 : : // much other grooming/scanning operation.
4472 : 72 : rc = sqlite3_open_v2 (db_path.c_str(), &dbq, (SQLITE_OPEN_READONLY
4473 : : |SQLITE_OPEN_URI
4474 : : |SQLITE_OPEN_PRIVATECACHE
4475 : : |SQLITE_OPEN_FULLMUTEX), /* thread-safe */
4476 : : NULL);
4477 [ - + ]: 72 : if (rc)
4478 : : {
4479 : 0 : error (EXIT_FAILURE, 0,
4480 : : "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(dbq));
4481 : : }
4482 : :
4483 : :
4484 [ + - ]: 144 : obatched(clog) << "opened database " << db_path
4485 [ + + + - : 74 : << (db?" rw":"") << (dbq?" ro":"") << endl;
- + + - +
- ]
4486 [ + - + - ]: 144 : obatched(clog) << "sqlite version " << sqlite3_version << endl;
4487 [ + + + - : 214 : obatched(clog) << "service mode " << (passive_p ? "passive":"active") << endl;
+ - ]
4488 : :
4489 : : // add special string-prefix-similarity function used in rpm sref/sdef resolution
4490 : 72 : rc = sqlite3_create_function(dbq, "sharedprefix", 2, SQLITE_UTF8, NULL,
4491 : : & sqlite3_sharedprefix_fn, NULL, NULL);
4492 [ - + ]: 72 : if (rc != SQLITE_OK)
4493 : 0 : error (EXIT_FAILURE, 0,
4494 : : "cannot create sharedprefix function: %s", sqlite3_errmsg(dbq));
4495 : :
4496 [ + + ]: 72 : if (! passive_p)
4497 : : {
4498 [ + + ]: 70 : if (verbose > 3)
4499 [ + - + - ]: 88 : obatched(clog) << "ddl: " << DEBUGINFOD_SQLITE_DDL << endl;
4500 : 70 : rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_DDL, NULL, NULL, NULL);
4501 [ - + ]: 70 : if (rc != SQLITE_OK)
4502 : : {
4503 : 0 : error (EXIT_FAILURE, 0,
4504 : : "cannot run database schema ddl: %s", sqlite3_errmsg(db));
4505 : : }
4506 : : }
4507 : :
4508 [ + - + - : 144 : obatched(clog) << "libmicrohttpd version " << MHD_get_version() << endl;
+ - ]
4509 : :
4510 : : /* If '-C' wasn't given or was given with no arg, pick a reasonable default
4511 : : for the number of worker threads. */
4512 [ + + ]: 72 : if (connection_pool == 0)
4513 : 68 : connection_pool = default_concurrency();
4514 : :
4515 : : /* Note that MHD_USE_EPOLL and MHD_USE_THREAD_PER_CONNECTION don't
4516 : : work together. */
4517 : 72 : unsigned int use_epoll = 0;
4518 : : #if MHD_VERSION >= 0x00095100
4519 : 72 : use_epoll = MHD_USE_EPOLL;
4520 : : #endif
4521 : :
4522 : 72 : unsigned int mhd_flags = (
4523 : : #if MHD_VERSION >= 0x00095300
4524 : : MHD_USE_INTERNAL_POLLING_THREAD
4525 : : #else
4526 : : MHD_USE_SELECT_INTERNALLY
4527 : : #endif
4528 : : | MHD_USE_DUAL_STACK
4529 : : | use_epoll
4530 : : #if MHD_VERSION >= 0x00095200
4531 : : | MHD_USE_ITC
4532 : : #endif
4533 : : | MHD_USE_DEBUG); /* report errors to stderr */
4534 : :
4535 : : // Start httpd server threads. Use a single dual-homed pool.
4536 : 72 : MHD_Daemon *d46 = MHD_start_daemon (mhd_flags, http_port,
4537 : : NULL, NULL, /* default accept policy */
4538 : : handler_cb, NULL, /* handler callback */
4539 : : MHD_OPTION_EXTERNAL_LOGGER,
4540 : : error_cb, NULL,
4541 : : MHD_OPTION_THREAD_POOL_SIZE,
4542 : : (int)connection_pool,
4543 : : MHD_OPTION_END);
4544 : :
4545 : 72 : MHD_Daemon *d4 = NULL;
4546 [ - + ]: 72 : if (d46 == NULL)
4547 : : {
4548 : : // Cannot use dual_stack, use ipv4 only
4549 : 0 : mhd_flags &= ~(MHD_USE_DUAL_STACK);
4550 [ # # ]: 0 : d4 = MHD_start_daemon (mhd_flags, http_port,
4551 : : NULL, NULL, /* default accept policy */
4552 : : handler_cb, NULL, /* handler callback */
4553 : : MHD_OPTION_EXTERNAL_LOGGER,
4554 : : error_cb, NULL,
4555 : : (connection_pool
4556 : : ? MHD_OPTION_THREAD_POOL_SIZE
4557 : : : MHD_OPTION_END),
4558 : : (connection_pool
4559 : : ? (int)connection_pool
4560 : : : MHD_OPTION_END),
4561 : : MHD_OPTION_END);
4562 [ # # ]: 0 : if (d4 == NULL)
4563 : : {
4564 : 0 : sqlite3 *database = db;
4565 : 0 : sqlite3 *databaseq = dbq;
4566 : 0 : db = dbq = 0; // for signal_handler not to freak
4567 : 0 : sqlite3_close (databaseq);
4568 : 0 : sqlite3_close (database);
4569 : 0 : error (EXIT_FAILURE, 0, "cannot start http server at port %d",
4570 : : http_port);
4571 : : }
4572 : :
4573 : : }
4574 : 72 : obatched(clog) << "started http server on"
4575 : : << (d4 != NULL ? " IPv4 " : " IPv4 IPv6 ")
4576 [ + - + - : 144 : << "port=" << http_port << endl;
+ - + - +
- ]
4577 : :
4578 : : // add maxigroom sql if -G given
4579 [ - + ]: 72 : if (maxigroom)
4580 : : {
4581 [ # # ]: 0 : obatched(clog) << "maxigrooming database, please wait." << endl;
4582 [ # # ]: 0 : extra_ddl.push_back("create index if not exists " BUILDIDS "_r_sref_arc on " BUILDIDS "_r_sref(artifactsrc);");
4583 [ # # ]: 0 : extra_ddl.push_back("delete from " BUILDIDS "_r_sdef where not exists (select 1 from " BUILDIDS "_r_sref b where " BUILDIDS "_r_sdef.content = b.artifactsrc);");
4584 [ # # ]: 0 : extra_ddl.push_back("drop index if exists " BUILDIDS "_r_sref_arc;");
4585 : :
4586 : : // NB: we don't maxigroom the _files interning table. It'd require a temp index on all the
4587 : : // tables that have file foreign-keys, which is a lot.
4588 : :
4589 : : // NB: with =delete, may take up 3x disk space total during vacuum process
4590 : : // vs. =off (only 2x but may corrupt database if program dies mid-vacuum)
4591 : : // vs. =wal (>3x observed, but safe)
4592 [ # # ]: 0 : extra_ddl.push_back("pragma journal_mode=delete;");
4593 [ # # ]: 0 : extra_ddl.push_back("vacuum;");
4594 [ # # ]: 0 : extra_ddl.push_back("pragma journal_mode=wal;");
4595 : : }
4596 : :
4597 : : // run extra -D sql if given
4598 [ + + ]: 72 : if (! passive_p)
4599 [ - + ]: 70 : for (auto&& i: extra_ddl)
4600 : : {
4601 [ # # ]: 0 : if (verbose > 1)
4602 [ # # # # ]: 0 : obatched(clog) << "extra ddl:\n" << i << endl;
4603 : 0 : rc = sqlite3_exec (db, i.c_str(), NULL, NULL, NULL);
4604 [ # # # # ]: 0 : if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW)
4605 : 0 : error (0, 0,
4606 : : "warning: cannot run database extra ddl %s: %s", i.c_str(), sqlite3_errmsg(db));
4607 : :
4608 [ # # ]: 0 : if (maxigroom)
4609 [ # # ]: 0 : obatched(clog) << "maxigroomed database" << endl;
4610 : : }
4611 : :
4612 [ + + ]: 72 : if (! passive_p)
4613 [ + - + - ]: 140 : obatched(clog) << "search concurrency " << concurrency << endl;
4614 : 72 : obatched(clog) << "webapi connection pool " << connection_pool
4615 [ + - - + : 72 : << (connection_pool ? "" : " (unlimited)") << endl;
+ - + - ]
4616 [ + + ]: 72 : if (! passive_p) {
4617 [ + - + - ]: 140 : obatched(clog) << "rescan time " << rescan_s << endl;
4618 [ + - + - ]: 140 : obatched(clog) << "scan checkpoint " << scan_checkpoint << endl;
4619 : : }
4620 [ + - + - ]: 144 : obatched(clog) << "fdcache fds " << fdcache_fds << endl;
4621 [ + - + - ]: 144 : obatched(clog) << "fdcache mbs " << fdcache_mbs << endl;
4622 [ + - + - ]: 144 : obatched(clog) << "fdcache prefetch " << fdcache_prefetch << endl;
4623 [ + - + - ]: 144 : obatched(clog) << "fdcache tmpdir " << tmpdir << endl;
4624 [ + - + - ]: 144 : obatched(clog) << "fdcache tmpdir min% " << fdcache_mintmp << endl;
4625 [ + + ]: 72 : if (! passive_p)
4626 [ + - + - ]: 140 : obatched(clog) << "groom time " << groom_s << endl;
4627 [ + - + - ]: 144 : obatched(clog) << "prefetch fds " << fdcache_prefetch_fds << endl;
4628 [ + - + - ]: 144 : obatched(clog) << "prefetch mbs " << fdcache_prefetch_mbs << endl;
4629 [ + - + - ]: 144 : obatched(clog) << "forwarded ttl limit " << forwarded_ttl_limit << endl;
4630 : :
4631 [ + + ]: 72 : if (scan_archives.size()>0)
4632 : : {
4633 : 50 : obatched ob(clog);
4634 [ + - ]: 50 : auto& o = ob << "accepting archive types ";
4635 [ + + ]: 152 : for (auto&& arch : scan_archives)
4636 [ + - + - : 102 : o << arch.first << "(" << arch.second << ") ";
+ - + - ]
4637 [ + - ]: 50 : o << endl;
4638 : 50 : }
4639 : 72 : const char* du = getenv(DEBUGINFOD_URLS_ENV_VAR);
4640 [ + + + + ]: 72 : if (du && du[0] != '\0') // set to non-empty string?
4641 [ + - + - ]: 28 : obatched(clog) << "upstream debuginfod servers: " << du << endl;
4642 : :
4643 [ + + ]: 72 : vector<pthread_t> all_threads;
4644 : :
4645 [ + + ]: 72 : if (! passive_p)
4646 : : {
4647 : 70 : pthread_t pt;
4648 : 70 : rc = pthread_create (& pt, NULL, thread_main_groom, NULL);
4649 [ - + ]: 70 : if (rc)
4650 : 0 : error (EXIT_FAILURE, rc, "cannot spawn thread to groom database\n");
4651 : : else
4652 : : {
4653 : : #ifdef HAVE_PTHREAD_SETNAME_NP
4654 : 70 : (void) pthread_setname_np (pt, "groom");
4655 : : #endif
4656 [ + - ]: 70 : all_threads.push_back(pt);
4657 : : }
4658 : :
4659 [ + + + + ]: 70 : if (scan_files || scan_archives.size() > 0)
4660 : : {
4661 [ + - ]: 64 : if (scan_checkpoint > 0)
4662 [ + - ]: 64 : scan_barrier = new sqlite_checkpoint_pb(concurrency, (unsigned) scan_checkpoint);
4663 : :
4664 : 64 : rc = pthread_create (& pt, NULL, thread_main_fts_source_paths, NULL);
4665 [ - + ]: 64 : if (rc)
4666 : 0 : error (EXIT_FAILURE, rc, "cannot spawn thread to traverse source paths\n");
4667 : : #ifdef HAVE_PTHREAD_SETNAME_NP
4668 : 64 : (void) pthread_setname_np (pt, "traverse");
4669 : : #endif
4670 [ + - ]: 64 : all_threads.push_back(pt);
4671 : :
4672 [ + + ]: 320 : for (unsigned i=0; i<concurrency; i++)
4673 : : {
4674 : 256 : rc = pthread_create (& pt, NULL, thread_main_scanner, NULL);
4675 [ - + ]: 256 : if (rc)
4676 : 0 : error (EXIT_FAILURE, rc, "cannot spawn thread to scan source files / archives\n");
4677 : : #ifdef HAVE_PTHREAD_SETNAME_NP
4678 : 256 : (void) pthread_setname_np (pt, "scan");
4679 : : #endif
4680 [ + - ]: 256 : all_threads.push_back(pt);
4681 : : }
4682 : : }
4683 : : }
4684 : :
4685 : : /* Trivial main loop! */
4686 [ + - + - ]: 72 : set_metric("ready", 1);
4687 [ + + ]: 210 : while (! interrupted)
4688 [ + - ]: 138 : pause ();
4689 [ + - ]: 72 : scanq.nuke(); // wake up any remaining scanq-related threads, let them die
4690 [ + + + - ]: 72 : if (scan_barrier) scan_barrier->nuke(); // ... in case they're stuck in a barrier
4691 [ + - + - ]: 72 : set_metric("ready", 0);
4692 : :
4693 [ + - ]: 72 : if (verbose)
4694 [ + - + - : 144 : obatched(clog) << "stopping" << endl;
- - ]
4695 : :
4696 : : /* Join all our threads. */
4697 [ + + ]: 462 : for (auto&& it : all_threads)
4698 [ + - ]: 390 : pthread_join (it, NULL);
4699 : :
4700 : : /* Stop all the web service threads. */
4701 [ + - + - ]: 72 : if (d46) MHD_stop_daemon (d46);
4702 [ - + - - ]: 72 : if (d4) MHD_stop_daemon (d4);
4703 : :
4704 [ + + ]: 72 : if (! passive_p)
4705 : : {
4706 : : /* With all threads known dead, we can clean up the global resources. */
4707 [ + - ]: 70 : rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_CLEANUP_DDL, NULL, NULL, NULL);
4708 [ - + ]: 70 : if (rc != SQLITE_OK)
4709 : : {
4710 [ # # # # ]: 0 : error (0, 0,
4711 : : "warning: cannot run database cleanup ddl: %s", sqlite3_errmsg(db));
4712 : : }
4713 : : }
4714 : :
4715 [ + - ]: 72 : debuginfod_pool_groom ();
4716 [ + + ]: 72 : delete scan_barrier;
4717 : :
4718 : : // NB: no problem with unconditional free here - an earlier failed regcomp would exit program
4719 [ + - ]: 72 : (void) regfree (& file_include_regex);
4720 [ + - ]: 72 : (void) regfree (& file_exclude_regex);
4721 : :
4722 : 72 : sqlite3 *database = db;
4723 : 72 : sqlite3 *databaseq = dbq;
4724 : 72 : db = dbq = 0; // for signal_handler not to freak
4725 [ + - ]: 72 : (void) sqlite3_close (databaseq);
4726 [ + + ]: 72 : if (! passive_p)
4727 [ + - ]: 70 : (void) sqlite3_close (database);
4728 : :
4729 [ + + ]: 72 : return 0;
4730 : 72 : }
|