1 /* Debuginfo-over-http server.
2    Copyright (C) 2019-2021 Red Hat, Inc.
3    This file is part of elfutils.
4 
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 
19 /* cargo-cult from libdwfl linux-kernel-modules.c */
20 /* In case we have a bad fts we include this before config.h because it
21    can't handle _FILE_OFFSET_BITS.
22    Everything we need here is fine if its declarations just come first.
23    Also, include sys/types.h before fts. On some systems fts.h is not self
24    contained. */
25 #ifdef BAD_FTS
26   #include <sys/types.h>
27   #include <fts.h>
28 #endif
29 
30 #ifdef HAVE_CONFIG_H
31   #include "config.h"
32 #endif
33 
34 extern "C" {
35 #include "printversion.h"
36 }
37 
38 #include "debuginfod.h"
39 #include <dwarf.h>
40 
41 #include <argp.h>
42 #ifdef __GNUC__
43 #undef __attribute__ /* glibc bug - rhbz 1763325 */
44 #endif
45 
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <error.h>
49 #include <libintl.h>
50 #include <locale.h>
51 #include <pthread.h>
52 #include <signal.h>
53 #include <sys/stat.h>
54 #include <sys/time.h>
55 #include <sys/vfs.h>
56 #include <unistd.h>
57 #include <fcntl.h>
58 #include <netdb.h>
59 
60 
61 /* If fts.h is included before config.h, its indirect inclusions may not
62    give us the right LFS aliases of these functions, so map them manually.  */
63 #ifdef BAD_FTS
64   #ifdef _FILE_OFFSET_BITS
65     #define open open64
66     #define fopen fopen64
67   #endif
68 #else
69   #include <sys/types.h>
70   #include <fts.h>
71 #endif
72 
73 #include <cstring>
74 #include <vector>
75 #include <set>
76 #include <map>
77 #include <string>
78 #include <iostream>
79 #include <iomanip>
80 #include <ostream>
81 #include <sstream>
82 #include <mutex>
83 #include <deque>
84 #include <condition_variable>
85 #include <thread>
86 // #include <regex> // on rhel7 gcc 4.8, not competent
87 #include <regex.h>
88 // #include <algorithm>
89 using namespace std;
90 
91 #include <gelf.h>
92 #include <libdwelf.h>
93 
94 #include <microhttpd.h>
95 
96 #if MHD_VERSION >= 0x00097002
97 // libmicrohttpd 0.9.71 broke API
98 #define MHD_RESULT enum MHD_Result
99 #else
100 #define MHD_RESULT int
101 #endif
102 
103 #include <curl/curl.h>
104 #include <archive.h>
105 #include <archive_entry.h>
106 #include <sqlite3.h>
107 
108 #ifdef __linux__
109 #include <sys/syscall.h>
110 #endif
111 
112 #ifdef __linux__
113 #define tid() syscall(SYS_gettid)
114 #else
115 #define tid() pthread_self()
116 #endif
117 
118 
119 inline bool
string_endswith(const string & haystack,const string & needle)120 string_endswith(const string& haystack, const string& needle)
121 {
122   return (haystack.size() >= needle.size() &&
123 	  equal(haystack.end()-needle.size(), haystack.end(),
124                 needle.begin()));
125 }
126 
127 
128 // Roll this identifier for every sqlite schema incompatibility.
129 #define BUILDIDS "buildids9"
130 
131 #if SQLITE_VERSION_NUMBER >= 3008000
132 #define WITHOUT_ROWID "without rowid"
133 #else
134 #define WITHOUT_ROWID ""
135 #endif
136 
137 static const char DEBUGINFOD_SQLITE_DDL[] =
138   "pragma foreign_keys = on;\n"
139   "pragma synchronous = 0;\n" // disable fsync()s - this cache is disposable across a machine crash
140   "pragma journal_mode = wal;\n" // https://sqlite.org/wal.html
141   "pragma wal_checkpoint = truncate;\n" // clean out any preexisting wal file
142   "pragma journal_size_limit = 0;\n" // limit steady state file (between grooming, which also =truncate's)
143   "pragma auto_vacuum = incremental;\n" // https://sqlite.org/pragma.html
144   "pragma busy_timeout = 1000;\n" // https://sqlite.org/pragma.html
145   // NB: all these are overridable with -D option
146 
147   // Normalization table for interning file names
148   "create table if not exists " BUILDIDS "_files (\n"
149   "        id integer primary key not null,\n"
150   "        name text unique not null\n"
151   "        );\n"
152   // Normalization table for interning buildids
153   "create table if not exists " BUILDIDS "_buildids (\n"
154   "        id integer primary key not null,\n"
155   "        hex text unique not null);\n"
156   // Track the completion of scanning of a given file & sourcetype at given time
157   "create table if not exists " BUILDIDS "_file_mtime_scanned (\n"
158   "        mtime integer not null,\n"
159   "        file integer not null,\n"
160   "        size integer not null,\n" // in bytes
161   "        sourcetype text(1) not null\n"
162   "            check (sourcetype IN ('F', 'R')),\n"
163   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
164   "        primary key (file, mtime, sourcetype)\n"
165   "        ) " WITHOUT_ROWID ";\n"
166   "create table if not exists " BUILDIDS "_f_de (\n"
167   "        buildid integer not null,\n"
168   "        debuginfo_p integer not null,\n"
169   "        executable_p integer not null,\n"
170   "        file integer not null,\n"
171   "        mtime integer not null,\n"
172   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
173   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
174   "        primary key (buildid, file, mtime)\n"
175   "        ) " WITHOUT_ROWID ";\n"
176   "create table if not exists " BUILDIDS "_f_s (\n"
177   "        buildid integer not null,\n"
178   "        artifactsrc integer not null,\n"
179   "        file integer not null,\n" // NB: not necessarily entered into _mtime_scanned
180   "        mtime integer not null,\n"
181   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
182   "        foreign key (artifactsrc) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
183   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
184   "        primary key (buildid, artifactsrc, file, mtime)\n"
185   "        ) " WITHOUT_ROWID ";\n"
186   "create table if not exists " BUILDIDS "_r_de (\n"
187   "        buildid integer not null,\n"
188   "        debuginfo_p integer not null,\n"
189   "        executable_p integer not null,\n"
190   "        file integer not null,\n"
191   "        mtime integer not null,\n"
192   "        content integer not null,\n"
193   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
194   "        foreign key (content) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
195   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
196   "        primary key (buildid, debuginfo_p, executable_p, file, content, mtime)\n"
197   "        ) " WITHOUT_ROWID ";\n"
198   "create table if not exists " BUILDIDS "_r_sref (\n" // outgoing dwarf sourcefile references from rpm
199   "        buildid integer not null,\n"
200   "        artifactsrc integer not null,\n"
201   "        foreign key (artifactsrc) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
202   "        foreign key (buildid) references " BUILDIDS "_buildids(id) on update cascade on delete cascade,\n"
203   "        primary key (buildid, artifactsrc)\n"
204   "        ) " WITHOUT_ROWID ";\n"
205   "create table if not exists " BUILDIDS "_r_sdef (\n" // rpm contents that may satisfy sref
206   "        file integer not null,\n"
207   "        mtime integer not null,\n"
208   "        content integer not null,\n"
209   "        foreign key (file) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
210   "        foreign key (content) references " BUILDIDS "_files(id) on update cascade on delete cascade,\n"
211   "        primary key (content, file, mtime)\n"
212   "        ) " WITHOUT_ROWID ";\n"
213   // create views to glue together some of the above tables, for webapi D queries
214   "create view if not exists " BUILDIDS "_query_d as \n"
215   "select\n"
216   "        b.hex as buildid, n.mtime, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1\n"
217   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_f_de n\n"
218   "        where b.id = n.buildid and f0.id = n.file and n.debuginfo_p = 1\n"
219   "union all select\n"
220   "        b.hex as buildid, n.mtime, 'R' as sourcetype, f0.name as source0, n.mtime as mtime, f1.name as source1\n"
221   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files f1, " BUILDIDS "_r_de n\n"
222   "        where b.id = n.buildid and f0.id = n.file and f1.id = n.content and n.debuginfo_p = 1\n"
223   ";"
224   // ... and for E queries
225   "create view if not exists " BUILDIDS "_query_e as \n"
226   "select\n"
227   "        b.hex as buildid, n.mtime, 'F' as sourcetype, f0.name as source0, n.mtime as mtime, null as source1\n"
228   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_f_de n\n"
229   "        where b.id = n.buildid and f0.id = n.file and n.executable_p = 1\n"
230   "union all select\n"
231   "        b.hex as buildid, n.mtime, 'R' as sourcetype, f0.name as source0, n.mtime as mtime, f1.name as source1\n"
232   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files f1, " BUILDIDS "_r_de n\n"
233   "        where b.id = n.buildid and f0.id = n.file and f1.id = n.content and n.executable_p = 1\n"
234   ";"
235   // ... and for S queries
236   "create view if not exists " BUILDIDS "_query_s as \n"
237   "select\n"
238   "        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"
239   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files fs, " BUILDIDS "_f_s n\n"
240   "        where b.id = n.buildid and f0.id = n.file and fs.id = n.artifactsrc\n"
241   "union all select\n"
242   "        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"
243   "        from " BUILDIDS "_buildids b, " BUILDIDS "_files f0, " BUILDIDS "_files f1, " BUILDIDS "_files fsref, "
244   "        " BUILDIDS "_r_sdef sd, " BUILDIDS "_r_sref sr, " BUILDIDS "_r_de sde\n"
245   "        where b.id = sr.buildid and f0.id = sd.file and fsref.id = sde.file and f1.id = sd.content\n"
246   "        and sr.artifactsrc = sd.content and sde.buildid = sr.buildid\n"
247   ";"
248   // and for startup overview counts
249   "drop view if exists " BUILDIDS "_stats;\n"
250   "create view if not exists " BUILDIDS "_stats as\n"
251   "          select 'file d/e' as label,count(*) as quantity from " BUILDIDS "_f_de\n"
252   "union all select 'file s',count(*) from " BUILDIDS "_f_s\n"
253   "union all select 'archive d/e',count(*) from " BUILDIDS "_r_de\n"
254   "union all select 'archive sref',count(*) from " BUILDIDS "_r_sref\n"
255   "union all select 'archive sdef',count(*) from " BUILDIDS "_r_sdef\n"
256   "union all select 'buildids',count(*) from " BUILDIDS "_buildids\n"
257   "union all select 'filenames',count(*) from " BUILDIDS "_files\n"
258   "union all select 'files scanned (#)',count(*) from " BUILDIDS "_file_mtime_scanned\n"
259   "union all select 'files scanned (mb)',coalesce(sum(size)/1024/1024,0) from " BUILDIDS "_file_mtime_scanned\n"
260 #if SQLITE_VERSION_NUMBER >= 3016000
261   "union all select 'index db size (mb)',page_count*page_size/1024/1024 as size FROM pragma_page_count(), pragma_page_size()\n"
262 #endif
263   ";\n"
264 
265 // schema change history & garbage collection
266 //
267 // XXX: we could have migration queries here to bring prior-schema
268 // data over instead of just dropping it.
269 //
270 // buildids9: widen the mtime_scanned table
271   "" // <<< we are here
272 // buildids8: slim the sref table
273   "drop table if exists buildids8_f_de;\n"
274   "drop table if exists buildids8_f_s;\n"
275   "drop table if exists buildids8_r_de;\n"
276   "drop table if exists buildids8_r_sref;\n"
277   "drop table if exists buildids8_r_sdef;\n"
278   "drop table if exists buildids8_file_mtime_scanned;\n"
279   "drop table if exists buildids8_files;\n"
280   "drop table if exists buildids8_buildids;\n"
281 // buildids7: separate _norm table into dense subtype tables
282   "drop table if exists buildids7_f_de;\n"
283   "drop table if exists buildids7_f_s;\n"
284   "drop table if exists buildids7_r_de;\n"
285   "drop table if exists buildids7_r_sref;\n"
286   "drop table if exists buildids7_r_sdef;\n"
287   "drop table if exists buildids7_file_mtime_scanned;\n"
288   "drop table if exists buildids7_files;\n"
289   "drop table if exists buildids7_buildids;\n"
290 // buildids6: drop bolo/rfolo again, represent sources / rpmcontents in main table
291   "drop table if exists buildids6_norm;\n"
292   "drop table if exists buildids6_files;\n"
293   "drop table if exists buildids6_buildids;\n"
294   "drop view if exists buildids6;\n"
295 // buildids5: redefine srcfile1 column to be '.'-less (for rpms)
296   "drop table if exists buildids5_norm;\n"
297   "drop table if exists buildids5_files;\n"
298   "drop table if exists buildids5_buildids;\n"
299   "drop table if exists buildids5_bolo;\n"
300   "drop table if exists buildids5_rfolo;\n"
301   "drop view if exists buildids5;\n"
302 // buildids4: introduce rpmfile RFOLO
303   "drop table if exists buildids4_norm;\n"
304   "drop table if exists buildids4_files;\n"
305   "drop table if exists buildids4_buildids;\n"
306   "drop table if exists buildids4_bolo;\n"
307   "drop table if exists buildids4_rfolo;\n"
308   "drop view if exists buildids4;\n"
309 // buildids3*: split out srcfile BOLO
310   "drop table if exists buildids3_norm;\n"
311   "drop table if exists buildids3_files;\n"
312   "drop table if exists buildids3_buildids;\n"
313   "drop table if exists buildids3_bolo;\n"
314   "drop view if exists buildids3;\n"
315 // buildids2: normalized buildid and filenames into interning tables;
316   "drop table if exists buildids2_norm;\n"
317   "drop table if exists buildids2_files;\n"
318   "drop table if exists buildids2_buildids;\n"
319   "drop view if exists buildids2;\n"
320   // buildids1: made buildid and artifacttype NULLable, to represent cached-negative
321 //           lookups from sources, e.g. files or rpms that contain no buildid-indexable content
322   "drop table if exists buildids1;\n"
323 // buildids: original
324   "drop table if exists buildids;\n"
325   ;
326 
327 static const char DEBUGINFOD_SQLITE_CLEANUP_DDL[] =
328   "pragma wal_checkpoint = truncate;\n" // clean out any preexisting wal file
329   ;
330 
331 
332 
333 
334 /* Name and version of program.  */
335 /* ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; */ // not this simple for C++
336 
337 /* Bug report address.  */
338 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
339 
340 /* Definitions of arguments for argp functions.  */
341 static const struct argp_option options[] =
342   {
343    { NULL, 0, NULL, 0, "Scanners:", 1 },
344    { "scan-file-dir", 'F', NULL, 0, "Enable ELF/DWARF file scanning.", 0 },
345    { "scan-rpm-dir", 'R', NULL, 0, "Enable RPM scanning.", 0 },
346    { "scan-deb-dir", 'U', NULL, 0, "Enable DEB scanning.", 0 },
347    { "scan-archive", 'Z', "EXT=CMD", 0, "Enable arbitrary archive scanning.", 0 },
348    // "source-oci-imageregistry"  ...
349 
350    { NULL, 0, NULL, 0, "Options:", 2 },
351    { "logical", 'L', NULL, 0, "Follow symlinks, default=ignore.", 0 },
352    { "rescan-time", 't', "SECONDS", 0, "Number of seconds to wait between rescans, 0=disable.", 0 },
353    { "groom-time", 'g', "SECONDS", 0, "Number of seconds to wait between database grooming, 0=disable.", 0 },
354    { "maxigroom", 'G', NULL, 0, "Run a complete database groom/shrink pass at startup.", 0 },
355    { "concurrency", 'c', "NUM", 0, "Limit scanning thread concurrency to NUM.", 0 },
356    { "include", 'I', "REGEX", 0, "Include files matching REGEX, default=all.", 0 },
357    { "exclude", 'X', "REGEX", 0, "Exclude files matching REGEX, default=none.", 0 },
358    { "port", 'p', "NUM", 0, "HTTP port to listen on, default 8002.", 0 },
359    { "database", 'd', "FILE", 0, "Path to sqlite database.", 0 },
360    { "ddl", 'D', "SQL", 0, "Apply extra sqlite ddl/pragma to connection.", 0 },
361    { "verbose", 'v', NULL, 0, "Increase verbosity.", 0 },
362 #define ARGP_KEY_FDCACHE_FDS 0x1001
363    { "fdcache-fds", ARGP_KEY_FDCACHE_FDS, "NUM", 0, "Maximum number of archive files to keep in fdcache.", 0 },
364 #define ARGP_KEY_FDCACHE_MBS 0x1002
365    { "fdcache-mbs", ARGP_KEY_FDCACHE_MBS, "MB", 0, "Maximum total size of archive file fdcache.", 0 },
366 #define ARGP_KEY_FDCACHE_PREFETCH 0x1003
367    { "fdcache-prefetch", ARGP_KEY_FDCACHE_PREFETCH, "NUM", 0, "Number of archive files to prefetch into fdcache.", 0 },
368 #define ARGP_KEY_FDCACHE_MINTMP 0x1004
369    { "fdcache-mintmp", ARGP_KEY_FDCACHE_MINTMP, "NUM", 0, "Minimum free space% on tmpdir.", 0 },
370    { NULL, 0, NULL, 0, NULL, 0 }
371   };
372 
373 /* Short description of program.  */
374 static const char doc[] = "Serve debuginfo-related content across HTTP from files under PATHs.";
375 
376 /* Strings for arguments in help texts.  */
377 static const char args_doc[] = "[PATH ...]";
378 
379 /* Prototype for option handler.  */
380 static error_t parse_opt (int key, char *arg, struct argp_state *state);
381 
382 /* Data structure to communicate with argp functions.  */
383 static struct argp argp =
384   {
385    options, parse_opt, args_doc, doc, NULL, NULL, NULL
386   };
387 
388 
389 static string db_path;
390 static sqlite3 *db;  // single connection, serialized across all our threads!
391 static sqlite3 *dbq; // webapi query-servicing readonly connection, serialized ditto!
392 static unsigned verbose;
393 static volatile sig_atomic_t interrupted = 0;
394 static volatile sig_atomic_t forced_rescan_count = 0;
395 static volatile sig_atomic_t sigusr1 = 0;
396 static volatile sig_atomic_t forced_groom_count = 0;
397 static volatile sig_atomic_t sigusr2 = 0;
398 static unsigned http_port = 8002;
399 static unsigned rescan_s = 300;
400 static unsigned groom_s = 86400;
401 static bool maxigroom = false;
402 static unsigned concurrency = std::thread::hardware_concurrency() ?: 1;
403 static set<string> source_paths;
404 static bool scan_files = false;
405 static map<string,string> scan_archives;
406 static vector<string> extra_ddl;
407 static regex_t file_include_regex;
408 static regex_t file_exclude_regex;
409 static bool traverse_logical;
410 static long fdcache_fds;
411 static long fdcache_mbs;
412 static long fdcache_prefetch;
413 static long fdcache_mintmp;
414 static string tmpdir;
415 
416 static void set_metric(const string& key, double value);
417 // static void inc_metric(const string& key);
418 static void set_metric(const string& metric,
419                        const string& lname, const string& lvalue,
420                        double value);
421 static void inc_metric(const string& metric,
422                        const string& lname, const string& lvalue);
423 static void add_metric(const string& metric,
424                        const string& lname, const string& lvalue,
425                        double value);
426 // static void add_metric(const string& metric, double value);
427 
428 class tmp_inc_metric { // a RAII style wrapper for exception-safe scoped increment & decrement
429   string m, n, v;
430 public:
tmp_inc_metric(const string & mname,const string & lname,const string & lvalue)431   tmp_inc_metric(const string& mname, const string& lname, const string& lvalue):
432     m(mname), n(lname), v(lvalue)
433   {
434     add_metric (m, n, v, 1);
435   }
~tmp_inc_metric()436   ~tmp_inc_metric()
437   {
438     add_metric (m, n, v, -1);
439   }
440 };
441 
442 class tmp_ms_metric { // a RAII style wrapper for exception-safe scoped timing
443   string m, n, v;
444   struct timespec ts_start;
445 public:
tmp_ms_metric(const string & mname,const string & lname,const string & lvalue)446   tmp_ms_metric(const string& mname, const string& lname, const string& lvalue):
447     m(mname), n(lname), v(lvalue)
448   {
449     clock_gettime (CLOCK_MONOTONIC, & ts_start);
450   }
~tmp_ms_metric()451   ~tmp_ms_metric()
452   {
453     struct timespec ts_end;
454     clock_gettime (CLOCK_MONOTONIC, & ts_end);
455     double deltas = (ts_end.tv_sec - ts_start.tv_sec)
456       + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
457 
458     add_metric (m + "_milliseconds_sum", n, v, (deltas*1000.0));
459     inc_metric (m + "_milliseconds_count", n, v);
460   }
461 };
462 
463 
464 /* Handle program arguments.  */
465 static error_t
parse_opt(int key,char * arg,struct argp_state * state)466 parse_opt (int key, char *arg,
467 	   struct argp_state *state __attribute__ ((unused)))
468 {
469   int rc;
470   switch (key)
471     {
472     case 'v': verbose ++; break;
473     case 'd': db_path = string(arg); break;
474     case 'p': http_port = (unsigned) atoi(arg);
475       if (http_port == 0 || http_port > 65535)
476         argp_failure(state, 1, EINVAL, "port number");
477       break;
478     case 'F': scan_files = true; break;
479     case 'R':
480       scan_archives[".rpm"]="cat"; // libarchive groks rpm natively
481       break;
482     case 'U':
483       if (access("/usr/bin/dpkg-deb", X_OK) == 0)
484         {
485           scan_archives[".deb"]="dpkg-deb --fsys-tarfile";
486           scan_archives[".ddeb"]="dpkg-deb --fsys-tarfile";
487         }
488       else
489         {
490           scan_archives[".deb"]="(bsdtar -O -x -f - data.tar.xz)<";
491           scan_archives[".ddeb"]="(bsdtar -O -x -f - data.tar.xz)<";
492         }
493       // .udeb too?
494       break;
495     case 'Z':
496       {
497         char* extension = strchr(arg, '=');
498         if (arg[0] == '\0')
499           argp_failure(state, 1, EINVAL, "missing EXT");
500         else if (extension)
501           scan_archives[string(arg, (extension-arg))]=string(extension+1);
502         else
503           scan_archives[string(arg)]=string("cat");
504       }
505       break;
506     case 'L':
507       traverse_logical = true;
508       break;
509     case 'D': extra_ddl.push_back(string(arg)); break;
510     case 't':
511       rescan_s = (unsigned) atoi(arg);
512       break;
513     case 'g':
514       groom_s = (unsigned) atoi(arg);
515       break;
516     case 'G':
517       maxigroom = true;
518       break;
519     case 'c':
520       concurrency = (unsigned) atoi(arg);
521       if (concurrency < 1) concurrency = 1;
522       break;
523     case 'I':
524       // NB: no problem with unconditional free here - an earlier failed regcomp would exit program
525       regfree (&file_include_regex);
526       rc = regcomp (&file_include_regex, arg, REG_EXTENDED|REG_NOSUB);
527       if (rc != 0)
528         argp_failure(state, 1, EINVAL, "regular expression");
529       break;
530     case 'X':
531       regfree (&file_exclude_regex);
532       rc = regcomp (&file_exclude_regex, arg, REG_EXTENDED|REG_NOSUB);
533       if (rc != 0)
534         argp_failure(state, 1, EINVAL, "regular expression");
535       break;
536     case ARGP_KEY_FDCACHE_FDS:
537       fdcache_fds = atol (arg);
538       break;
539     case ARGP_KEY_FDCACHE_MBS:
540       fdcache_mbs = atol (arg);
541       break;
542     case ARGP_KEY_FDCACHE_PREFETCH:
543       fdcache_prefetch = atol (arg);
544       break;
545     case ARGP_KEY_FDCACHE_MINTMP:
546       fdcache_mintmp = atol (arg);
547       break;
548     case ARGP_KEY_ARG:
549       source_paths.insert(string(arg));
550       break;
551       // case 'h': argp_state_help (state, stderr, ARGP_HELP_LONG|ARGP_HELP_EXIT_OK);
552     default: return ARGP_ERR_UNKNOWN;
553     }
554 
555   return 0;
556 }
557 
558 
559 ////////////////////////////////////////////////////////////////////////
560 
561 
562 // represent errors that may get reported to an ostream and/or a libmicrohttpd connection
563 
564 struct reportable_exception
565 {
566   int code;
567   string message;
568 
reportable_exceptionreportable_exception569   reportable_exception(int c, const string& m): code(c), message(m) {}
reportable_exceptionreportable_exception570   reportable_exception(const string& m): code(503), message(m) {}
reportable_exceptionreportable_exception571   reportable_exception(): code(503), message() {}
572 
573   void report(ostream& o) const; // defined under obatched() class below
574 
mhd_send_responsereportable_exception575   MHD_RESULT mhd_send_response(MHD_Connection* c) const {
576     MHD_Response* r = MHD_create_response_from_buffer (message.size(),
577                                                        (void*) message.c_str(),
578                                                        MHD_RESPMEM_MUST_COPY);
579     MHD_add_response_header (r, "Content-Type", "text/plain");
580     MHD_RESULT rc = MHD_queue_response (c, code, r);
581     MHD_destroy_response (r);
582     return rc;
583   }
584 };
585 
586 
587 struct sqlite_exception: public reportable_exception
588 {
sqlite_exceptionsqlite_exception589   sqlite_exception(int rc, const string& msg):
590     reportable_exception(string("sqlite3 error: ") + msg + ": " + string(sqlite3_errstr(rc) ?: "?")) {
591     inc_metric("error_count","sqlite3",sqlite3_errstr(rc));
592   }
593 };
594 
595 struct libc_exception: public reportable_exception
596 {
libc_exceptionlibc_exception597   libc_exception(int rc, const string& msg):
598     reportable_exception(string("libc error: ") + msg + ": " + string(strerror(rc) ?: "?")) {
599     inc_metric("error_count","libc",strerror(rc));
600   }
601 };
602 
603 
604 struct archive_exception: public reportable_exception
605 {
archive_exceptionarchive_exception606   archive_exception(const string& msg):
607     reportable_exception(string("libarchive error: ") + msg) {
608       inc_metric("error_count","libarchive",msg);
609   }
archive_exceptionarchive_exception610   archive_exception(struct archive* a, const string& msg):
611     reportable_exception(string("libarchive error: ") + msg + ": " + string(archive_error_string(a) ?: "?")) {
612     inc_metric("error_count","libarchive",msg + ": " + string(archive_error_string(a) ?: "?"));
613   }
614 };
615 
616 
617 struct elfutils_exception: public reportable_exception
618 {
elfutils_exceptionelfutils_exception619   elfutils_exception(int rc, const string& msg):
620     reportable_exception(string("elfutils error: ") + msg + ": " + string(elf_errmsg(rc) ?: "?")) {
621     inc_metric("error_count","elfutils",elf_errmsg(rc));
622   }
623 };
624 
625 
626 ////////////////////////////////////////////////////////////////////////
627 
628 template <typename Payload>
629 class workq
630 {
631   set<Payload> q; // eliminate duplicates
632   mutex mtx;
633   condition_variable cv;
634   bool dead;
635   unsigned idlers;
636 
637 public:
workq()638   workq() { dead = false; idlers = 0; }
~workq()639   ~workq() {}
640 
push_back(const Payload & p)641   void push_back(const Payload& p)
642   {
643     unique_lock<mutex> lock(mtx);
644     q.insert (p);
645     set_metric("thread_work_pending","role","scan", q.size());
646     cv.notify_all();
647   }
648 
649   // kill this workqueue, wake up all idlers / scanners
nuke()650   void nuke() {
651     unique_lock<mutex> lock(mtx);
652     // optional: q.clear();
653     dead = true;
654     cv.notify_all();
655   }
656 
657   // clear the workqueue, when scanning is interrupted with USR2
clear()658   void clear() {
659     unique_lock<mutex> lock(mtx);
660     q.clear();
661     set_metric("thread_work_pending","role","scan", q.size());
662     cv.notify_all(); // maybe wake up waiting idlers
663   }
664 
665   // block this scanner thread until there is work to do and no active
wait_front(Payload & p)666   bool wait_front (Payload& p)
667   {
668     unique_lock<mutex> lock(mtx);
669     while (!dead && (q.size() == 0 || idlers > 0))
670       cv.wait(lock);
671     if (dead)
672       return false;
673     else
674       {
675         p = * q.begin();
676         q.erase (q.begin());
677         set_metric("thread_work_pending","role","scan", q.size());
678         if (q.size() == 0)
679           cv.notify_all(); // maybe wake up waiting idlers
680         return true;
681       }
682   }
683 
684   // block this idler thread until there is no work to do
wait_idle()685   void wait_idle ()
686   {
687     unique_lock<mutex> lock(mtx);
688     cv.notify_all(); // maybe wake up waiting scanners
689     while (!dead && (q.size() != 0))
690       cv.wait(lock);
691     idlers ++;
692   }
693 
done_idle()694   void done_idle ()
695   {
696     unique_lock<mutex> lock(mtx);
697     idlers --;
698     cv.notify_all(); // maybe wake up waiting scanners, but probably not (shutting down)
699   }
700 };
701 
702 typedef struct stat stat_t;
703 typedef pair<string,stat_t> scan_payload;
operator <(const scan_payload & a,const scan_payload & b)704 inline bool operator< (const scan_payload& a, const scan_payload& b)
705 {
706   return a.first < b.first; // don't bother compare the stat fields
707 }
708 static workq<scan_payload> scanq; // just a single one
709 // producer & idler: thread_main_fts_source_paths()
710 // consumer: thread_main_scanner()
711 // idler: thread_main_groom()
712 
713 
714 
715 ////////////////////////////////////////////////////////////////////////
716 
717 
718 // Print a standard timestamp.
719 static ostream&
timestamp(ostream & o)720 timestamp (ostream &o)
721 {
722   char datebuf[80];
723   char *now2 = NULL;
724   time_t now_t = time(NULL);
725   struct tm *now = gmtime (&now_t);
726   if (now)
727     {
728       (void) strftime (datebuf, sizeof (datebuf), "%c", now);
729       now2 = datebuf;
730     }
731 
732   return o << "[" << (now2 ? now2 : "") << "] "
733            << "(" << getpid () << "/" << tid() << "): ";
734 }
735 
736 
737 // A little class that impersonates an ostream to the extent that it can
738 // take << streaming operations.  It batches up the bits into an internal
739 // stringstream until it is destroyed; then flushes to the original ostream.
740 // It adds a timestamp
741 class obatched
742 {
743 private:
744   ostream& o;
745   stringstream stro;
746   static mutex lock;
747 public:
obatched(ostream & oo,bool timestamp_p=true)748   obatched(ostream& oo, bool timestamp_p = true): o(oo)
749   {
750     if (timestamp_p)
751       timestamp(stro);
752   }
~obatched()753   ~obatched()
754   {
755     unique_lock<mutex> do_not_cross_the_streams(obatched::lock);
756     o << stro.str();
757     o.flush();
758   }
operator ostream&()759   operator ostream& () { return stro; }
operator <<(const T & t)760   template <typename T> ostream& operator << (const T& t) { stro << t; return stro; }
761 };
762 mutex obatched::lock; // just the one, since cout/cerr iostreams are not thread-safe
763 
764 
report(ostream & o) const765 void reportable_exception::report(ostream& o) const {
766   obatched(o) << message << endl;
767 }
768 
769 
770 ////////////////////////////////////////////////////////////////////////
771 
772 
773 // RAII style sqlite prepared-statement holder that matches { } block lifetime
774 
775 struct sqlite_ps
776 {
777 private:
778   sqlite3* db;
779   const string nickname;
780   const string sql;
781   sqlite3_stmt *pp;
782 
783   sqlite_ps(const sqlite_ps&); // make uncopyable
784   sqlite_ps& operator=(const sqlite_ps &); // make unassignable
785 
786 public:
sqlite_pssqlite_ps787   sqlite_ps (sqlite3* d, const string& n, const string& s): db(d), nickname(n), sql(s) {
788     // tmp_ms_metric tick("sqlite3","prep",nickname);
789     if (verbose > 4)
790       obatched(clog) << nickname << " prep " << sql << endl;
791     int rc = sqlite3_prepare_v2 (db, sql.c_str(), -1 /* to \0 */, & this->pp, NULL);
792     if (rc != SQLITE_OK)
793       throw sqlite_exception(rc, "prepare " + sql);
794   }
795 
resetsqlite_ps796   sqlite_ps& reset()
797   {
798     tmp_ms_metric tick("sqlite3","reset",nickname);
799     sqlite3_reset(this->pp);
800     return *this;
801   }
802 
bindsqlite_ps803   sqlite_ps& bind(int parameter, const string& str)
804   {
805     if (verbose > 4)
806       obatched(clog) << nickname << " bind " << parameter << "=" << str << endl;
807     int rc = sqlite3_bind_text (this->pp, parameter, str.c_str(), -1, SQLITE_TRANSIENT);
808     if (rc != SQLITE_OK)
809       throw sqlite_exception(rc, "sqlite3 bind");
810     return *this;
811   }
812 
bindsqlite_ps813   sqlite_ps& bind(int parameter, int64_t value)
814   {
815     if (verbose > 4)
816       obatched(clog) << nickname << " bind " << parameter << "=" << value << endl;
817     int rc = sqlite3_bind_int64 (this->pp, parameter, value);
818     if (rc != SQLITE_OK)
819       throw sqlite_exception(rc, "sqlite3 bind");
820     return *this;
821   }
822 
bindsqlite_ps823   sqlite_ps& bind(int parameter)
824   {
825     if (verbose > 4)
826       obatched(clog) << nickname << " bind " << parameter << "=" << "NULL" << endl;
827     int rc = sqlite3_bind_null (this->pp, parameter);
828     if (rc != SQLITE_OK)
829       throw sqlite_exception(rc, "sqlite3 bind");
830     return *this;
831   }
832 
833 
step_ok_donesqlite_ps834   void step_ok_done() {
835     tmp_ms_metric tick("sqlite3","step_done",nickname);
836     int rc = sqlite3_step (this->pp);
837     if (verbose > 4)
838       obatched(clog) << nickname << " step-ok-done(" << sqlite3_errstr(rc) << ") " << sql << endl;
839     if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW)
840       throw sqlite_exception(rc, "sqlite3 step");
841     (void) sqlite3_reset (this->pp);
842   }
843 
844 
stepsqlite_ps845   int step() {
846     tmp_ms_metric tick("sqlite3","step",nickname);
847     int rc = sqlite3_step (this->pp);
848     if (verbose > 4)
849       obatched(clog) << nickname << " step(" << sqlite3_errstr(rc) << ") " << sql << endl;
850     return rc;
851   }
852 
~sqlite_pssqlite_ps853   ~sqlite_ps () { sqlite3_finalize (this->pp); }
operator sqlite3_stmt*sqlite_ps854   operator sqlite3_stmt* () { return this->pp; }
855 };
856 
857 
858 ////////////////////////////////////////////////////////////////////////
859 
860 // RAII style templated autocloser
861 
862 template <class Payload, class Ignore>
863 struct defer_dtor
864 {
865 public:
866   typedef Ignore (*dtor_fn) (Payload);
867 
868 private:
869   Payload p;
870   dtor_fn fn;
871 
872 public:
defer_dtordefer_dtor873   defer_dtor(Payload _p, dtor_fn _fn): p(_p), fn(_fn) {}
~defer_dtordefer_dtor874   ~defer_dtor() { (void) (*fn)(p); }
875 
876 private:
877   defer_dtor(const defer_dtor<Payload,Ignore>&); // make uncopyable
878   defer_dtor& operator=(const defer_dtor<Payload,Ignore> &); // make unassignable
879 };
880 
881 
882 
883 ////////////////////////////////////////////////////////////////////////
884 
885 
886 static string
header_censor(const string & str)887 header_censor(const string& str)
888 {
889   string y;
890   for (auto&& x : str)
891     {
892       if (isalnum(x) || x == '/' || x == '.' || x == ',' || x == '_' || x == ':')
893         y += x;
894     }
895   return y;
896 }
897 
898 
899 static string
conninfo(struct MHD_Connection * conn)900 conninfo (struct MHD_Connection * conn)
901 {
902   char hostname[256]; // RFC1035
903   char servname[256];
904   int sts = -1;
905 
906   if (conn == 0)
907     return "internal";
908 
909   /* Look up client address data. */
910   const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
911                                                                MHD_CONNECTION_INFO_CLIENT_ADDRESS);
912   struct sockaddr *so = u ? u->client_addr : 0;
913 
914   if (so && so->sa_family == AF_INET) {
915     sts = getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), servname,
916                        sizeof (servname), NI_NUMERICHOST | NI_NUMERICSERV);
917   } else if (so && so->sa_family == AF_INET6) {
918     sts = getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname),
919                        servname, sizeof (servname), NI_NUMERICHOST | NI_NUMERICSERV);
920   }
921   if (sts != 0) {
922     hostname[0] = servname[0] = '\0';
923   }
924 
925   // extract headers relevant to administration
926   const char* user_agent = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
927   const char* x_forwarded_for = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
928   // NB: these are untrustworthy, beware if machine-processing log files
929 
930   return string(hostname) + string(":") + string(servname) +
931     string(" UA:") + header_censor(string(user_agent)) +
932     string(" XFF:") + header_censor(string(x_forwarded_for));
933 }
934 
935 
936 
937 ////////////////////////////////////////////////////////////////////////
938 
939 
940 static void
add_mhd_last_modified(struct MHD_Response * resp,time_t mtime)941 add_mhd_last_modified (struct MHD_Response *resp, time_t mtime)
942 {
943   struct tm *now = gmtime (&mtime);
944   if (now != NULL)
945     {
946       char datebuf[80];
947       size_t rc = strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %T GMT", now);
948       if (rc > 0 && rc < sizeof (datebuf))
949         (void) MHD_add_response_header (resp, "Last-Modified", datebuf);
950     }
951 
952   (void) MHD_add_response_header (resp, "Cache-Control", "public");
953 }
954 
955 
956 
957 static struct MHD_Response*
handle_buildid_f_match(bool internal_req_t,int64_t b_mtime,const string & b_source0,int * result_fd)958 handle_buildid_f_match (bool internal_req_t,
959                         int64_t b_mtime,
960                         const string& b_source0,
961                         int *result_fd)
962 {
963   (void) internal_req_t; // ignored
964   int fd = open(b_source0.c_str(), O_RDONLY);
965   if (fd < 0)
966     throw libc_exception (errno, string("open ") + b_source0);
967 
968   // NB: use manual close(2) in error case instead of defer_dtor, because
969   // in the normal case, we want to hand the fd over to libmicrohttpd for
970   // file transfer.
971 
972   struct stat s;
973   int rc = fstat(fd, &s);
974   if (rc < 0)
975     {
976       close(fd);
977       throw libc_exception (errno, string("fstat ") + b_source0);
978     }
979 
980   if ((int64_t) s.st_mtime != b_mtime)
981     {
982       if (verbose)
983         obatched(clog) << "mtime mismatch for " << b_source0 << endl;
984       close(fd);
985       return 0;
986     }
987 
988   inc_metric ("http_responses_total","result","file");
989   struct MHD_Response* r = MHD_create_response_from_fd ((uint64_t) s.st_size, fd);
990   if (r == 0)
991     {
992       if (verbose)
993         obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
994       close(fd);
995     }
996   else
997     {
998       MHD_add_response_header (r, "Content-Type", "application/octet-stream");
999       add_mhd_last_modified (r, s.st_mtime);
1000       if (verbose > 1)
1001         obatched(clog) << "serving file " << b_source0 << endl;
1002       /* libmicrohttpd will close it. */
1003       if (result_fd)
1004         *result_fd = fd;
1005     }
1006 
1007   return r;
1008 }
1009 
1010 
1011 // quote all questionable characters of str for safe passage through a sh -c expansion.
1012 static string
shell_escape(const string & str)1013 shell_escape(const string& str)
1014 {
1015   string y;
1016   for (auto&& x : str)
1017     {
1018       if (! isalnum(x) && x != '/')
1019         y += "\\";
1020       y += x;
1021     }
1022   return y;
1023 }
1024 
1025 
1026 // PR25548: Perform POSIX / RFC3986 style path canonicalization on the input string.
1027 //
1028 // Namely:
1029 //    //         ->   /
1030 //    /foo/../   ->   /
1031 //    /./        ->   /
1032 //
1033 // This mapping is done on dwarf-side source path names, which may
1034 // include these constructs, so we can deal with debuginfod clients
1035 // that accidentally canonicalize the paths.
1036 //
1037 // realpath(3) is close but not quite right, because it also resolves
1038 // symbolic links.  Symlinks at the debuginfod server have nothing to
1039 // do with the build-time symlinks, thus they must not be considered.
1040 //
1041 // see also curl Curl_dedotdotify() aka RFC3986, which we mostly follow here
1042 // see also libc __realpath()
1043 // see also llvm llvm::sys::path::remove_dots()
1044 static string
canon_pathname(const string & input)1045 canon_pathname (const string& input)
1046 {
1047   string i = input; // 5.2.4 (1)
1048   string o;
1049 
1050   while (i.size() != 0)
1051     {
1052       // 5.2.4 (2) A
1053       if (i.substr(0,3) == "../")
1054         i = i.substr(3);
1055       else if(i.substr(0,2) == "./")
1056         i = i.substr(2);
1057 
1058       // 5.2.4 (2) B
1059       else if (i.substr(0,3) == "/./")
1060         i = i.substr(2);
1061       else if (i == "/.")
1062         i = ""; // no need to handle "/." complete-path-segment case; we're dealing with file names
1063 
1064       // 5.2.4 (2) C
1065       else if (i.substr(0,4) == "/../") {
1066         i = i.substr(3);
1067         string::size_type sl = o.rfind("/");
1068         if (sl != string::npos)
1069           o = o.substr(0, sl);
1070         else
1071           o = "";
1072       } else if (i == "/..")
1073         i = ""; // no need to handle "/.." complete-path-segment case; we're dealing with file names
1074 
1075       // 5.2.4 (2) D
1076       // no need to handle these cases; we're dealing with file names
1077       else if (i == ".")
1078         i = "";
1079       else if (i == "..")
1080         i = "";
1081 
1082       // POSIX special: map // to /
1083       else if (i.substr(0,2) == "//")
1084         i = i.substr(1);
1085 
1086       // 5.2.4 (2) E
1087       else {
1088         string::size_type next_slash = i.find("/", (i[0]=='/' ? 1 : 0)); // skip first slash
1089         o += i.substr(0, next_slash);
1090         if (next_slash == string::npos)
1091           i = "";
1092         else
1093           i = i.substr(next_slash);
1094       }
1095     }
1096 
1097   return o;
1098 }
1099 
1100 
1101 // Estimate available free space for a given filesystem via statfs(2).
1102 // Return true if the free fraction is known to be smaller than the
1103 // given minimum percentage.  Also update a related metric.
statfs_free_enough_p(const string & path,const string & label,long minfree=0)1104 bool statfs_free_enough_p(const string& path, const string& label, long minfree = 0)
1105 {
1106   struct statfs sfs;
1107   int rc = statfs(path.c_str(), &sfs);
1108   if (rc == 0)
1109     {
1110       double s = (double) sfs.f_bavail / (double) sfs.f_blocks;
1111       set_metric("filesys_free_ratio","purpose",label, s);
1112       return ((s * 100.0) < minfree);
1113     }
1114   return false;
1115 }
1116 
1117 
1118 
1119 // A map-like class that owns a cache of file descriptors (indexed by
1120 // file / content names).
1121 //
1122 // If only it could use fd's instead of file names ... but we can't
1123 // dup(2) to create independent descriptors for the same unlinked
1124 // files, so would have to use some goofy linux /proc/self/fd/%d
1125 // hack such as the following
1126 
1127 #if 0
1128 int superdup(int fd)
1129 {
1130 #ifdef __linux__
1131   char *fdpath = NULL;
1132   int rc = asprintf(& fdpath, "/proc/self/fd/%d", fd);
1133   int newfd;
1134   if (rc >= 0)
1135     newfd = open(fdpath, O_RDONLY);
1136   else
1137     newfd = -1;
1138   free (fdpath);
1139   return newfd;
1140 #else
1141   return -1;
1142 #endif
1143 }
1144 #endif
1145 
1146 class libarchive_fdcache
1147 {
1148 private:
1149   mutex fdcache_lock;
1150 
1151   struct fdcache_entry
1152   {
1153     string archive;
1154     string entry;
1155     string fd;
1156     double fd_size_mb; // slightly rounded up megabytes
1157   };
1158   deque<fdcache_entry> lru; // @head: most recently used
1159   long max_fds;
1160   long max_mbs;
1161 
1162 public:
set_metrics()1163   void set_metrics()
1164   {
1165     double total_mb = 0.0;
1166     for (auto i = lru.begin(); i < lru.end(); i++)
1167       total_mb += i->fd_size_mb;
1168     set_metric("fdcache_bytes", (int64_t)(total_mb*1024.0*1024.0));
1169     set_metric("fdcache_count", lru.size());
1170   }
1171 
intern(const string & a,const string & b,string fd,off_t sz,bool front_p)1172   void intern(const string& a, const string& b, string fd, off_t sz, bool front_p)
1173   {
1174     {
1175       unique_lock<mutex> lock(fdcache_lock);
1176       for (auto i = lru.begin(); i < lru.end(); i++) // nuke preexisting copy
1177         {
1178           if (i->archive == a && i->entry == b)
1179             {
1180               unlink (i->fd.c_str());
1181               lru.erase(i);
1182               inc_metric("fdcache_op_count","op","dequeue");
1183               break; // must not continue iterating
1184             }
1185         }
1186       double mb = (sz+65535)/1048576.0; // round up to 64K block
1187       fdcache_entry n = { a, b, fd, mb };
1188       if (front_p)
1189         {
1190           inc_metric("fdcache_op_count","op","enqueue_front");
1191           lru.push_front(n);
1192         }
1193       else
1194         {
1195           inc_metric("fdcache_op_count","op","enqueue_back");
1196           lru.push_back(n);
1197         }
1198       if (verbose > 3)
1199         obatched(clog) << "fdcache interned a=" << a << " b=" << b
1200                        << " fd=" << fd << " mb=" << mb << " front=" << front_p << endl;
1201     }
1202     set_metrics();
1203 
1204     // NB: we age the cache at lookup time too
1205     if (statfs_free_enough_p(tmpdir, "tmpdir", fdcache_mintmp))
1206       {
1207         inc_metric("fdcache_op_count","op","emerg-flush");
1208         obatched(clog) << "fdcache emergency flush for filling tmpdir" << endl;
1209         this->limit(0, 0); // emergency flush
1210       }
1211     else if (front_p)
1212       this->limit(max_fds, max_mbs); // age cache if required
1213   }
1214 
lookup(const string & a,const string & b)1215   int lookup(const string& a, const string& b)
1216   {
1217     int fd = -1;
1218     {
1219       unique_lock<mutex> lock(fdcache_lock);
1220       for (auto i = lru.begin(); i < lru.end(); i++)
1221         {
1222           if (i->archive == a && i->entry == b)
1223             { // found it; move it to head of lru
1224               fdcache_entry n = *i;
1225               lru.erase(i); // invalidates i, so no more iteration!
1226               lru.push_front(n);
1227               inc_metric("fdcache_op_count","op","requeue_front");
1228               fd = open(n.fd.c_str(), O_RDONLY); // NB: no problem if dup() fails; looks like cache miss
1229               break;
1230             }
1231         }
1232     }
1233 
1234     if (statfs_free_enough_p(tmpdir, "tmpdir", fdcache_mintmp))
1235       {
1236         inc_metric("fdcache_op_count","op","emerg-flush");
1237         obatched(clog) << "fdcache emergency flush for filling tmpdir";
1238         this->limit(0, 0); // emergency flush
1239       }
1240     else if (fd >= 0)
1241       this->limit(max_fds, max_mbs); // age cache if required
1242 
1243     return fd;
1244   }
1245 
probe(const string & a,const string & b)1246   int probe(const string& a, const string& b) // just a cache residency check - don't modify LRU state, don't open
1247   {
1248     unique_lock<mutex> lock(fdcache_lock);
1249     for (auto i = lru.begin(); i < lru.end(); i++)
1250       {
1251         if (i->archive == a && i->entry == b)
1252           {
1253             inc_metric("fdcache_op_count","op","probe_hit");
1254             return true;
1255           }
1256       }
1257     inc_metric("fdcache_op_count","op","probe_miss");
1258     return false;
1259   }
1260 
clear(const string & a,const string & b)1261   void clear(const string& a, const string& b)
1262   {
1263     unique_lock<mutex> lock(fdcache_lock);
1264     for (auto i = lru.begin(); i < lru.end(); i++)
1265       {
1266         if (i->archive == a && i->entry == b)
1267           { // found it; move it to head of lru
1268             fdcache_entry n = *i;
1269             lru.erase(i); // invalidates i, so no more iteration!
1270             inc_metric("fdcache_op_count","op","clear");
1271             unlink (n.fd.c_str());
1272             set_metrics();
1273             return;
1274           }
1275       }
1276   }
1277 
1278 
limit(long maxfds,long maxmbs,bool metrics_p=true)1279   void limit(long maxfds, long maxmbs, bool metrics_p = true)
1280   {
1281     if (verbose > 3 && (this->max_fds != maxfds || this->max_mbs != maxmbs))
1282       obatched(clog) << "fdcache limited to maxfds=" << maxfds << " maxmbs=" << maxmbs << endl;
1283 
1284     unique_lock<mutex> lock(fdcache_lock);
1285     this->max_fds = maxfds;
1286     this->max_mbs = maxmbs;
1287 
1288     long total_fd = 0;
1289     double total_mb = 0.0;
1290     for (auto i = lru.begin(); i < lru.end(); i++)
1291       {
1292         // accumulate totals from most recently used one going backward
1293         total_fd ++;
1294         total_mb += i->fd_size_mb;
1295         if (total_fd > max_fds || total_mb > max_mbs)
1296           {
1297             // found the cut here point!
1298 
1299             for (auto j = i; j < lru.end(); j++) // close all the fds from here on in
1300               {
1301                 if (verbose > 3)
1302                   obatched(clog) << "fdcache evicted a=" << j->archive << " b=" << j->entry
1303                                  << " fd=" << j->fd << " mb=" << j->fd_size_mb << endl;
1304                 if (metrics_p)
1305                   inc_metric("fdcache_op_count","op","evict");
1306                 unlink (j->fd.c_str());
1307               }
1308 
1309             lru.erase(i, lru.end()); // erase the nodes generally
1310             break;
1311           }
1312       }
1313     if (metrics_p) set_metrics();
1314   }
1315 
1316 
~libarchive_fdcache()1317   ~libarchive_fdcache()
1318   {
1319     // unlink any fdcache entries in $TMPDIR
1320     // don't update metrics; those globals may be already destroyed
1321     limit(0, 0, false);
1322   }
1323 };
1324 static libarchive_fdcache fdcache;
1325 
1326 
1327 // For security/portability reasons, many distro-package archives have
1328 // a "./" in front of path names; others have nothing, others have
1329 // "/".  Canonicalize them all to a single leading "/", with the
1330 // assumption that this matches the dwarf-derived file names too.
canonicalized_archive_entry_pathname(struct archive_entry * e)1331 string canonicalized_archive_entry_pathname(struct archive_entry *e)
1332 {
1333   string fn = archive_entry_pathname(e);
1334   if (fn.size() == 0)
1335     return fn;
1336   if (fn[0] == '/')
1337     return fn;
1338   if (fn[0] == '.')
1339     return fn.substr(1);
1340   else
1341     return string("/")+fn;
1342 }
1343 
1344 
1345 
1346 static struct MHD_Response*
handle_buildid_r_match(bool internal_req_p,int64_t b_mtime,const string & b_source0,const string & b_source1,int * result_fd)1347 handle_buildid_r_match (bool internal_req_p,
1348                         int64_t b_mtime,
1349                         const string& b_source0,
1350                         const string& b_source1,
1351                         int *result_fd)
1352 {
1353   struct stat fs;
1354   int rc = stat (b_source0.c_str(), &fs);
1355   if (rc != 0)
1356     throw libc_exception (errno, string("stat ") + b_source0);
1357 
1358   if ((int64_t) fs.st_mtime != b_mtime)
1359     {
1360       if (verbose)
1361         obatched(clog) << "mtime mismatch for " << b_source0 << endl;
1362       return 0;
1363     }
1364 
1365   // check for a match in the fdcache first
1366   int fd = fdcache.lookup(b_source0, b_source1);
1367   while (fd >= 0) // got one!; NB: this is really an if() with a possible branch out to the end
1368     {
1369       rc = fstat(fd, &fs);
1370       if (rc < 0) // disappeared?
1371         {
1372           if (verbose)
1373             obatched(clog) << "cannot fstat fdcache " << b_source0 << endl;
1374           close(fd);
1375           fdcache.clear(b_source0, b_source1);
1376           break; // branch out of if "loop", to try new libarchive fetch attempt
1377         }
1378 
1379       struct MHD_Response* r = MHD_create_response_from_fd (fs.st_size, fd);
1380       if (r == 0)
1381         {
1382           if (verbose)
1383             obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
1384           close(fd);
1385           break; // branch out of if "loop", to try new libarchive fetch attempt
1386         }
1387 
1388       inc_metric ("http_responses_total","result","archive fdcache");
1389 
1390       MHD_add_response_header (r, "Content-Type", "application/octet-stream");
1391       add_mhd_last_modified (r, fs.st_mtime);
1392       if (verbose > 1)
1393         obatched(clog) << "serving fdcache archive " << b_source0 << " file " << b_source1 << endl;
1394       /* libmicrohttpd will close it. */
1395       if (result_fd)
1396         *result_fd = fd;
1397       return r;
1398       // NB: see, we never go around the 'loop' more than once
1399     }
1400 
1401   // no match ... grumble, must process the archive
1402   string archive_decoder = "/dev/null";
1403   string archive_extension = "";
1404   for (auto&& arch : scan_archives)
1405     if (string_endswith(b_source0, arch.first))
1406       {
1407         archive_extension = arch.first;
1408         archive_decoder = arch.second;
1409       }
1410   FILE* fp;
1411   defer_dtor<FILE*,int>::dtor_fn dfn;
1412   if (archive_decoder != "cat")
1413     {
1414       string popen_cmd = archive_decoder + " " + shell_escape(b_source0);
1415       fp = popen (popen_cmd.c_str(), "r"); // "e" O_CLOEXEC?
1416       dfn = pclose;
1417       if (fp == NULL)
1418         throw libc_exception (errno, string("popen ") + popen_cmd);
1419     }
1420   else
1421     {
1422       fp = fopen (b_source0.c_str(), "r");
1423       dfn = fclose;
1424       if (fp == NULL)
1425         throw libc_exception (errno, string("fopen ") + b_source0);
1426     }
1427   defer_dtor<FILE*,int> fp_closer (fp, dfn);
1428 
1429   struct archive *a;
1430   a = archive_read_new();
1431   if (a == NULL)
1432     throw archive_exception("cannot create archive reader");
1433   defer_dtor<struct archive*,int> archive_closer (a, archive_read_free);
1434 
1435   rc = archive_read_support_format_all(a);
1436   if (rc != ARCHIVE_OK)
1437     throw archive_exception(a, "cannot select all format");
1438   rc = archive_read_support_filter_all(a);
1439   if (rc != ARCHIVE_OK)
1440     throw archive_exception(a, "cannot select all filters");
1441 
1442   rc = archive_read_open_FILE (a, fp);
1443   if (rc != ARCHIVE_OK)
1444     throw archive_exception(a, "cannot open archive from pipe");
1445 
1446   // archive traversal is in three stages, no, four stages:
1447   // 1) skip entries whose names do not match the requested one
1448   // 2) extract the matching entry name (set r = result)
1449   // 3) extract some number of prefetched entries (just into fdcache)
1450   // 4) abort any further processing
1451   struct MHD_Response* r = 0;                 // will set in stage 2
1452   unsigned prefetch_count =
1453     internal_req_p ? 0 : fdcache_prefetch;    // will decrement in stage 3
1454 
1455   while(r == 0 || prefetch_count > 0) // stage 1, 2, or 3
1456     {
1457       if (interrupted)
1458         break;
1459 
1460       struct archive_entry *e;
1461       rc = archive_read_next_header (a, &e);
1462       if (rc != ARCHIVE_OK)
1463         break;
1464 
1465       if (! S_ISREG(archive_entry_mode (e))) // skip non-files completely
1466         continue;
1467 
1468       string fn = canonicalized_archive_entry_pathname (e);
1469       if ((r == 0) && (fn != b_source1)) // stage 1
1470         continue;
1471 
1472       if (fdcache.probe (b_source0, fn)) // skip if already interned
1473         continue;
1474 
1475       // extract this file to a temporary file
1476       char* tmppath = NULL;
1477       rc = asprintf (&tmppath, "%s/debuginfod.XXXXXX", tmpdir.c_str());
1478       if (rc < 0)
1479         throw libc_exception (ENOMEM, "cannot allocate tmppath");
1480       defer_dtor<void*,void> tmmpath_freer (tmppath, free);
1481       fd = mkstemp (tmppath);
1482       if (fd < 0)
1483         throw libc_exception (errno, "cannot create temporary file");
1484       // NB: don't unlink (tmppath), as fdcache will take charge of it.
1485 
1486       // NB: this can take many uninterruptible seconds for a huge file
1487       rc = archive_read_data_into_fd (a, fd);
1488       if (rc != ARCHIVE_OK) // e.g. ENOSPC!
1489         {
1490           close (fd);
1491           unlink (tmppath);
1492           throw archive_exception(a, "cannot extract file");
1493         }
1494 
1495       // Set the mtime so the fdcache file mtimes, even prefetched ones,
1496       // propagate to future webapi clients.
1497       struct timeval tvs[2];
1498       tvs[0].tv_sec = tvs[1].tv_sec = archive_entry_mtime(e);
1499       tvs[0].tv_usec = tvs[1].tv_usec = 0;
1500       (void) futimes (fd, tvs);  /* best effort */
1501 
1502       if (r != 0) // stage 3
1503         {
1504           // NB: now we know we have a complete reusable file; make fdcache
1505           // responsible for unlinking it later.
1506           fdcache.intern(b_source0, fn,
1507                          tmppath, archive_entry_size(e),
1508                          false); // prefetched ones go to back of lru
1509           prefetch_count --;
1510           close (fd); // we're not saving this fd to make a mhd-response from!
1511           continue;
1512         }
1513 
1514       // NB: now we know we have a complete reusable file; make fdcache
1515       // responsible for unlinking it later.
1516       fdcache.intern(b_source0, b_source1,
1517                      tmppath, archive_entry_size(e),
1518                      true); // requested ones go to the front of lru
1519 
1520       inc_metric ("http_responses_total","result",archive_extension + " archive");
1521       r = MHD_create_response_from_fd (archive_entry_size(e), fd);
1522       if (r == 0)
1523         {
1524           if (verbose)
1525             obatched(clog) << "cannot create fd-response for " << b_source0 << endl;
1526           close(fd);
1527           break; // assume no chance of better luck around another iteration; no other copies of same file
1528         }
1529       else
1530         {
1531           MHD_add_response_header (r, "Content-Type", "application/octet-stream");
1532           add_mhd_last_modified (r, archive_entry_mtime(e));
1533           if (verbose > 1)
1534             obatched(clog) << "serving archive " << b_source0 << " file " << b_source1 << endl;
1535           /* libmicrohttpd will close it. */
1536           if (result_fd)
1537             *result_fd = fd;
1538           continue;
1539         }
1540     }
1541 
1542   // XXX: rpm/file not found: delete this R entry?
1543   return r;
1544 }
1545 
1546 
1547 static struct MHD_Response*
handle_buildid_match(bool internal_req_p,int64_t b_mtime,const string & b_stype,const string & b_source0,const string & b_source1,int * result_fd)1548 handle_buildid_match (bool internal_req_p,
1549                       int64_t b_mtime,
1550                       const string& b_stype,
1551                       const string& b_source0,
1552                       const string& b_source1,
1553                       int *result_fd)
1554 {
1555   try
1556     {
1557       if (b_stype == "F")
1558         return handle_buildid_f_match(internal_req_p, b_mtime, b_source0, result_fd);
1559       else if (b_stype == "R")
1560         return handle_buildid_r_match(internal_req_p, b_mtime, b_source0, b_source1, result_fd);
1561     }
1562   catch (const reportable_exception &e)
1563     {
1564       e.report(clog);
1565       // Report but swallow libc etc. errors here; let the caller
1566       // iterate to other matches of the content.
1567     }
1568 
1569   return 0;
1570 }
1571 
1572 
1573 static int
debuginfod_find_progress(debuginfod_client *,long a,long b)1574 debuginfod_find_progress (debuginfod_client *, long a, long b)
1575 {
1576   if (verbose > 4)
1577     obatched(clog) << "federated debuginfod progress=" << a << "/" << b << endl;
1578 
1579   return interrupted;
1580 }
1581 
1582 
1583 static struct MHD_Response*
handle_buildid(MHD_Connection * conn,const string & buildid,const string & artifacttype,const string & suffix,int * result_fd)1584 handle_buildid (MHD_Connection* conn,
1585                 const string& buildid /* unsafe */,
1586                 const string& artifacttype /* unsafe */,
1587                 const string& suffix /* unsafe */,
1588                 int *result_fd)
1589 {
1590   // validate artifacttype
1591   string atype_code;
1592   if (artifacttype == "debuginfo") atype_code = "D";
1593   else if (artifacttype == "executable") atype_code = "E";
1594   else if (artifacttype == "source") atype_code = "S";
1595   else throw reportable_exception("invalid artifacttype");
1596 
1597   if (atype_code == "S" && suffix == "")
1598      throw reportable_exception("invalid source suffix");
1599 
1600   // validate buildid
1601   if ((buildid.size() < 2) || // not empty
1602       (buildid.size() % 2) || // even number
1603       (buildid.find_first_not_of("0123456789abcdef") != string::npos)) // pure tasty lowercase hex
1604     throw reportable_exception("invalid buildid");
1605 
1606   if (verbose > 1)
1607     obatched(clog) << "searching for buildid=" << buildid << " artifacttype=" << artifacttype
1608          << " suffix=" << suffix << endl;
1609 
1610   // If invoked from the scanner threads, use the scanners' read-write
1611   // connection.  Otherwise use the web query threads' read-only connection.
1612   sqlite3 *thisdb = (conn == 0) ? db : dbq;
1613 
1614   sqlite_ps *pp = 0;
1615 
1616   if (atype_code == "D")
1617     {
1618       pp = new sqlite_ps (thisdb, "mhd-query-d",
1619                           "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_d where buildid = ? "
1620                           "order by mtime desc");
1621       pp->reset();
1622       pp->bind(1, buildid);
1623     }
1624   else if (atype_code == "E")
1625     {
1626       pp = new sqlite_ps (thisdb, "mhd-query-e",
1627                           "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_e where buildid = ? "
1628                           "order by mtime desc");
1629       pp->reset();
1630       pp->bind(1, buildid);
1631     }
1632   else if (atype_code == "S")
1633     {
1634       // PR25548
1635       // Incoming source queries may come in with either dwarf-level OR canonicalized paths.
1636       // We let the query pass with either one.
1637 
1638       pp = new sqlite_ps (thisdb, "mhd-query-s",
1639                           "select mtime, sourcetype, source0, source1 from " BUILDIDS "_query_s where buildid = ? and artifactsrc in (?,?) "
1640                           "order by sharedprefix(source0,source0ref) desc, mtime desc");
1641       pp->reset();
1642       pp->bind(1, buildid);
1643       // NB: we don't store the non-canonicalized path names any more, but old databases
1644       // might have them (and no canon ones), so we keep searching for both.
1645       pp->bind(2, suffix);
1646       pp->bind(3, canon_pathname(suffix));
1647     }
1648   unique_ptr<sqlite_ps> ps_closer(pp); // release pp if exception or return
1649 
1650   // consume all the rows
1651   while (1)
1652     {
1653       int rc = pp->step();
1654       if (rc == SQLITE_DONE) break;
1655       if (rc != SQLITE_ROW)
1656         throw sqlite_exception(rc, "step");
1657 
1658       int64_t b_mtime = sqlite3_column_int64 (*pp, 0);
1659       string b_stype = string((const char*) sqlite3_column_text (*pp, 1) ?: ""); /* by DDL may not be NULL */
1660       string b_source0 = string((const char*) sqlite3_column_text (*pp, 2) ?: ""); /* may be NULL */
1661       string b_source1 = string((const char*) sqlite3_column_text (*pp, 3) ?: ""); /* may be NULL */
1662 
1663       if (verbose > 1)
1664         obatched(clog) << "found mtime=" << b_mtime << " stype=" << b_stype
1665              << " source0=" << b_source0 << " source1=" << b_source1 << endl;
1666 
1667       // Try accessing the located match.
1668       // XXX: in case of multiple matches, attempt them in parallel?
1669       auto r = handle_buildid_match (conn ? false : true,
1670                                      b_mtime, b_stype, b_source0, b_source1, result_fd);
1671       if (r)
1672         return r;
1673     }
1674   pp->reset();
1675 
1676   // We couldn't find it in the database.  Last ditch effort
1677   // is to defer to other debuginfo servers.
1678 
1679   int fd = -1;
1680   debuginfod_client *client = debuginfod_begin ();
1681   if (client != NULL)
1682     {
1683       debuginfod_set_progressfn (client, & debuginfod_find_progress);
1684 
1685       if (conn)
1686         {
1687           // Transcribe incoming User-Agent:
1688           string ua = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "User-Agent") ?: "";
1689           string ua_complete = string("User-Agent: ") + ua;
1690           debuginfod_add_http_header (client, ua_complete.c_str());
1691 
1692           // Compute larger XFF:, for avoiding info loss during
1693           // federation, and for future cyclicity detection.
1694           string xff = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Forwarded-For") ?: "";
1695           if (xff != "")
1696             xff += string(", "); // comma separated list
1697 
1698           // Compute the client's numeric IP address only - so can't merge with conninfo()
1699           const union MHD_ConnectionInfo *u = MHD_get_connection_info (conn,
1700                                                                        MHD_CONNECTION_INFO_CLIENT_ADDRESS);
1701           struct sockaddr *so = u ? u->client_addr : 0;
1702           char hostname[256] = ""; // RFC1035
1703           if (so && so->sa_family == AF_INET)
1704             (void) getnameinfo (so, sizeof (struct sockaddr_in), hostname, sizeof (hostname), NULL, 0,
1705                                 NI_NUMERICHOST);
1706           else if (so && so->sa_family == AF_INET6)
1707             (void) getnameinfo (so, sizeof (struct sockaddr_in6), hostname, sizeof (hostname), NULL, 0,
1708                                 NI_NUMERICHOST);
1709 
1710           string xff_complete = string("X-Forwarded-For: ")+xff+string(hostname);
1711           debuginfod_add_http_header (client, xff_complete.c_str());
1712         }
1713 
1714       if (artifacttype == "debuginfo")
1715 	fd = debuginfod_find_debuginfo (client,
1716 					(const unsigned char*) buildid.c_str(),
1717 					0, NULL);
1718       else if (artifacttype == "executable")
1719 	fd = debuginfod_find_executable (client,
1720 					 (const unsigned char*) buildid.c_str(),
1721 					 0, NULL);
1722       else if (artifacttype == "source")
1723 	fd = debuginfod_find_source (client,
1724 				     (const unsigned char*) buildid.c_str(),
1725 				     0, suffix.c_str(), NULL);
1726     }
1727   else
1728     fd = -errno; /* Set by debuginfod_begin.  */
1729   debuginfod_end (client);
1730 
1731   if (fd >= 0)
1732     {
1733       inc_metric ("http_responses_total","result","upstream");
1734       struct stat s;
1735       int rc = fstat (fd, &s);
1736       if (rc == 0)
1737         {
1738           auto r = MHD_create_response_from_fd ((uint64_t) s.st_size, fd);
1739           if (r)
1740             {
1741               MHD_add_response_header (r, "Content-Type", "application/octet-stream");
1742               add_mhd_last_modified (r, s.st_mtime);
1743               if (verbose > 1)
1744                 obatched(clog) << "serving file from upstream debuginfod/cache" << endl;
1745               if (result_fd)
1746                 *result_fd = fd;
1747               return r; // NB: don't close fd; libmicrohttpd will
1748             }
1749         }
1750       close (fd);
1751     }
1752   else
1753     switch(fd)
1754       {
1755       case -ENOSYS:
1756         break;
1757       case -ENOENT:
1758         break;
1759       default: // some more tricky error
1760         throw libc_exception(-fd, "upstream debuginfod query failed");
1761       }
1762 
1763   throw reportable_exception(MHD_HTTP_NOT_FOUND, "not found");
1764 }
1765 
1766 
1767 ////////////////////////////////////////////////////////////////////////
1768 
1769 static map<string,double> metrics; // arbitrary data for /metrics query
1770 // NB: store int64_t since all our metrics are integers; prometheus accepts double
1771 static mutex metrics_lock;
1772 // NB: these objects get released during the process exit via global dtors
1773 // do not call them from within other global dtors
1774 
1775 // utility function for assembling prometheus-compatible
1776 // name="escaped-value" strings
1777 // https://prometheus.io/docs/instrumenting/exposition_formats/
1778 static string
metric_label(const string & name,const string & value)1779 metric_label(const string& name, const string& value)
1780 {
1781   string x = name + "=\"";
1782   for (auto&& c : value)
1783     switch(c)
1784       {
1785       case '\\': x += "\\\\"; break;
1786       case '\"': x += "\\\""; break;
1787       case '\n': x += "\\n"; break;
1788       default: x += c; break;
1789       }
1790   x += "\"";
1791   return x;
1792 }
1793 
1794 
1795 // add prometheus-format metric name + label tuple (if any) + value
1796 
1797 static void
set_metric(const string & metric,double value)1798 set_metric(const string& metric, double value)
1799 {
1800   unique_lock<mutex> lock(metrics_lock);
1801   metrics[metric] = value;
1802 }
1803 #if 0 /* unused */
1804 static void
1805 inc_metric(const string& metric)
1806 {
1807   unique_lock<mutex> lock(metrics_lock);
1808   metrics[metric] ++;
1809 }
1810 #endif
1811 static void
set_metric(const string & metric,const string & lname,const string & lvalue,double value)1812 set_metric(const string& metric,
1813            const string& lname, const string& lvalue,
1814            double value)
1815 {
1816   string key = (metric + "{" + metric_label(lname, lvalue) + "}");
1817   unique_lock<mutex> lock(metrics_lock);
1818   metrics[key] = value;
1819 }
1820 
1821 static void
inc_metric(const string & metric,const string & lname,const string & lvalue)1822 inc_metric(const string& metric,
1823            const string& lname, const string& lvalue)
1824 {
1825   string key = (metric + "{" + metric_label(lname, lvalue) + "}");
1826   unique_lock<mutex> lock(metrics_lock);
1827   metrics[key] ++;
1828 }
1829 static void
add_metric(const string & metric,const string & lname,const string & lvalue,double value)1830 add_metric(const string& metric,
1831            const string& lname, const string& lvalue,
1832            double value)
1833 {
1834   string key = (metric + "{" + metric_label(lname, lvalue) + "}");
1835   unique_lock<mutex> lock(metrics_lock);
1836   metrics[key] += value;
1837 }
1838 #if 0
1839 static void
1840 add_metric(const string& metric,
1841            double value)
1842 {
1843   unique_lock<mutex> lock(metrics_lock);
1844   metrics[metric] += value;
1845 }
1846 #endif
1847 
1848 
1849 // and more for higher arity labels if needed
1850 
1851 
1852 static struct MHD_Response*
handle_metrics(off_t * size)1853 handle_metrics (off_t* size)
1854 {
1855   stringstream o;
1856   {
1857     unique_lock<mutex> lock(metrics_lock);
1858     for (auto&& i : metrics)
1859       o << i.first
1860         << " "
1861         << std::setprecision(std::numeric_limits<double>::digits10 + 1)
1862         << i.second
1863         << endl;
1864   }
1865   const string& os = o.str();
1866   MHD_Response* r = MHD_create_response_from_buffer (os.size(),
1867                                                      (void*) os.c_str(),
1868                                                      MHD_RESPMEM_MUST_COPY);
1869   *size = os.size();
1870   MHD_add_response_header (r, "Content-Type", "text/plain");
1871   return r;
1872 }
1873 
1874 static struct MHD_Response*
handle_root(off_t * size)1875 handle_root (off_t* size)
1876 {
1877   static string version = "debuginfod (" + string (PACKAGE_NAME) + ") "
1878 			  + string (PACKAGE_VERSION);
1879   MHD_Response* r = MHD_create_response_from_buffer (version.size (),
1880 						     (void *) version.c_str (),
1881 						     MHD_RESPMEM_PERSISTENT);
1882   *size = version.size ();
1883   MHD_add_response_header (r, "Content-Type", "text/plain");
1884   return r;
1885 }
1886 
1887 
1888 ////////////////////////////////////////////////////////////////////////
1889 
1890 
1891 /* libmicrohttpd callback */
1892 static MHD_RESULT
handler_cb(void *,struct MHD_Connection * connection,const char * url,const char * method,const char *,const char *,size_t *,void **)1893 handler_cb (void * /*cls*/,
1894             struct MHD_Connection *connection,
1895             const char *url,
1896             const char *method,
1897             const char * /*version*/,
1898             const char * /*upload_data*/,
1899             size_t * /*upload_data_size*/,
1900             void ** /*con_cls*/)
1901 {
1902   struct MHD_Response *r = NULL;
1903   string url_copy = url;
1904 
1905 #if MHD_VERSION >= 0x00097002
1906   enum MHD_Result rc;
1907 #else
1908   int rc = MHD_NO; // mhd
1909 #endif
1910   int http_code = 500;
1911   off_t http_size = -1;
1912   struct timespec ts_start, ts_end;
1913   clock_gettime (CLOCK_MONOTONIC, &ts_start);
1914 
1915   try
1916     {
1917       if (string(method) != "GET")
1918         throw reportable_exception(400, "we support GET only");
1919 
1920       /* Start decoding the URL. */
1921       size_t slash1 = url_copy.find('/', 1);
1922       string url1 = url_copy.substr(0, slash1); // ok even if slash1 not found
1923 
1924       if (slash1 != string::npos && url1 == "/buildid")
1925         {
1926           tmp_inc_metric m ("thread_busy", "role", "http-buildid");
1927           size_t slash2 = url_copy.find('/', slash1+1);
1928           if (slash2 == string::npos)
1929             throw reportable_exception("/buildid/ webapi error, need buildid");
1930 
1931           string buildid = url_copy.substr(slash1+1, slash2-slash1-1);
1932 
1933           size_t slash3 = url_copy.find('/', slash2+1);
1934           string artifacttype, suffix;
1935           if (slash3 == string::npos)
1936             {
1937               artifacttype = url_copy.substr(slash2+1);
1938               suffix = "";
1939             }
1940           else
1941             {
1942               artifacttype = url_copy.substr(slash2+1, slash3-slash2-1);
1943               suffix = url_copy.substr(slash3); // include the slash in the suffix
1944             }
1945 
1946           inc_metric("http_requests_total", "type", artifacttype);
1947           // get the resulting fd so we can report its size
1948           int fd;
1949           r = handle_buildid(connection, buildid, artifacttype, suffix, &fd);
1950           if (r)
1951             {
1952               struct stat fs;
1953               if (fstat(fd, &fs) == 0)
1954                 http_size = fs.st_size;
1955               // libmicrohttpd will close (fd);
1956             }
1957         }
1958       else if (url1 == "/metrics")
1959         {
1960           tmp_inc_metric m ("thread_busy", "role", "http-metrics");
1961           inc_metric("http_requests_total", "type", "metrics");
1962           r = handle_metrics(& http_size);
1963         }
1964       else if (url1 == "/")
1965         {
1966           inc_metric("http_requests_total", "type", "/");
1967           r = handle_root(& http_size);
1968         }
1969       else
1970         throw reportable_exception("webapi error, unrecognized '" + url1 + "'");
1971 
1972       if (r == 0)
1973         throw reportable_exception("internal error, missing response");
1974 
1975       rc = MHD_queue_response (connection, MHD_HTTP_OK, r);
1976       http_code = MHD_HTTP_OK;
1977       MHD_destroy_response (r);
1978     }
1979   catch (const reportable_exception& e)
1980     {
1981       inc_metric("http_responses_total","result","error");
1982       e.report(clog);
1983       http_code = e.code;
1984       http_size = e.message.size();
1985       rc = e.mhd_send_response (connection);
1986     }
1987 
1988   clock_gettime (CLOCK_MONOTONIC, &ts_end);
1989   double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
1990   obatched(clog) << conninfo(connection)
1991                  << ' ' << method << ' ' << url
1992                  << ' ' << http_code << ' ' << http_size
1993                  << ' ' << (int)(deltas*1000) << "ms"
1994                  << endl;
1995 
1996   // related prometheus metrics
1997   string http_code_str = to_string(http_code);
1998   if (http_size >= 0)
1999     add_metric("http_responses_transfer_bytes_sum","code",http_code_str,
2000                http_size);
2001   inc_metric("http_responses_transfer_bytes_count","code",http_code_str);
2002 
2003   add_metric("http_responses_duration_milliseconds_sum","code",http_code_str,
2004              deltas*1000); // prometheus prefers _seconds and floating point
2005   inc_metric("http_responses_duration_milliseconds_count","code",http_code_str);
2006 
2007   return rc;
2008 }
2009 
2010 
2011 ////////////////////////////////////////////////////////////////////////
2012 // borrowed originally from src/nm.c get_local_names()
2013 
2014 static void
dwarf_extract_source_paths(Elf * elf,set<string> & debug_sourcefiles)2015 dwarf_extract_source_paths (Elf *elf, set<string>& debug_sourcefiles)
2016   noexcept // no exceptions - so we can simplify the altdbg resource release at end
2017 {
2018   Dwarf* dbg = dwarf_begin_elf (elf, DWARF_C_READ, NULL);
2019   if (dbg == NULL)
2020     return;
2021 
2022   Dwarf* altdbg = NULL;
2023   int    altdbg_fd = -1;
2024 
2025   // DWZ handling: if we have an unsatisfied debug-alt-link, add an
2026   // empty string into the outgoing sourcefiles set, so the caller
2027   // should know that our data is incomplete.
2028   const char *alt_name_p;
2029   const void *alt_build_id; // elfutils-owned memory
2030   ssize_t sz = dwelf_dwarf_gnu_debugaltlink (dbg, &alt_name_p, &alt_build_id);
2031   if (sz > 0) // got one!
2032     {
2033       string buildid;
2034       unsigned char* build_id_bytes = (unsigned char*) alt_build_id;
2035       for (ssize_t idx=0; idx<sz; idx++)
2036         {
2037           buildid += "0123456789abcdef"[build_id_bytes[idx] >> 4];
2038           buildid += "0123456789abcdef"[build_id_bytes[idx] & 0xf];
2039         }
2040 
2041       if (verbose > 3)
2042         obatched(clog) << "Need altdebug buildid=" << buildid << endl;
2043 
2044       // but is it unsatisfied the normal elfutils ways?
2045       Dwarf* alt = dwarf_getalt (dbg);
2046       if (alt == NULL)
2047         {
2048           // Yup, unsatisfied the normal way.  Maybe we can satisfy it
2049           // from our own debuginfod database.
2050           int alt_fd;
2051           struct MHD_Response *r = 0;
2052           try
2053             {
2054               r = handle_buildid (0, buildid, "debuginfo", "", &alt_fd);
2055             }
2056           catch (const reportable_exception& e)
2057             {
2058               // swallow exceptions
2059             }
2060 
2061           // NB: this is not actually recursive!  This invokes the web-query
2062           // path, which cannot get back into the scan code paths.
2063           if (r)
2064             {
2065               // Found it!
2066               altdbg_fd = dup(alt_fd); // ok if this fails, downstream failures ok
2067               alt = altdbg = dwarf_begin (altdbg_fd, DWARF_C_READ);
2068               // NB: must close this dwarf and this fd at the bottom of the function!
2069               MHD_destroy_response (r); // will close alt_fd
2070               if (alt)
2071                 dwarf_setalt (dbg, alt);
2072             }
2073         }
2074       else
2075         {
2076           // NB: dwarf_setalt(alt) inappropriate - already done!
2077           // NB: altdbg will stay 0 so nothing tries to redundantly dealloc.
2078         }
2079 
2080       if (alt)
2081         {
2082           if (verbose > 3)
2083             obatched(clog) << "Resolved altdebug buildid=" << buildid << endl;
2084         }
2085       else // (alt == NULL) - signal possible presence of poor debuginfo
2086         {
2087           debug_sourcefiles.insert("");
2088           if (verbose > 3)
2089             obatched(clog) << "Unresolved altdebug buildid=" << buildid << endl;
2090         }
2091     }
2092 
2093   Dwarf_Off offset = 0;
2094   Dwarf_Off old_offset;
2095   size_t hsize;
2096 
2097   while (dwarf_nextcu (dbg, old_offset = offset, &offset, &hsize, NULL, NULL, NULL) == 0)
2098     {
2099       Dwarf_Die cudie_mem;
2100       Dwarf_Die *cudie = dwarf_offdie (dbg, old_offset + hsize, &cudie_mem);
2101 
2102       if (cudie == NULL)
2103         continue;
2104       if (dwarf_tag (cudie) != DW_TAG_compile_unit)
2105         continue;
2106 
2107       const char *cuname = dwarf_diename(cudie) ?: "unknown";
2108 
2109       Dwarf_Files *files;
2110       size_t nfiles;
2111       if (dwarf_getsrcfiles (cudie, &files, &nfiles) != 0)
2112         continue;
2113 
2114       // extract DW_AT_comp_dir to resolve relative file names
2115       const char *comp_dir = "";
2116       const char *const *dirs;
2117       size_t ndirs;
2118       if (dwarf_getsrcdirs (files, &dirs, &ndirs) == 0 &&
2119           dirs[0] != NULL)
2120         comp_dir = dirs[0];
2121       if (comp_dir == NULL)
2122         comp_dir = "";
2123 
2124       if (verbose > 3)
2125         obatched(clog) << "searching for sources for cu=" << cuname << " comp_dir=" << comp_dir
2126                        << " #files=" << nfiles << " #dirs=" << ndirs << endl;
2127 
2128       if (comp_dir[0] == '\0' && cuname[0] != '/')
2129         {
2130           // This is a common symptom for dwz-compressed debug files,
2131           // where the altdebug file cannot be resolved.
2132           if (verbose > 3)
2133             obatched(clog) << "skipping cu=" << cuname << " due to empty comp_dir" << endl;
2134           continue;
2135         }
2136 
2137       for (size_t f = 1; f < nfiles; f++)
2138         {
2139           const char *hat = dwarf_filesrc (files, f, NULL, NULL);
2140           if (hat == NULL)
2141             continue;
2142 
2143           if (string(hat) == "<built-in>") // gcc intrinsics, don't bother record
2144             continue;
2145 
2146           string waldo;
2147           if (hat[0] == '/') // absolute
2148             waldo = (string (hat));
2149           else if (comp_dir[0] != '\0') // comp_dir relative
2150             waldo = (string (comp_dir) + string("/") + string (hat));
2151           else
2152            {
2153              if (verbose > 3)
2154                obatched(clog) << "skipping hat=" << hat << " due to empty comp_dir" << endl;
2155              continue;
2156            }
2157 
2158           // NB: this is the 'waldo' that a dbginfo client will have
2159           // to supply for us to give them the file The comp_dir
2160           // prefixing is a definite complication.  Otherwise we'd
2161           // have to return a setof comp_dirs (one per CU!) with
2162           // corresponding filesrc[] names, instead of one absolute
2163           // resoved set.  Maybe we'll have to do that anyway.  XXX
2164 
2165           if (verbose > 4)
2166             obatched(clog) << waldo
2167                            << (debug_sourcefiles.find(waldo)==debug_sourcefiles.end() ? " new" : " dup") <<  endl;
2168 
2169           debug_sourcefiles.insert (waldo);
2170         }
2171     }
2172 
2173   dwarf_end(dbg);
2174   if (altdbg)
2175     dwarf_end(altdbg);
2176   if (altdbg_fd >= 0)
2177     close(altdbg_fd);
2178 }
2179 
2180 
2181 
2182 static void
elf_classify(int fd,bool & executable_p,bool & debuginfo_p,string & buildid,set<string> & debug_sourcefiles)2183 elf_classify (int fd, bool &executable_p, bool &debuginfo_p, string &buildid, set<string>& debug_sourcefiles)
2184 {
2185   Elf *elf = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, NULL);
2186   if (elf == NULL)
2187     return;
2188 
2189   try // catch our types of errors and clean up the Elf* object
2190     {
2191       if (elf_kind (elf) != ELF_K_ELF)
2192         {
2193           elf_end (elf);
2194           return;
2195         }
2196 
2197       GElf_Ehdr ehdr_storage;
2198       GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_storage);
2199       if (ehdr == NULL)
2200         {
2201           elf_end (elf);
2202           return;
2203         }
2204       auto elf_type = ehdr->e_type;
2205 
2206       const void *build_id; // elfutils-owned memory
2207       ssize_t sz = dwelf_elf_gnu_build_id (elf, & build_id);
2208       if (sz <= 0)
2209         {
2210           // It's not a diagnostic-worthy error for an elf file to lack build-id.
2211           // It might just be very old.
2212           elf_end (elf);
2213           return;
2214         }
2215 
2216       // build_id is a raw byte array; convert to hexadecimal *lowercase*
2217       unsigned char* build_id_bytes = (unsigned char*) build_id;
2218       for (ssize_t idx=0; idx<sz; idx++)
2219         {
2220           buildid += "0123456789abcdef"[build_id_bytes[idx] >> 4];
2221           buildid += "0123456789abcdef"[build_id_bytes[idx] & 0xf];
2222         }
2223 
2224       // now decide whether it's an executable - namely, any allocatable section has
2225       // PROGBITS;
2226       if (elf_type == ET_EXEC || elf_type == ET_DYN)
2227         {
2228           size_t shnum;
2229           int rc = elf_getshdrnum (elf, &shnum);
2230           if (rc < 0)
2231             throw elfutils_exception(rc, "getshdrnum");
2232 
2233           executable_p = false;
2234           for (size_t sc = 0; sc < shnum; sc++)
2235             {
2236               Elf_Scn *scn = elf_getscn (elf, sc);
2237               if (scn == NULL)
2238                 continue;
2239 
2240               GElf_Shdr shdr_mem;
2241               GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2242               if (shdr == NULL)
2243                 continue;
2244 
2245               // allocated (loadable / vm-addr-assigned) section with available content?
2246               if ((shdr->sh_type == SHT_PROGBITS) && (shdr->sh_flags & SHF_ALLOC))
2247                 {
2248                   if (verbose > 4)
2249                     obatched(clog) << "executable due to SHF_ALLOC SHT_PROGBITS sc=" << sc << endl;
2250                   executable_p = true;
2251                   break; // no need to keep looking for others
2252                 }
2253             } // iterate over sections
2254         } // executable_p classification
2255 
2256       // now decide whether it's a debuginfo - namely, if it has any .debug* or .zdebug* sections
2257       // logic mostly stolen from fweimer@redhat.com's elfclassify drafts
2258       size_t shstrndx;
2259       int rc = elf_getshdrstrndx (elf, &shstrndx);
2260       if (rc < 0)
2261         throw elfutils_exception(rc, "getshdrstrndx");
2262 
2263       Elf_Scn *scn = NULL;
2264       while (true)
2265         {
2266           scn = elf_nextscn (elf, scn);
2267           if (scn == NULL)
2268             break;
2269           GElf_Shdr shdr_storage;
2270           GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_storage);
2271           if (shdr == NULL)
2272             break;
2273           const char *section_name = elf_strptr (elf, shstrndx, shdr->sh_name);
2274           if (section_name == NULL)
2275             break;
2276           if (strncmp(section_name, ".debug_line", 11) == 0 ||
2277               strncmp(section_name, ".zdebug_line", 12) == 0)
2278             {
2279               debuginfo_p = true;
2280               dwarf_extract_source_paths (elf, debug_sourcefiles);
2281               break; // expecting only one .*debug_line, so no need to look for others
2282             }
2283           else if (strncmp(section_name, ".debug_", 7) == 0 ||
2284                    strncmp(section_name, ".zdebug_", 8) == 0)
2285             {
2286               debuginfo_p = true;
2287               // NB: don't break; need to parse .debug_line for sources
2288             }
2289         }
2290     }
2291   catch (const reportable_exception& e)
2292     {
2293       e.report(clog);
2294     }
2295   elf_end (elf);
2296 }
2297 
2298 
2299 static void
scan_source_file(const string & rps,const stat_t & st,sqlite_ps & ps_upsert_buildids,sqlite_ps & ps_upsert_files,sqlite_ps & ps_upsert_de,sqlite_ps & ps_upsert_s,sqlite_ps & ps_query,sqlite_ps & ps_scan_done,unsigned & fts_cached,unsigned & fts_executable,unsigned & fts_debuginfo,unsigned & fts_sourcefiles)2300 scan_source_file (const string& rps, const stat_t& st,
2301                   sqlite_ps& ps_upsert_buildids,
2302                   sqlite_ps& ps_upsert_files,
2303                   sqlite_ps& ps_upsert_de,
2304                   sqlite_ps& ps_upsert_s,
2305                   sqlite_ps& ps_query,
2306                   sqlite_ps& ps_scan_done,
2307                   unsigned& fts_cached,
2308                   unsigned& fts_executable,
2309                   unsigned& fts_debuginfo,
2310                   unsigned& fts_sourcefiles)
2311 {
2312   /* See if we know of it already. */
2313   int rc = ps_query
2314     .reset()
2315     .bind(1, rps)
2316     .bind(2, st.st_mtime)
2317     .step();
2318   ps_query.reset();
2319   if (rc == SQLITE_ROW) // i.e., a result, as opposed to DONE (no results)
2320     // no need to recheck a file/version we already know
2321     // specifically, no need to elf-begin a file we already determined is non-elf
2322     // (so is stored with buildid=NULL)
2323     {
2324       fts_cached++;
2325       return;
2326     }
2327 
2328   bool executable_p = false, debuginfo_p = false; // E and/or D
2329   string buildid;
2330   set<string> sourcefiles;
2331 
2332   int fd = open (rps.c_str(), O_RDONLY);
2333   try
2334     {
2335       if (fd >= 0)
2336         elf_classify (fd, executable_p, debuginfo_p, buildid, sourcefiles);
2337       else
2338         throw libc_exception(errno, string("open ") + rps);
2339       add_metric ("scanned_bytes_total","source","file",
2340                   st.st_size);
2341       inc_metric ("scanned_files_total","source","file");
2342     }
2343   // NB: we catch exceptions here too, so that we can
2344   // cache the corrupt-elf case (!executable_p &&
2345   // !debuginfo_p) just below, just as if we had an
2346   // EPERM error from open(2).
2347   catch (const reportable_exception& e)
2348     {
2349       e.report(clog);
2350     }
2351 
2352   if (fd >= 0)
2353     close (fd);
2354 
2355   // register this file name in the interning table
2356   ps_upsert_files
2357     .reset()
2358     .bind(1, rps)
2359     .step_ok_done();
2360 
2361   if (buildid == "")
2362     {
2363       // no point storing an elf file without buildid
2364       executable_p = false;
2365       debuginfo_p = false;
2366     }
2367   else
2368     {
2369       // register this build-id in the interning table
2370       ps_upsert_buildids
2371         .reset()
2372         .bind(1, buildid)
2373         .step_ok_done();
2374     }
2375 
2376   if (executable_p)
2377     fts_executable ++;
2378   if (debuginfo_p)
2379     fts_debuginfo ++;
2380   if (executable_p || debuginfo_p)
2381     {
2382       ps_upsert_de
2383         .reset()
2384         .bind(1, buildid)
2385         .bind(2, debuginfo_p ? 1 : 0)
2386         .bind(3, executable_p ? 1 : 0)
2387         .bind(4, rps)
2388         .bind(5, st.st_mtime)
2389         .step_ok_done();
2390     }
2391   if (executable_p)
2392     inc_metric("found_executable_total","source","files");
2393   if (debuginfo_p)
2394     inc_metric("found_debuginfo_total","source","files");
2395 
2396   if (sourcefiles.size() && buildid != "")
2397     {
2398       fts_sourcefiles += sourcefiles.size();
2399 
2400       for (auto&& dwarfsrc : sourcefiles)
2401         {
2402           char *srp = realpath(dwarfsrc.c_str(), NULL);
2403           if (srp == NULL) // also if DWZ unresolved dwarfsrc=""
2404             continue; // unresolvable files are not a serious problem
2405           // throw libc_exception(errno, "fts/file realpath " + srcpath);
2406           string srps = string(srp);
2407           free (srp);
2408 
2409           struct stat sfs;
2410           rc = stat(srps.c_str(), &sfs);
2411           if (rc != 0)
2412             continue;
2413 
2414           if (verbose > 2)
2415             obatched(clog) << "recorded buildid=" << buildid << " file=" << srps
2416                            << " mtime=" << sfs.st_mtime
2417                            << " as source " << dwarfsrc << endl;
2418 
2419           ps_upsert_files
2420             .reset()
2421             .bind(1, srps)
2422             .step_ok_done();
2423 
2424           // PR25548: store canonicalized dwarfsrc path
2425           string dwarfsrc_canon = canon_pathname (dwarfsrc);
2426           if (dwarfsrc_canon != dwarfsrc)
2427             {
2428               if (verbose > 3)
2429                 obatched(clog) << "canonicalized src=" << dwarfsrc << " alias=" << dwarfsrc_canon << endl;
2430             }
2431 
2432           ps_upsert_files
2433             .reset()
2434             .bind(1, dwarfsrc_canon)
2435             .step_ok_done();
2436 
2437           ps_upsert_s
2438             .reset()
2439             .bind(1, buildid)
2440             .bind(2, dwarfsrc_canon)
2441             .bind(3, srps)
2442             .bind(4, sfs.st_mtime)
2443             .step_ok_done();
2444 
2445           inc_metric("found_sourcerefs_total","source","files");
2446         }
2447     }
2448 
2449   ps_scan_done
2450     .reset()
2451     .bind(1, rps)
2452     .bind(2, st.st_mtime)
2453     .bind(3, st.st_size)
2454     .step_ok_done();
2455 
2456   if (verbose > 2)
2457     obatched(clog) << "recorded buildid=" << buildid << " file=" << rps
2458                    << " mtime=" << st.st_mtime << " atype="
2459                    << (executable_p ? "E" : "")
2460                    << (debuginfo_p ? "D" : "") << endl;
2461 }
2462 
2463 
2464 
2465 
2466 
2467 // Analyze given archive file of given age; record buildids / exec/debuginfo-ness of its
2468 // constituent files with given upsert statements.
2469 static void
archive_classify(const string & rps,string & archive_extension,sqlite_ps & ps_upsert_buildids,sqlite_ps & ps_upsert_files,sqlite_ps & ps_upsert_de,sqlite_ps & ps_upsert_sref,sqlite_ps & ps_upsert_sdef,time_t mtime,unsigned & fts_executable,unsigned & fts_debuginfo,unsigned & fts_sref,unsigned & fts_sdef,bool & fts_sref_complete_p)2470 archive_classify (const string& rps, string& archive_extension,
2471                   sqlite_ps& ps_upsert_buildids, sqlite_ps& ps_upsert_files,
2472                   sqlite_ps& ps_upsert_de, sqlite_ps& ps_upsert_sref, sqlite_ps& ps_upsert_sdef,
2473                   time_t mtime,
2474                   unsigned& fts_executable, unsigned& fts_debuginfo, unsigned& fts_sref, unsigned& fts_sdef,
2475                   bool& fts_sref_complete_p)
2476 {
2477   string archive_decoder = "/dev/null";
2478   for (auto&& arch : scan_archives)
2479     if (string_endswith(rps, arch.first))
2480       {
2481         archive_extension = arch.first;
2482         archive_decoder = arch.second;
2483       }
2484 
2485   FILE* fp;
2486   defer_dtor<FILE*,int>::dtor_fn dfn;
2487   if (archive_decoder != "cat")
2488     {
2489       string popen_cmd = archive_decoder + " " + shell_escape(rps);
2490       fp = popen (popen_cmd.c_str(), "r"); // "e" O_CLOEXEC?
2491       dfn = pclose;
2492       if (fp == NULL)
2493         throw libc_exception (errno, string("popen ") + popen_cmd);
2494     }
2495   else
2496     {
2497       fp = fopen (rps.c_str(), "r");
2498       dfn = fclose;
2499       if (fp == NULL)
2500         throw libc_exception (errno, string("fopen ") + rps);
2501     }
2502   defer_dtor<FILE*,int> fp_closer (fp, dfn);
2503 
2504   struct archive *a;
2505   a = archive_read_new();
2506   if (a == NULL)
2507     throw archive_exception("cannot create archive reader");
2508   defer_dtor<struct archive*,int> archive_closer (a, archive_read_free);
2509 
2510   int rc = archive_read_support_format_all(a);
2511   if (rc != ARCHIVE_OK)
2512     throw archive_exception(a, "cannot select all formats");
2513   rc = archive_read_support_filter_all(a);
2514   if (rc != ARCHIVE_OK)
2515     throw archive_exception(a, "cannot select all filters");
2516 
2517   rc = archive_read_open_FILE (a, fp);
2518   if (rc != ARCHIVE_OK)
2519     throw archive_exception(a, "cannot open archive from pipe");
2520 
2521   if (verbose > 3)
2522     obatched(clog) << "libarchive scanning " << rps << endl;
2523 
2524   while(1) // parse archive entries
2525     {
2526     if (interrupted)
2527       break;
2528 
2529     try
2530         {
2531           struct archive_entry *e;
2532           rc = archive_read_next_header (a, &e);
2533           if (rc != ARCHIVE_OK)
2534             break;
2535 
2536           if (! S_ISREG(archive_entry_mode (e))) // skip non-files completely
2537             continue;
2538 
2539           string fn = canonicalized_archive_entry_pathname (e);
2540 
2541           if (verbose > 3)
2542             obatched(clog) << "libarchive checking " << fn << endl;
2543 
2544           // extract this file to a temporary file
2545           char* tmppath = NULL;
2546           rc = asprintf (&tmppath, "%s/debuginfod.XXXXXX", tmpdir.c_str());
2547           if (rc < 0)
2548             throw libc_exception (ENOMEM, "cannot allocate tmppath");
2549           defer_dtor<void*,void> tmmpath_freer (tmppath, free);
2550           int fd = mkstemp (tmppath);
2551           if (fd < 0)
2552             throw libc_exception (errno, "cannot create temporary file");
2553           unlink (tmppath); // unlink now so OS will release the file as soon as we close the fd
2554           defer_dtor<int,int> minifd_closer (fd, close);
2555 
2556           rc = archive_read_data_into_fd (a, fd);
2557           if (rc != ARCHIVE_OK)
2558             throw archive_exception(a, "cannot extract file");
2559 
2560           // finally ... time to run elf_classify on this bad boy and update the database
2561           bool executable_p = false, debuginfo_p = false;
2562           string buildid;
2563           set<string> sourcefiles;
2564           elf_classify (fd, executable_p, debuginfo_p, buildid, sourcefiles);
2565           // NB: might throw
2566 
2567           if (buildid != "") // intern buildid
2568             {
2569               ps_upsert_buildids
2570                 .reset()
2571                 .bind(1, buildid)
2572                 .step_ok_done();
2573             }
2574 
2575           ps_upsert_files // register this rpm constituent file name in interning table
2576             .reset()
2577             .bind(1, fn)
2578             .step_ok_done();
2579 
2580           if (sourcefiles.size() > 0) // sref records needed
2581             {
2582               // NB: we intern each source file once.  Once raw, as it
2583               // appears in the DWARF file list coming back from
2584               // elf_classify() - because it'll end up in the
2585               // _norm.artifactsrc column.  We don't also put another
2586               // version with a '.' at the front, even though that's
2587               // how rpm/cpio packs names, because we hide that from
2588               // the database for storage efficiency.
2589 
2590               for (auto&& s : sourcefiles)
2591                 {
2592                   if (s == "")
2593                     {
2594                       fts_sref_complete_p = false;
2595                       continue;
2596                     }
2597 
2598                   // PR25548: store canonicalized source path
2599                   const string& dwarfsrc = s;
2600                   string dwarfsrc_canon = canon_pathname (dwarfsrc);
2601                   if (dwarfsrc_canon != dwarfsrc)
2602                     {
2603                       if (verbose > 3)
2604                         obatched(clog) << "canonicalized src=" << dwarfsrc << " alias=" << dwarfsrc_canon << endl;
2605                     }
2606 
2607                   ps_upsert_files
2608                     .reset()
2609                     .bind(1, dwarfsrc_canon)
2610                     .step_ok_done();
2611 
2612                   ps_upsert_sref
2613                     .reset()
2614                     .bind(1, buildid)
2615                     .bind(2, dwarfsrc_canon)
2616                     .step_ok_done();
2617 
2618                   fts_sref ++;
2619                 }
2620             }
2621 
2622           if (executable_p)
2623             fts_executable ++;
2624           if (debuginfo_p)
2625             fts_debuginfo ++;
2626 
2627           if (executable_p || debuginfo_p)
2628             {
2629               ps_upsert_de
2630                 .reset()
2631                 .bind(1, buildid)
2632                 .bind(2, debuginfo_p ? 1 : 0)
2633                 .bind(3, executable_p ? 1 : 0)
2634                 .bind(4, rps)
2635                 .bind(5, mtime)
2636                 .bind(6, fn)
2637                 .step_ok_done();
2638             }
2639           else // potential source - sdef record
2640             {
2641               fts_sdef ++;
2642               ps_upsert_sdef
2643                 .reset()
2644                 .bind(1, rps)
2645                 .bind(2, mtime)
2646                 .bind(3, fn)
2647                 .step_ok_done();
2648             }
2649 
2650           if ((verbose > 2) && (executable_p || debuginfo_p))
2651             obatched(clog) << "recorded buildid=" << buildid << " rpm=" << rps << " file=" << fn
2652                            << " mtime=" << mtime << " atype="
2653                            << (executable_p ? "E" : "")
2654                            << (debuginfo_p ? "D" : "")
2655                            << " sourcefiles=" << sourcefiles.size() << endl;
2656 
2657         }
2658       catch (const reportable_exception& e)
2659         {
2660           e.report(clog);
2661         }
2662     }
2663 }
2664 
2665 
2666 
2667 // scan for archive files such as .rpm
2668 static void
scan_archive_file(const string & rps,const stat_t & st,sqlite_ps & ps_upsert_buildids,sqlite_ps & ps_upsert_files,sqlite_ps & ps_upsert_de,sqlite_ps & ps_upsert_sref,sqlite_ps & ps_upsert_sdef,sqlite_ps & ps_query,sqlite_ps & ps_scan_done,unsigned & fts_cached,unsigned & fts_executable,unsigned & fts_debuginfo,unsigned & fts_sref,unsigned & fts_sdef)2669 scan_archive_file (const string& rps, const stat_t& st,
2670                    sqlite_ps& ps_upsert_buildids,
2671                    sqlite_ps& ps_upsert_files,
2672                    sqlite_ps& ps_upsert_de,
2673                    sqlite_ps& ps_upsert_sref,
2674                    sqlite_ps& ps_upsert_sdef,
2675                    sqlite_ps& ps_query,
2676                    sqlite_ps& ps_scan_done,
2677                    unsigned& fts_cached,
2678                    unsigned& fts_executable,
2679                    unsigned& fts_debuginfo,
2680                    unsigned& fts_sref,
2681                    unsigned& fts_sdef)
2682 {
2683   /* See if we know of it already. */
2684   int rc = ps_query
2685     .reset()
2686     .bind(1, rps)
2687     .bind(2, st.st_mtime)
2688     .step();
2689   ps_query.reset();
2690   if (rc == SQLITE_ROW) // i.e., a result, as opposed to DONE (no results)
2691     // no need to recheck a file/version we already know
2692     // specifically, no need to parse this archive again, since we already have
2693     // it as a D or E or S record,
2694     // (so is stored with buildid=NULL)
2695     {
2696       fts_cached ++;
2697       return;
2698     }
2699 
2700   // intern the archive file name
2701   ps_upsert_files
2702     .reset()
2703     .bind(1, rps)
2704     .step_ok_done();
2705 
2706   // extract the archive contents
2707   unsigned my_fts_executable = 0, my_fts_debuginfo = 0, my_fts_sref = 0, my_fts_sdef = 0;
2708   bool my_fts_sref_complete_p = true;
2709   try
2710     {
2711       string archive_extension;
2712       archive_classify (rps, archive_extension,
2713                         ps_upsert_buildids, ps_upsert_files,
2714                         ps_upsert_de, ps_upsert_sref, ps_upsert_sdef, // dalt
2715                         st.st_mtime,
2716                         my_fts_executable, my_fts_debuginfo, my_fts_sref, my_fts_sdef,
2717                         my_fts_sref_complete_p);
2718       add_metric ("scanned_bytes_total","source",archive_extension + " archive",
2719                   st.st_size);
2720       inc_metric ("scanned_files_total","source",archive_extension + " archive");
2721       add_metric("found_debuginfo_total","source",archive_extension + " archive",
2722                  my_fts_debuginfo);
2723       add_metric("found_executable_total","source",archive_extension + " archive",
2724                  my_fts_executable);
2725       add_metric("found_sourcerefs_total","source",archive_extension + " archive",
2726                  my_fts_sref);
2727     }
2728   catch (const reportable_exception& e)
2729     {
2730       e.report(clog);
2731     }
2732 
2733   if (verbose > 2)
2734     obatched(clog) << "scanned archive=" << rps
2735                    << " mtime=" << st.st_mtime
2736                    << " executables=" << my_fts_executable
2737                    << " debuginfos=" << my_fts_debuginfo
2738                    << " srefs=" << my_fts_sref
2739                    << " sdefs=" << my_fts_sdef
2740                    << endl;
2741 
2742   fts_executable += my_fts_executable;
2743   fts_debuginfo += my_fts_debuginfo;
2744   fts_sref += my_fts_sref;
2745   fts_sdef += my_fts_sdef;
2746 
2747   if (my_fts_sref_complete_p) // leave incomplete?
2748     ps_scan_done
2749       .reset()
2750       .bind(1, rps)
2751       .bind(2, st.st_mtime)
2752       .bind(3, st.st_size)
2753       .step_ok_done();
2754 }
2755 
2756 
2757 
2758 ////////////////////////////////////////////////////////////////////////
2759 
2760 
2761 
2762 // The thread that consumes file names off of the scanq.  We hold
2763 // the persistent sqlite_ps's at this level and delegate file/archive
2764 // scanning to other functions.
2765 static void*
thread_main_scanner(void * arg)2766 thread_main_scanner (void* arg)
2767 {
2768   (void) arg;
2769 
2770   // all the prepared statements fit to use, the _f_ set:
2771   sqlite_ps ps_f_upsert_buildids (db, "file-buildids-intern", "insert or ignore into " BUILDIDS "_buildids VALUES (NULL, ?);");
2772   sqlite_ps ps_f_upsert_files (db, "file-files-intern", "insert or ignore into " BUILDIDS "_files VALUES (NULL, ?);");
2773   sqlite_ps ps_f_upsert_de (db, "file-de-upsert",
2774                           "insert or ignore into " BUILDIDS "_f_de "
2775                           "(buildid, debuginfo_p, executable_p, file, mtime) "
2776                           "values ((select id from " BUILDIDS "_buildids where hex = ?),"
2777                           "        ?,?,"
2778                           "        (select id from " BUILDIDS "_files where name = ?), ?);");
2779   sqlite_ps ps_f_upsert_s (db, "file-s-upsert",
2780                          "insert or ignore into " BUILDIDS "_f_s "
2781                          "(buildid, artifactsrc, file, mtime) "
2782                          "values ((select id from " BUILDIDS "_buildids where hex = ?),"
2783                          "        (select id from " BUILDIDS "_files where name = ?),"
2784                          "        (select id from " BUILDIDS "_files where name = ?),"
2785                          "        ?);");
2786   sqlite_ps ps_f_query (db, "file-negativehit-find",
2787                         "select 1 from " BUILDIDS "_file_mtime_scanned where sourcetype = 'F' "
2788                         "and file = (select id from " BUILDIDS "_files where name = ?) and mtime = ?;");
2789   sqlite_ps ps_f_scan_done (db, "file-scanned",
2790                           "insert or ignore into " BUILDIDS "_file_mtime_scanned (sourcetype, file, mtime, size)"
2791                           "values ('F', (select id from " BUILDIDS "_files where name = ?), ?, ?);");
2792 
2793   // and now for the _r_ set
2794   sqlite_ps ps_r_upsert_buildids (db, "rpm-buildid-intern", "insert or ignore into " BUILDIDS "_buildids VALUES (NULL, ?);");
2795   sqlite_ps ps_r_upsert_files (db, "rpm-file-intern", "insert or ignore into " BUILDIDS "_files VALUES (NULL, ?);");
2796   sqlite_ps ps_r_upsert_de (db, "rpm-de-insert",
2797                           "insert or ignore into " BUILDIDS "_r_de (buildid, debuginfo_p, executable_p, file, mtime, content) values ("
2798                           "(select id from " BUILDIDS "_buildids where hex = ?), ?, ?, "
2799                           "(select id from " BUILDIDS "_files where name = ?), ?, "
2800                           "(select id from " BUILDIDS "_files where name = ?));");
2801   sqlite_ps ps_r_upsert_sref (db, "rpm-sref-insert",
2802                             "insert or ignore into " BUILDIDS "_r_sref (buildid, artifactsrc) values ("
2803                             "(select id from " BUILDIDS "_buildids where hex = ?), "
2804                             "(select id from " BUILDIDS "_files where name = ?));");
2805   sqlite_ps ps_r_upsert_sdef (db, "rpm-sdef-insert",
2806                             "insert or ignore into " BUILDIDS "_r_sdef (file, mtime, content) values ("
2807                             "(select id from " BUILDIDS "_files where name = ?), ?,"
2808                             "(select id from " BUILDIDS "_files where name = ?));");
2809   sqlite_ps ps_r_query (db, "rpm-negativehit-query",
2810                       "select 1 from " BUILDIDS "_file_mtime_scanned where "
2811                       "sourcetype = 'R' and file = (select id from " BUILDIDS "_files where name = ?) and mtime = ?;");
2812   sqlite_ps ps_r_scan_done (db, "rpm-scanned",
2813                           "insert or ignore into " BUILDIDS "_file_mtime_scanned (sourcetype, file, mtime, size)"
2814                           "values ('R', (select id from " BUILDIDS "_files where name = ?), ?, ?);");
2815 
2816 
2817   unsigned fts_cached = 0, fts_executable = 0, fts_debuginfo = 0, fts_sourcefiles = 0;
2818   unsigned fts_sref = 0, fts_sdef = 0;
2819 
2820   add_metric("thread_count", "role", "scan", 1);
2821   add_metric("thread_busy", "role", "scan", 1);
2822   while (! interrupted)
2823     {
2824       scan_payload p;
2825 
2826       add_metric("thread_busy", "role", "scan", -1);
2827       bool gotone = scanq.wait_front(p);
2828       add_metric("thread_busy", "role", "scan", 1);
2829 
2830       if (! gotone) continue; // go back to waiting
2831 
2832       try
2833         {
2834           bool scan_archive = false;
2835           for (auto&& arch : scan_archives)
2836             if (string_endswith(p.first, arch.first))
2837               scan_archive = true;
2838 
2839           if (scan_archive)
2840             scan_archive_file (p.first, p.second,
2841                                ps_r_upsert_buildids,
2842                                ps_r_upsert_files,
2843                                ps_r_upsert_de,
2844                                ps_r_upsert_sref,
2845                                ps_r_upsert_sdef,
2846                                ps_r_query,
2847                                ps_r_scan_done,
2848                                fts_cached,
2849                                fts_executable,
2850                                fts_debuginfo,
2851                                fts_sref,
2852                                fts_sdef);
2853 
2854           if (scan_files) // NB: maybe "else if" ?
2855             scan_source_file (p.first, p.second,
2856                               ps_f_upsert_buildids,
2857                               ps_f_upsert_files,
2858                               ps_f_upsert_de,
2859                               ps_f_upsert_s,
2860                               ps_f_query,
2861                               ps_f_scan_done,
2862                               fts_cached, fts_executable, fts_debuginfo, fts_sourcefiles);
2863         }
2864       catch (const reportable_exception& e)
2865         {
2866           e.report(cerr);
2867         }
2868 
2869       if (fts_cached || fts_executable || fts_debuginfo || fts_sourcefiles || fts_sref || fts_sdef)
2870         {} // NB: not just if a successful scan - we might have encountered -ENOSPC & failed
2871       (void) statfs_free_enough_p(db_path, "database"); // report sqlite filesystem size
2872       (void) statfs_free_enough_p(tmpdir, "tmpdir"); // this too, in case of fdcache/tmpfile usage
2873 
2874       // finished a scanning step -- not a "loop", because we just
2875       // consume the traversal loop's work, whenever
2876       inc_metric("thread_work_total","role","scan");
2877     }
2878 
2879 
2880   add_metric("thread_busy", "role", "scan", -1);
2881   return 0;
2882 }
2883 
2884 
2885 
2886 // The thread that traverses all the source_paths and enqueues all the
2887 // matching files into the file/archive scan queue.
2888 static void
scan_source_paths()2889 scan_source_paths()
2890 {
2891   // NB: fedora 31 glibc/fts(3) crashes inside fts_read() on empty
2892   // path list.
2893   if (source_paths.empty())
2894     return;
2895 
2896   // Turn the source_paths into an fts(3)-compatible char**.  Since
2897   // source_paths[] does not change after argv processing, the
2898   // c_str()'s are safe to keep around awile.
2899   vector<const char *> sps;
2900   for (auto&& sp: source_paths)
2901     sps.push_back(sp.c_str());
2902   sps.push_back(NULL);
2903 
2904   FTS *fts = fts_open ((char * const *)sps.data(),
2905                       (traverse_logical ? FTS_LOGICAL : FTS_PHYSICAL|FTS_XDEV)
2906                       | FTS_NOCHDIR /* multithreaded */,
2907                       NULL);
2908   if (fts == NULL)
2909     throw libc_exception(errno, "cannot fts_open");
2910   defer_dtor<FTS*,int> fts_cleanup (fts, fts_close);
2911 
2912   struct timespec ts_start, ts_end;
2913   clock_gettime (CLOCK_MONOTONIC, &ts_start);
2914   unsigned fts_scanned = 0, fts_regex = 0;
2915 
2916   FTSENT *f;
2917   while ((f = fts_read (fts)) != NULL)
2918   {
2919     if (interrupted) break;
2920 
2921     if (sigusr2 != forced_groom_count) // stop early if groom triggered
2922       {
2923         scanq.clear(); // clear previously issued work for scanner threads
2924         break;
2925       }
2926 
2927     fts_scanned ++;
2928 
2929     if (verbose > 2)
2930       obatched(clog) << "fts traversing " << f->fts_path << endl;
2931 
2932     switch (f->fts_info)
2933       {
2934       case FTS_F:
2935         {
2936           /* Found a file.  Convert it to an absolute path, so
2937              the buildid database does not have relative path
2938              names that are unresolvable from a subsequent run
2939              in a different cwd. */
2940           char *rp = realpath(f->fts_path, NULL);
2941           if (rp == NULL)
2942             continue; // ignore dangling symlink or such
2943           string rps = string(rp);
2944           free (rp);
2945 
2946           bool ri = !regexec (&file_include_regex, rps.c_str(), 0, 0, 0);
2947           bool rx = !regexec (&file_exclude_regex, rps.c_str(), 0, 0, 0);
2948           if (!ri || rx)
2949             {
2950               if (verbose > 3)
2951                 obatched(clog) << "fts skipped by regex "
2952                                << (!ri ? "I" : "") << (rx ? "X" : "") << endl;
2953               fts_regex ++;
2954               if (!ri)
2955                 inc_metric("traversed_total","type","file-skipped-I");
2956               if (rx)
2957                 inc_metric("traversed_total","type","file-skipped-X");
2958             }
2959           else
2960             {
2961               scanq.push_back (make_pair(rps, *f->fts_statp));
2962               inc_metric("traversed_total","type","file");
2963             }
2964         }
2965         break;
2966 
2967       case FTS_ERR:
2968       case FTS_NS:
2969         // report on some types of errors because they may reflect fixable misconfiguration
2970         {
2971           auto x = libc_exception(f->fts_errno, string("fts traversal ") + string(f->fts_path));
2972           x.report(cerr);
2973         }
2974         inc_metric("traversed_total","type","error");
2975         break;
2976 
2977       case FTS_SL: // ignore, but count because debuginfod -L would traverse these
2978         inc_metric("traversed_total","type","symlink");
2979         break;
2980 
2981       case FTS_D: // ignore
2982         inc_metric("traversed_total","type","directory");
2983         break;
2984 
2985       default: // ignore
2986         inc_metric("traversed_total","type","other");
2987         break;
2988       }
2989   }
2990   clock_gettime (CLOCK_MONOTONIC, &ts_end);
2991   double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
2992 
2993   obatched(clog) << "fts traversed source paths in " << deltas << "s, scanned=" << fts_scanned
2994                  << ", regex-skipped=" << fts_regex << endl;
2995 }
2996 
2997 
2998 static void*
thread_main_fts_source_paths(void * arg)2999 thread_main_fts_source_paths (void* arg)
3000 {
3001   (void) arg; // ignore; we operate on global data
3002 
3003   set_metric("thread_tid", "role","traverse", tid());
3004   add_metric("thread_count", "role", "traverse", 1);
3005 
3006   time_t last_rescan = 0;
3007 
3008   while (! interrupted)
3009     {
3010       sleep (1);
3011       scanq.wait_idle(); // don't start a new traversal while scanners haven't finished the job
3012       scanq.done_idle(); // release the hounds
3013       if (interrupted) break;
3014 
3015       time_t now = time(NULL);
3016       bool rescan_now = false;
3017       if (last_rescan == 0) // at least one initial rescan is documented even for -t0
3018         rescan_now = true;
3019       if (rescan_s > 0 && (long)now > (long)(last_rescan + rescan_s))
3020         rescan_now = true;
3021       if (sigusr1 != forced_rescan_count)
3022         {
3023           forced_rescan_count = sigusr1;
3024           rescan_now = true;
3025         }
3026       if (rescan_now)
3027         {
3028           set_metric("thread_busy", "role","traverse", 1);
3029           try
3030             {
3031               scan_source_paths();
3032             }
3033           catch (const reportable_exception& e)
3034             {
3035               e.report(cerr);
3036             }
3037           last_rescan = time(NULL); // NB: now was before scanning
3038           // finished a traversal loop
3039           inc_metric("thread_work_total", "role","traverse");
3040           set_metric("thread_busy", "role","traverse", 0);
3041         }
3042     }
3043 
3044   return 0;
3045 }
3046 
3047 
3048 
3049 ////////////////////////////////////////////////////////////////////////
3050 
3051 static void
database_stats_report()3052 database_stats_report()
3053 {
3054   sqlite_ps ps_query (db, "database-overview",
3055                       "select label,quantity from " BUILDIDS "_stats");
3056 
3057   obatched(clog) << "database record counts:" << endl;
3058   while (1)
3059     {
3060       if (interrupted) break;
3061       if (sigusr1 != forced_rescan_count) // stop early if scan triggered
3062         break;
3063 
3064       int rc = ps_query.step();
3065       if (rc == SQLITE_DONE) break;
3066       if (rc != SQLITE_ROW)
3067         throw sqlite_exception(rc, "step");
3068 
3069       obatched(clog)
3070         << right << setw(20) << ((const char*) sqlite3_column_text(ps_query, 0) ?: (const char*) "NULL")
3071         << " "
3072         << (sqlite3_column_text(ps_query, 1) ?: (const unsigned char*) "NULL")
3073         << endl;
3074 
3075       set_metric("groom", "statistic",
3076                  ((const char*) sqlite3_column_text(ps_query, 0) ?: (const char*) "NULL"),
3077                  (sqlite3_column_double(ps_query, 1)));
3078     }
3079 }
3080 
3081 
3082 // Do a round of database grooming that might take many minutes to run.
groom()3083 void groom()
3084 {
3085   obatched(clog) << "grooming database" << endl;
3086 
3087   struct timespec ts_start, ts_end;
3088   clock_gettime (CLOCK_MONOTONIC, &ts_start);
3089 
3090   database_stats_report();
3091 
3092   // scan for files that have disappeared
3093   sqlite_ps files (db, "check old files", "select s.mtime, s.file, f.name from "
3094                        BUILDIDS "_file_mtime_scanned s, " BUILDIDS "_files f "
3095                        "where f.id = s.file");
3096   sqlite_ps files_del_f_de (db, "nuke f_de", "delete from " BUILDIDS "_f_de where file = ? and mtime = ?");
3097   sqlite_ps files_del_r_de (db, "nuke r_de", "delete from " BUILDIDS "_r_de where file = ? and mtime = ?");
3098   sqlite_ps files_del_scan (db, "nuke f_m_s", "delete from " BUILDIDS "_file_mtime_scanned "
3099                             "where file = ? and mtime = ?");
3100   files.reset();
3101   while(1)
3102     {
3103       if (interrupted) break;
3104 
3105       int rc = files.step();
3106       if (rc != SQLITE_ROW)
3107         break;
3108 
3109       int64_t mtime = sqlite3_column_int64 (files, 0);
3110       int64_t fileid = sqlite3_column_int64 (files, 1);
3111       const char* filename = ((const char*) sqlite3_column_text (files, 2) ?: "");
3112       struct stat s;
3113       rc = stat(filename, &s);
3114       if (rc < 0 || (mtime != (int64_t) s.st_mtime))
3115         {
3116           if (verbose > 2)
3117             obatched(clog) << "groom: forgetting file=" << filename << " mtime=" << mtime << endl;
3118           files_del_f_de.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
3119           files_del_r_de.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
3120           files_del_scan.reset().bind(1,fileid).bind(2,mtime).step_ok_done();
3121           inc_metric("groomed_total", "decision", "stale");
3122         }
3123       else
3124         inc_metric("groomed_total", "decision", "fresh");
3125 
3126       if (sigusr1 != forced_rescan_count) // stop early if scan triggered
3127         break;
3128     }
3129   files.reset();
3130 
3131   // delete buildids with no references in _r_de or _f_de tables;
3132   // cascades to _r_sref & _f_s records
3133   sqlite_ps buildids_del (db, "nuke orphan buildids",
3134                           "delete from " BUILDIDS "_buildids "
3135                           "where not exists (select 1 from " BUILDIDS "_f_de d where " BUILDIDS "_buildids.id = d.buildid) "
3136                           "and not exists (select 1 from " BUILDIDS "_r_de d where " BUILDIDS "_buildids.id = d.buildid)");
3137   buildids_del.reset().step_ok_done();
3138 
3139   if (interrupted) return;
3140 
3141   // NB: "vacuum" is too heavy for even daily runs: it rewrites the entire db, so is done as maxigroom -G
3142   sqlite_ps g1 (db, "incremental vacuum", "pragma incremental_vacuum");
3143   g1.reset().step_ok_done();
3144   sqlite_ps g2 (db, "optimize", "pragma optimize");
3145   g2.reset().step_ok_done();
3146   sqlite_ps g3 (db, "wal checkpoint", "pragma wal_checkpoint=truncate");
3147   g3.reset().step_ok_done();
3148 
3149   database_stats_report();
3150 
3151   (void) statfs_free_enough_p(db_path, "database"); // report sqlite filesystem size
3152 
3153   sqlite3_db_release_memory(db); // shrink the process if possible
3154   sqlite3_db_release_memory(dbq); // ... for both connections
3155 
3156   fdcache.limit(0,0); // release the fdcache contents
3157   fdcache.limit(fdcache_fds,fdcache_mbs); // restore status quo parameters
3158 
3159   clock_gettime (CLOCK_MONOTONIC, &ts_end);
3160   double deltas = (ts_end.tv_sec - ts_start.tv_sec) + (ts_end.tv_nsec - ts_start.tv_nsec)/1.e9;
3161 
3162   obatched(clog) << "groomed database in " << deltas << "s" << endl;
3163 }
3164 
3165 
3166 static void*
thread_main_groom(void *)3167 thread_main_groom (void* /*arg*/)
3168 {
3169   set_metric("thread_tid", "role", "groom", tid());
3170   add_metric("thread_count", "role", "groom", 1);
3171 
3172   time_t last_groom = 0;
3173 
3174   while (1)
3175     {
3176       sleep (1);
3177       scanq.wait_idle(); // PR25394: block scanners during grooming!
3178       if (interrupted) break;
3179 
3180       time_t now = time(NULL);
3181       bool groom_now = false;
3182       if (last_groom == 0) // at least one initial groom is documented even for -g0
3183         groom_now = true;
3184       if (groom_s > 0 && (long)now > (long)(last_groom + groom_s))
3185         groom_now = true;
3186       if (sigusr2 != forced_groom_count)
3187         {
3188           forced_groom_count = sigusr2;
3189           groom_now = true;
3190         }
3191       if (groom_now)
3192         {
3193           set_metric("thread_busy", "role", "groom", 1);
3194           try
3195             {
3196               groom ();
3197             }
3198           catch (const sqlite_exception& e)
3199             {
3200               obatched(cerr) << e.message << endl;
3201             }
3202           last_groom = time(NULL); // NB: now was before grooming
3203           // finished a grooming loop
3204           inc_metric("thread_work_total", "role", "groom");
3205           set_metric("thread_busy", "role", "groom", 0);
3206         }
3207 
3208       scanq.done_idle();
3209     }
3210 
3211   return 0;
3212 }
3213 
3214 
3215 ////////////////////////////////////////////////////////////////////////
3216 
3217 
3218 static void
signal_handler(int)3219 signal_handler (int /* sig */)
3220 {
3221   interrupted ++;
3222 
3223   if (db)
3224     sqlite3_interrupt (db);
3225   if (dbq)
3226     sqlite3_interrupt (dbq);
3227 
3228   // NB: don't do anything else in here
3229 }
3230 
3231 static void
sigusr1_handler(int)3232 sigusr1_handler (int /* sig */)
3233 {
3234    sigusr1 ++;
3235   // NB: don't do anything else in here
3236 }
3237 
3238 static void
sigusr2_handler(int)3239 sigusr2_handler (int /* sig */)
3240 {
3241    sigusr2 ++;
3242   // NB: don't do anything else in here
3243 }
3244 
3245 
3246 
3247 
3248 
3249 // A user-defined sqlite function, to score the sharedness of the
3250 // prefix of two strings.  This is used to compare candidate debuginfo
3251 // / source-rpm names, so that the closest match
3252 // (directory-topology-wise closest) is found.  This is important in
3253 // case the same sref (source file name) is in many -debuginfo or
3254 // -debugsource RPMs, such as when multiple versions/releases of the
3255 // same package are in the database.
3256 
sqlite3_sharedprefix_fn(sqlite3_context * c,int argc,sqlite3_value ** argv)3257 static void sqlite3_sharedprefix_fn (sqlite3_context* c, int argc, sqlite3_value** argv)
3258 {
3259   if (argc != 2)
3260     sqlite3_result_error(c, "expect 2 string arguments", -1);
3261   else if ((sqlite3_value_type(argv[0]) != SQLITE_TEXT) ||
3262            (sqlite3_value_type(argv[1]) != SQLITE_TEXT))
3263     sqlite3_result_null(c);
3264   else
3265     {
3266       const unsigned char* a = sqlite3_value_text (argv[0]);
3267       const unsigned char* b = sqlite3_value_text (argv[1]);
3268       int i = 0;
3269       while (*a++ == *b++)
3270         i++;
3271       sqlite3_result_int (c, i);
3272     }
3273 }
3274 
3275 
3276 int
main(int argc,char * argv[])3277 main (int argc, char *argv[])
3278 {
3279   (void) setlocale (LC_ALL, "");
3280   (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
3281   (void) textdomain (PACKAGE_TARNAME);
3282 
3283   /* Tell the library which version we are expecting.  */
3284   elf_version (EV_CURRENT);
3285 
3286   tmpdir = string(getenv("TMPDIR") ?: "/tmp");
3287 
3288   /* Set computed default values. */
3289   db_path = string(getenv("HOME") ?: "/") + string("/.debuginfod.sqlite"); /* XDG? */
3290   int rc = regcomp (& file_include_regex, ".*", REG_EXTENDED|REG_NOSUB); // match everything
3291   if (rc != 0)
3292     error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
3293   rc = regcomp (& file_exclude_regex, "^$", REG_EXTENDED|REG_NOSUB); // match nothing
3294   if (rc != 0)
3295     error (EXIT_FAILURE, 0, "regcomp failure: %d", rc);
3296 
3297   // default parameters for fdcache are computed from system stats
3298   struct statfs sfs;
3299   rc = statfs(tmpdir.c_str(), &sfs);
3300   if (rc < 0)
3301     fdcache_mbs = 1024; // 1 gigabyte
3302   else
3303     fdcache_mbs = sfs.f_bavail * sfs.f_bsize / 1024 / 1024 / 4; // 25% of free space
3304   fdcache_mintmp = 25; // emergency flush at 25% remaining (75% full)
3305   fdcache_prefetch = 64; // guesstimate storage is this much less costly than re-decompression
3306   fdcache_fds = (concurrency + fdcache_prefetch) * 2;
3307 
3308   /* Parse and process arguments.  */
3309   int remaining;
3310   argp_program_version_hook = print_version; // this works
3311   (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
3312   if (remaining != argc)
3313       error (EXIT_FAILURE, 0,
3314              "unexpected argument: %s", argv[remaining]);
3315 
3316   if (scan_archives.size()==0 && !scan_files && source_paths.size()>0)
3317     obatched(clog) << "warning: without -F -R -U -Z, ignoring PATHs" << endl;
3318 
3319   fdcache.limit(fdcache_fds, fdcache_mbs);
3320 
3321   (void) signal (SIGPIPE, SIG_IGN); // microhttpd can generate it incidentally, ignore
3322   (void) signal (SIGINT, signal_handler); // ^C
3323   (void) signal (SIGHUP, signal_handler); // EOF
3324   (void) signal (SIGTERM, signal_handler); // systemd
3325   (void) signal (SIGUSR1, sigusr1_handler); // end-user
3326   (void) signal (SIGUSR2, sigusr2_handler); // end-user
3327 
3328   /* Get database ready. */
3329   rc = sqlite3_open_v2 (db_path.c_str(), &db, (SQLITE_OPEN_READWRITE
3330                                                |SQLITE_OPEN_URI
3331                                                |SQLITE_OPEN_PRIVATECACHE
3332                                                |SQLITE_OPEN_CREATE
3333                                                |SQLITE_OPEN_FULLMUTEX), /* thread-safe */
3334                         NULL);
3335   if (rc == SQLITE_CORRUPT)
3336     {
3337       (void) unlink (db_path.c_str());
3338       error (EXIT_FAILURE, 0,
3339              "cannot open %s, deleted database: %s", db_path.c_str(), sqlite3_errmsg(db));
3340     }
3341   else if (rc)
3342     {
3343       error (EXIT_FAILURE, 0,
3344              "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(db));
3345     }
3346 
3347   // open the readonly query variant
3348   // NB: PRIVATECACHE allows web queries to operate in parallel with
3349   // much other grooming/scanning operation.
3350   rc = sqlite3_open_v2 (db_path.c_str(), &dbq, (SQLITE_OPEN_READONLY
3351                                                 |SQLITE_OPEN_URI
3352                                                 |SQLITE_OPEN_PRIVATECACHE
3353                                                 |SQLITE_OPEN_FULLMUTEX), /* thread-safe */
3354                         NULL);
3355   if (rc)
3356     {
3357       error (EXIT_FAILURE, 0,
3358              "cannot open %s, consider deleting database: %s", db_path.c_str(), sqlite3_errmsg(dbq));
3359     }
3360 
3361 
3362   obatched(clog) << "opened database " << db_path << endl;
3363   obatched(clog) << "sqlite version " << sqlite3_version << endl;
3364 
3365   // add special string-prefix-similarity function used in rpm sref/sdef resolution
3366   rc = sqlite3_create_function(dbq, "sharedprefix", 2, SQLITE_UTF8, NULL,
3367                                & sqlite3_sharedprefix_fn, NULL, NULL);
3368   if (rc != SQLITE_OK)
3369     error (EXIT_FAILURE, 0,
3370            "cannot create sharedprefix function: %s", sqlite3_errmsg(dbq));
3371 
3372   if (verbose > 3)
3373     obatched(clog) << "ddl: " << DEBUGINFOD_SQLITE_DDL << endl;
3374   rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_DDL, NULL, NULL, NULL);
3375   if (rc != SQLITE_OK)
3376     {
3377       error (EXIT_FAILURE, 0,
3378              "cannot run database schema ddl: %s", sqlite3_errmsg(db));
3379     }
3380 
3381   // Start httpd server threads.  Separate pool for IPv4 and IPv6, in
3382   // case the host only has one protocol stack.
3383   MHD_Daemon *d4 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
3384 #if MHD_VERSION >= 0x00095300
3385                                      | MHD_USE_INTERNAL_POLLING_THREAD
3386 #else
3387                                      | MHD_USE_SELECT_INTERNALLY
3388 #endif
3389                                      | MHD_USE_DEBUG, /* report errors to stderr */
3390                                      http_port,
3391                                      NULL, NULL, /* default accept policy */
3392                                      handler_cb, NULL, /* handler callback */
3393                                      MHD_OPTION_END);
3394   MHD_Daemon *d6 = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
3395 #if MHD_VERSION >= 0x00095300
3396                                      | MHD_USE_INTERNAL_POLLING_THREAD
3397 #else
3398                                      | MHD_USE_SELECT_INTERNALLY
3399 #endif
3400                                      | MHD_USE_IPv6
3401                                      | MHD_USE_DEBUG, /* report errors to stderr */
3402                                      http_port,
3403                                      NULL, NULL, /* default accept policy */
3404                                      handler_cb, NULL, /* handler callback */
3405                                      MHD_OPTION_END);
3406 
3407   if (d4 == NULL && d6 == NULL) // neither ipv4 nor ipv6? boo
3408     {
3409       sqlite3 *database = db;
3410       sqlite3 *databaseq = dbq;
3411       db = dbq = 0; // for signal_handler not to freak
3412       sqlite3_close (databaseq);
3413       sqlite3_close (database);
3414       error (EXIT_FAILURE, 0, "cannot start http server at port %d", http_port);
3415     }
3416 
3417   obatched(clog) << "started http server on "
3418                  << (d4 != NULL ? "IPv4 " : "")
3419                  << (d6 != NULL ? "IPv6 " : "")
3420                  << "port=" << http_port << endl;
3421 
3422   // add maxigroom sql if -G given
3423   if (maxigroom)
3424     {
3425       obatched(clog) << "maxigrooming database, please wait." << endl;
3426       extra_ddl.push_back("create index if not exists " BUILDIDS "_r_sref_arc on " BUILDIDS "_r_sref(artifactsrc);");
3427       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);");
3428       extra_ddl.push_back("drop index if exists " BUILDIDS "_r_sref_arc;");
3429 
3430       // NB: we don't maxigroom the _files interning table.  It'd require a temp index on all the
3431       // tables that have file foreign-keys, which is a lot.
3432 
3433       // NB: with =delete, may take up 3x disk space total during vacuum process
3434       //     vs.  =off (only 2x but may corrupt database if program dies mid-vacuum)
3435       //     vs.  =wal (>3x observed, but safe)
3436       extra_ddl.push_back("pragma journal_mode=delete;");
3437       extra_ddl.push_back("vacuum;");
3438       extra_ddl.push_back("pragma journal_mode=wal;");
3439     }
3440 
3441   // run extra -D sql if given
3442   for (auto&& i: extra_ddl)
3443     {
3444       if (verbose > 1)
3445         obatched(clog) << "extra ddl:\n" << i << endl;
3446       rc = sqlite3_exec (db, i.c_str(), NULL, NULL, NULL);
3447       if (rc != SQLITE_OK && rc != SQLITE_DONE && rc != SQLITE_ROW)
3448         error (0, 0,
3449                "warning: cannot run database extra ddl %s: %s", i.c_str(), sqlite3_errmsg(db));
3450     }
3451 
3452   if (maxigroom)
3453     obatched(clog) << "maxigroomed database" << endl;
3454 
3455   obatched(clog) << "search concurrency " << concurrency << endl;
3456   obatched(clog) << "rescan time " << rescan_s << endl;
3457   obatched(clog) << "fdcache fds " << fdcache_fds << endl;
3458   obatched(clog) << "fdcache mbs " << fdcache_mbs << endl;
3459   obatched(clog) << "fdcache prefetch " << fdcache_prefetch << endl;
3460   obatched(clog) << "fdcache tmpdir " << tmpdir << endl;
3461   obatched(clog) << "fdcache tmpdir min% " << fdcache_mintmp << endl;
3462   obatched(clog) << "groom time " << groom_s << endl;
3463   if (scan_archives.size()>0)
3464     {
3465       obatched ob(clog);
3466       auto& o = ob << "scanning archive types ";
3467       for (auto&& arch : scan_archives)
3468 	o << arch.first << "(" << arch.second << ") ";
3469       o << endl;
3470     }
3471   const char* du = getenv(DEBUGINFOD_URLS_ENV_VAR);
3472   if (du && du[0] != '\0') // set to non-empty string?
3473     obatched(clog) << "upstream debuginfod servers: " << du << endl;
3474 
3475   vector<pthread_t> all_threads;
3476 
3477   pthread_t pt;
3478   rc = pthread_create (& pt, NULL, thread_main_groom, NULL);
3479   if (rc)
3480     error (EXIT_FAILURE, rc, "cannot spawn thread to groom database\n");
3481   else
3482     all_threads.push_back(pt);
3483 
3484   if (scan_files || scan_archives.size() > 0)
3485     {
3486       rc = pthread_create (& pt, NULL, thread_main_fts_source_paths, NULL);
3487       if (rc)
3488         error (EXIT_FAILURE, rc, "cannot spawn thread to traverse source paths\n");
3489       all_threads.push_back(pt);
3490       for (unsigned i=0; i<concurrency; i++)
3491         {
3492           rc = pthread_create (& pt, NULL, thread_main_scanner, NULL);
3493           if (rc)
3494             error (EXIT_FAILURE, rc, "cannot spawn thread to scan source files / archives\n");
3495           all_threads.push_back(pt);
3496         }
3497     }
3498 
3499   /* Trivial main loop! */
3500   set_metric("ready", 1);
3501   while (! interrupted)
3502     pause ();
3503   scanq.nuke(); // wake up any remaining scanq-related threads, let them die
3504   set_metric("ready", 0);
3505 
3506   if (verbose)
3507     obatched(clog) << "stopping" << endl;
3508 
3509   /* Join all our threads. */
3510   for (auto&& it : all_threads)
3511     pthread_join (it, NULL);
3512 
3513   /* Stop all the web service threads. */
3514   if (d4) MHD_stop_daemon (d4);
3515   if (d6) MHD_stop_daemon (d6);
3516 
3517   /* With all threads known dead, we can clean up the global resources. */
3518   rc = sqlite3_exec (db, DEBUGINFOD_SQLITE_CLEANUP_DDL, NULL, NULL, NULL);
3519   if (rc != SQLITE_OK)
3520     {
3521       error (0, 0,
3522              "warning: cannot run database cleanup ddl: %s", sqlite3_errmsg(db));
3523     }
3524 
3525   // NB: no problem with unconditional free here - an earlier failed regcomp would exit program
3526   (void) regfree (& file_include_regex);
3527   (void) regfree (& file_exclude_regex);
3528 
3529   sqlite3 *database = db;
3530   sqlite3 *databaseq = dbq;
3531   db = dbq = 0; // for signal_handler not to freak
3532   (void) sqlite3_close (databaseq);
3533   (void) sqlite3_close (database);
3534 
3535   return 0;
3536 }
3537