File: api.cxx

package info (click to toggle)
systemtap 5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,556 kB
  • sloc: cpp: 81,117; ansic: 54,933; xml: 49,795; exp: 43,595; sh: 11,526; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (1066 lines) | stat: -rw-r--r-- 29,517 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
// systemtap compile-server web api server
// Copyright (C) 2017-2018 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "api.h"
#include "server.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include "../staputil.h"
#include "backends.h"
#include "../cmdline.h"
#include "utils.h"
#include "../nsscommon.h"
#include "../privilege.h"

extern "C" {
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glob.h>
#include <sched.h>
#include <limits.h>
}

using namespace std;

static server *httpd = NULL;

struct result_file_info
{
    string path;
    mode_t mode;
};

class resource
{
public:
    resource(string resource_base) {
	uuid_str = get_uuid();
	uri = resource_base + uuid_str;
    }

    virtual ~resource() { }

    string get_uuid_str() {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(res_mutex);
	return uuid_str;
    }

    string get_uri() {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(res_mutex);
	return uri;
    }

    virtual void generate_response(response &r) = 0;
    virtual void generate_file_response(response &r, string &) { 
	r = get_404_response();
    }

protected:
    mutex res_mutex;
    string uuid_str;
    string uri;
};

class result_info : public resource
{
public:
    result_info(int rc, string &out_path, string &err_path)
	: resource("/results/"), rc(rc), stdout_path(out_path),
	  stderr_path(err_path), status_code(0)
    {
	size_t found = stdout_path.find_last_of("/");
	if (found != string::npos) {
	    stdout_file = stdout_path.substr(found + 1);
	}
	found = stderr_path.find_last_of("/");
	if (found != string::npos) {
	    stderr_file = stderr_path.substr(found + 1);
	}
    }
    result_info(unsigned int status_code, string content)
	: resource("/results/"), rc(0), status_code(status_code),
	  content(content)
    {
    }

    ~result_info()
    {
	if (!files.empty()) {
	    for (auto it = files.begin(); it != files.end(); it++) {
		delete it->second;
	    }
	    files.clear();
	}
    }

    void generate_response(response &r);
    void generate_file_response(response &r, string &f);

    void add_file(string &path, mode_t mode)
    {
	size_t found = path.find_last_of("/");
	string file_name;

	if (found != string::npos) {
	    file_name = path.substr(found + 1);
	}
	else {
	    file_name = path;
	}
	struct result_file_info *rfi = new struct result_file_info;
	rfi->path = path;
	rfi->mode = mode;
	files[file_name] = rfi;
    }

protected:
    int rc;
    string stdout_path;
    string stdout_file;
    string stderr_path;
    string stderr_file;
    map<string, struct result_file_info *> files;

    unsigned int status_code;
    string content;
};

static void result_infos_erase(result_info *r);

class build_info : public resource
{
public:
    build_info(client_request_data *crd)
	: resource("/builds/"), crd(crd), builder_thread_running(false),
	  result(NULL) { }

    ~build_info()
    {
	if (builder_thread_running) {
	    pthread_join(builder_tid, NULL);
	    builder_thread_running = false;
	}
	if (result) {
	    // If this build has an associated result, be sure to delete it
	    // from the results list.
	    result_infos_erase(result);
	    delete result;
	    result = NULL;
	}
	if (crd) {
	    delete crd;
	    crd = NULL;
	}
    }

    void generate_response(response &r);
    void start_module_build();

    bool is_build_finished()
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(res_mutex);
	return (result != NULL);
    }

private:
    client_request_data *crd;

    bool builder_thread_running;
    pthread_t builder_tid;

    void parse_cmd_args(void);
    static void *module_build_shim(void *arg);
    void *module_build();
    result_info *result;

    void set_result(result_info *ri);
};

void result_info::generate_response(response &r)
{
    ostringstream os;

    if (status_code != 0) {
	r.status_code = status_code;
	r.content = content;
	return;
    }

    r.status_code = 200;
    r.content_type = "application/json";
    os << "{" << endl;
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(res_mutex);

	os << "  \"version\": \"" VERSION "\"";
	os << "," << endl << "  \"uuid\": \"" << uuid_str << "\"";
	os << "," << endl << "  \"rc\": " << rc;

	// Always output stdout and stderr.
	if (!stdout_path.empty()) {
	    os << "," << endl << "  \"stdout_location\": \""
	       << uri + '/' + stdout_file << "\"";
	}
	if (!stderr_path.empty()) {
	    os << "," << endl << "  \"stderr_location\": \""
	       << uri + '/' + stderr_file << "\"";
	}

	// Here we output any extra files, like a module. For each
	// file print the location and mode (in decimal, since JSON
	// doesn't do octal).
	if (!files.empty()) {
	    os << "," << endl << "  \"files\": [" << endl;
	    bool first = true;
	    for (auto it = files.begin(); it != files.end(); it++) {
		if (!first)
		    os << "," << endl;
		else
		    first = false;
		os << "    { \"location\": \""
		   << uri + '/' + it->first
		   << "\", \"mode\": " << it->second->mode << " }";
	    }
	    os << endl << "  ]";
	}
    }
    os << endl << "}" << endl;
    r.content = os.str();
}

void result_info::generate_file_response(response &r, string &file)
{
    // We don't want to serve any old file (which would be a security
    // hole), only the files we told the user about.
    server_error(_F("Trying to retrieve file '%s'", file.c_str()));
    string path;
    r.status_code = 200;
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(res_mutex);

	if (!stdout_path.empty() && file == stdout_file) {
	    path = stdout_path;
	}
	else if (!stderr_path.empty() && file == stderr_file) {
	    path = stderr_path;
	}
	else if (!files.empty()) {
	    auto it = files.find(file);
	    if (it != files.end()) {
		path = it->second->path;
	    }
	}
    }

    if (!path.empty()) {
	server_error(_F("File requested:  %s", file.c_str()));
	server_error(_F("Served from   :  %s", path.c_str()));
	r.file = path;
    }
    else {
	server_error("Couldn't find file");
	r = get_404_response();
    }
}

void build_info::generate_response(response &r)
{
    ostringstream os;

    r.content_type = "application/json";
    os << "{" << endl;
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(res_mutex);

	if (result == NULL) {
	    r.status_code = 200;
	}
	else {
	    r.status_code = 303;
	    r.headers["Location"] = result->get_uri();
	}

	os << "  \"version\": \"" VERSION "\"," << endl;
	os << "  \"uuid\": \"" << uuid_str << "\"," << endl;
	os << "  \"kver\": \"" << crd->kver << "\"," << endl;
	os << "  \"arch\": \"" << crd->arch << "\"," << endl;

	os << "  \"cmd_args\": [" << endl;
	bool first = true;
	for (auto it = crd->cmd_args.begin(); it != crd->cmd_args.end();
	     it++) {
	    struct json_object *j = json_object_new_string((*it).c_str());
	    if (j) {
		if (!first)
		    os << "," << endl;
		else
		    first = false;
		os << "    "
		   << json_object_to_json_string_ext(j, JSON_C_TO_STRING_PLAIN);
		json_object_put(j);
	    }
	}
	os << endl << "  ]" << endl;
    }
    os << "}" << endl;
    r.content = os.str();
}

void build_info::start_module_build()
{
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(res_mutex);
	if (builder_thread_running) {
	    // This really shouldn't happen. Error out.
	    server_error("Multiple attempts to build module.");
	    return;
	}
	builder_thread_running = true;
    }

    /* Create a thread to handle the module build. */
    if (pthread_create(&builder_tid, NULL, module_build_shim, this) < 0) {
	server_error(_F("Failed to create thread: %s", strerror(errno)));
	exit(1);
    }
}


mutex builds_mutex;
vector<build_info *> build_infos;

mutex results_mutex;
vector<result_info *> result_infos;

static void
result_infos_erase(result_info *r)
{
    // Use a lock_guard to ensure the mutex gets released
    // even if an exception is thrown.
    lock_guard<mutex> lock(results_mutex);
    for (auto it = result_infos.begin(); it != result_infos.end(); it++) {
	if (r->get_uuid_str() == (*it)->get_uuid_str()) {
	    result_infos.erase(it);
	    break;
	}
    }
}


class build_collection_rh : public request_handler
{
public:
    response POST(const request &req);

    build_collection_rh(string n) : request_handler(n) {}
};

response build_collection_rh::POST(const request &req)
{
    client_request_data *crd = new client_request_data;
    if (crd == NULL) {
	// Return an error.
	server_error("500 - internal server error");
	response error500(500);
	error500.content = "<h1>Internal server error, memory allocation failed.</h1>";
	return error500;
    }

    // Gather up the info we need.
    vector<string> file_name;
    vector<string> build_id;
    vector<string> file_pkg;
    for (auto it = req.params.begin(); it != req.params.end(); it++) {
	if (it->first == "kver") {
	    crd->kver = it->second[0];
	}
	else if (it->first == "arch") {
	    crd->arch = it->second[0];
	}
	else if (it->first == "cmd_args") {
	    crd->cmd_args = it->second;
	}
	else if (it->first == "distro_name") {
	    // Notice we're lowercasing the distro name to make things
	    // simpler.
	    crd->distro_name = it->second[0];
	    transform(crd->distro_name.begin(), crd->distro_name.end(),
		      crd->distro_name.begin(), ::tolower);

	}
	else if (it->first == "distro_version") {
	    crd->distro_version = it->second[0];
	}
	else if (it->first == "build_id") {
	    build_id = it->second;
	}
	else if (it->first == "file_name") {
	    file_name = it->second;
	}
	else if (it->first == "file_pkg") {
	    file_pkg = it->second;
	}
	else if (it->first == "env_vars") {
	    // Get rid of a few standard environment variables (which
	    // might cause us to do unintended things) from the list
	    // the client sent us.
	    for (auto it2 = it->second.begin(); it2 != it->second.end();
		 it2++) {
		if (*it2 == "IFS" || *it2 == "CDPATH" || *it2 == "ENV"
		    || *it2 == "BASH_ENV") {
		    server_error(_F("ignoring client environment variable: %s",
				    (*it2).c_str()));
		}
		else {
		    crd->env_vars.push_back(*it2);
		}
	    }
	}
	// Notice we silently ignore any "extra" parameters.
    }

    // Combine the file info fields.
    if (! file_name.empty() || ! build_id.empty() || ! file_pkg.empty()) {
	if (file_name.size() != build_id.size()
	    || file_name.size() != file_pkg.size()) {
	    // Return an error.
	    server_error("400 - bad request (1)");
	    response error400(400);
	    error400.content = "<h1>Bad request</h1>";
	    return error400;
	}
	for (unsigned i = 0; i < file_name.size(); ++i) {
	    auto finfo = make_shared<struct file_info>();
	    finfo->name = file_name[i];
	    finfo->pkg = file_pkg[i];
	    finfo->build_id = build_id[i];
	    crd->file_info.push_back(finfo);
	}
    }

    // We've got 2 directories that we use:
    //   server_dir: the directory for "server" files - stuff received
    //               from the client, files generated on the server
    //               (like stdout/stderr files)
    //   client_dir: directory for "client" files, file to be operated
    //               on (or generated by) by systemtap itself
    // We need to create the client_dir.
    crd->server_dir = req.base_dir;
    if (crd->server_dir.empty() && !make_temp_dir(crd->server_dir)) {
	response error500(500);
	error500.content = "<h1>Internal server error, mkdtemp failed.</h1>";
	return error500;
    }
    if (!make_temp_dir(crd->client_dir)) {
	response error500(500);
	error500.content = "<h1>Internal server error, mkdtemp failed.</h1>";
	return error500;
    }

    if (! req.files.empty()) {
	server_error("Files received:");
	for (auto i = req.files.begin(); i != req.files.end(); i++) {
	    for (auto j = i->second.begin(); j != i->second.end();
		 j++) {
		server_error(*j);
		crd->files.push_back(*j);
	    }
	}
    }

    // Make sure we've got everything we need.
    if (crd->kver.empty() || crd->arch.empty() || crd->cmd_args.empty()
	|| crd->distro_name.empty() || crd->distro_version.empty()) {
	// Return an error.
	server_error("400 - bad request (2)");
	response error400(400);
	error400.content = "<h1>Bad request</h1>";
	return error400;
    }

    // Create a build with the information we've gathered.
    build_info *b = new build_info(crd);
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(builds_mutex);
	build_infos.push_back(b);
    }

    // Kick off the module build.
    b->start_module_build();

    // Return a 202 response.
    server_error("Returning a 202");
    response resp(202);
    resp.headers["Location"] = b->get_uri();
    resp.headers["Retry-After"] = "10";
    return resp;
}

class individual_build_rh : public request_handler
{
public:
    individual_build_rh(string n) : request_handler(n) {}

    response GET(const request &req);
    response DELETE(const request &req);
};

response individual_build_rh::GET(const request &req)
{
    // matches[0] is the entire string '/builds/XXXX'. matches[1] is
    // just the buildid 'XXXX'.
    string buildid = req.matches[1];
    build_info *b = NULL;
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(builds_mutex);
	for (auto it = build_infos.begin(); it != build_infos.end(); it++) {
	    if (buildid == (*it)->get_uuid_str()) {
		b = *it;
		break;
	    }
	}
    }

    if (b == NULL) {
	server_error(_F("Couldn't find build '%s'", buildid.c_str()));
	return get_404_response();
    }

    response rsp(0);
    b->generate_response(rsp);
    return rsp;
}

response individual_build_rh::DELETE(const request &req)
{
    // matches[0] is the entire string '/builds/XXXX'. matches[1] is
    // just the buildid 'XXXX'.
    string buildid = req.matches[1];
    build_info *b = NULL;
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(builds_mutex);
	for (auto it = build_infos.begin(); it != build_infos.end(); it++) {
	    if (buildid == (*it)->get_uuid_str()) {
		b = *it;
		build_infos.erase(it);
		break;
	    }
	}
    }

    if (b == NULL) {
	server_error(_F("Couldn't find build '%s'", buildid.c_str()));
	return get_404_response();
    }

    // At this point we've found a matching build. Delete it.
    delete b;
    response rsp(300);
    rsp.content = "";
    return rsp;
}

class individual_result_rh : public request_handler
{
public:
    response GET(const request &req);

    individual_result_rh(string n) : request_handler(n) {}
};

response individual_result_rh::GET(const request &req)
{
    // matches[0] is the entire string '/results/XXXX'. matches[1] is
    // just the id_str 'XXXX'.
    string id_str = req.matches[1];
    result_info *ri = NULL;
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(results_mutex);
	for (auto it = result_infos.begin(); it != result_infos.end(); it++) {
	    if (id_str == (*it)->get_uuid_str()) {
		ri = *it;
		break;
	    }
	}
    }

    if (ri == NULL) {
	server_error(_F("Couldn't find result id '%s'", id_str.c_str()));
	return get_404_response();
    }

    response rsp(0);
    ri->generate_response(rsp);
    return rsp;
}

class result_file_rh : public request_handler
{
public:
    response GET(const request &req);

    result_file_rh(string n) : request_handler(n) {}
};

response result_file_rh::GET(const request &req)
{
    // matches[0] is the entire string
    // '/results/XXXX/FILE'. matches[1] is the result uuid string
    // 'XXXX'. matches[2] is the filename 'FILE'.
    string id_str = req.matches[1];
    string file_str = req.matches[2];
    result_info *ri = NULL;
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(results_mutex);
	for (auto it = result_infos.begin(); it != result_infos.end(); it++) {
	    if (id_str == (*it)->get_uuid_str()) {
		ri = *it;
		break;
	    }
	}
    }

    if (ri == NULL) {
	server_error(_F("Couldn't find result id '%s'", id_str.c_str()));
	return get_404_response();
    }

    response rsp(0);
    ri->generate_file_response(rsp, file_str);
    return rsp;
}

build_collection_rh builds_rh("build collection");
individual_build_rh build_rh("individual build");
individual_result_rh result_rh("individual result");
result_file_rh result_file_rh("result file");

void
build_info::set_result(result_info *ri)
{
    {
	// Use a lock_guard to ensure the mutex gets released
	// even if an exception is thrown.
	lock_guard<mutex> lock(res_mutex);
	result = ri;
    }
    {
	// Use a lock_guard to ensure the mutex gets released
	// even if an exception is thrown.
	lock_guard<mutex> lock(results_mutex);
	result_infos.push_back(ri);
    }
}

void *
build_info::module_build_shim(void *arg)
{
    build_info *bi = static_cast<build_info *>(arg);

    return bi->module_build();
}

void
build_info::parse_cmd_args(void)
{
    // Here we parse the stap command line for anything
    // interesting. Note that we're not parsing the httpd command
    // line, but the stap command line the user entered on the client
    // side.
    //
    // Also note that we need not do any options consistency checking
    // since our spawned stap instance will do that.

    // Create an argv/argc for use by getopt_long. Note that we have
    // to add an argument 0 (for the 'stap' command itself) to make
    // getopt_long() happy.
    unsigned argc = crd->cmd_args.size() + 1;
    char **argv = new char *[argc + 1];
    char arg0[] = "stap";
    argv[0] = arg0;
    for (unsigned i = 0; i < crd->cmd_args.size(); ++i) {
	argv[i + 1] = (char *)crd->cmd_args[i].c_str();
    }
    argv[argc] = NULL;

    optind = 1;
    unsigned perpass_verbose[5] = { 0 };
    unsigned verbose = 0;
    privilege_t privilege = pr_highest; // Until specified otherwise.
    while (true) {
	int grc = getopt_long(argc, argv, STAP_SHORT_OPTIONS,
			      stap_long_options, NULL);
	if (grc < 0)
	    break;
	switch (grc) {
        case 'v':
	    for (unsigned i = 0; i < 5; i++)
		perpass_verbose[i]++;
	    verbose++;
	    break;
	case LONG_OPT_VERBOSE_PASS:
            assert(optarg);
	    if (strlen(optarg) > 0 && strlen(optarg) <= 5) {
		for (unsigned i = 0; i < strlen(optarg); i++) {
		    if (isdigit(optarg[i]))
			perpass_verbose[i] += (optarg[i] - '0');
		}
	    }
	    break;
	case LONG_OPT_PRIVILEGE:
	    if (strcmp(optarg, "stapdev") == 0)
		privilege = pr_stapdev;
	    else if (strcmp(optarg, "stapsys") == 0)
		privilege = pr_stapsys;
	    else if (strcmp(optarg, "stapusr") == 0)
		privilege = pr_stapusr;
	    else {
		// FIXME: what to do here?
		server_error(_F("Invalid argument '%s' for --privilege",
				optarg));
		privilege = pr_highest;
	    }
	    break;
	case LONG_OPT_UNPRIVILEGED:
	    privilege = pr_unprivileged;
	    break;
	default:
	    // We silently ignore all options we aren't interested in.
	    break;
	}
    }
    delete[] argv;

    // Now that we've finished parsing the arguments, we'll take the
    // pass 2 verbose level as the level of verbosity to report things
    // back to the client.
    crd->verbose = perpass_verbose[1];
    server_error(_F("Verbose level: %d", crd->verbose));
    crd->privilege = privilege;
    server_error(_F("Privilege: %d", crd->privilege));
}

void *
build_info::module_build()
{
    vector<string> argv;
    bool client_zip_exists = false;

    // The client can optionally send over a "client.zip" file, which
    // we automatically unzip here.
    for (auto i = crd->files.begin(); i != crd->files.end(); i++) {
	if (*i == "client.zip") {
	    string zip_path = crd->server_dir + "/client.zip";
	    vector<string> zip_argv = { "unzip", "-q", "-d", crd->client_dir,
					zip_path };
	    int rc = stap_system (2, zip_argv);
	    if (rc != 0) {
		// Return an error.
		server_error(_F("unzip failed: %d", rc));
		result_info *ri = new result_info(400,
						  "<h1>Bad request</h1>");
		set_result(ri);
		return NULL;
	    }
	    client_zip_exists = true;
	}
    }

    // Process the command arguments.
    argv.push_back("stap");

    // Specify the right kernel version.  Since we might be using a
    // '--sysroot' option, we need to specify the full path to the
    // kernel build directory (or stap will look for it in the
    // sysroot).
    argv.push_back("-r");
    string kernel_build_tree = "/lib/modules/" + crd->kver + "/build";
    argv.push_back(kernel_build_tree);

    if (client_zip_exists) {
	// If a directory called "files" exists in the stuff we
	// unzipped from client.zip, then we need a sysroot option (so
	// that stap can find the target user executables).
	struct stat stbuf;
	string files_path = crd->client_dir + "/files";
	if (stat(files_path.c_str(), &stbuf) == 0) {
	    argv.push_back("--sysroot=" + files_path);
	}
    }

    // Make sure stap knows where to put the results.
    argv.push_back(string("--tmpdir=") + crd->client_dir);

    // Add the "client options" argument, which tells stap to do some
    // extra command line validation and to stop at pass 4.
    argv.push_back("--client-options");

    // Add the rest of the client's arguments.
    for (auto it = crd->cmd_args.begin(); it != crd->cmd_args.end(); it++) {
	argv.push_back(*it);
    }

    // We need to do a chdir() to the client directory. However, all
    // threads in a process share the same root directory and working
    // directory. If we just do a chdir() here and then call spawn, it
    // is possible that a different thread is also here and does a
    // chdir() right after ours but before the spawn. So, instead
    // we'll call unshare() first to "unshare" the thread's working
    // directory. Then when we do a chdir(), it won't affect the other
    // threads working directory.
    if (unshare(CLONE_FS) < 0) {
	// Return an error.
	server_error(_F("Error in unshare: %s", strerror(errno)));
	result_info *ri = new result_info(500,
					  "<h1>Internal server error, unshare failed.</h1>");
	set_result(ri);
	return NULL;
    }
    if (chdir(crd->client_dir.c_str()) < 0) {
	// Return an error.
	server_error(_F("Error in chdir: %s", strerror(errno)));
	result_info *ri = new result_info(500,
					  "<h1>Internal server error, chdir failed.</h1>");
	set_result(ri);
	return NULL;
    }

    // Parse the client's command args.
    parse_cmd_args();

    // Create empty stdout/stderr files, so they always exist.
    string stdout_path = crd->server_dir + "/stdout";
    ofstream file;
    file.open(stdout_path, ios::out);
    file.close();
    string stderr_path = crd->server_dir + "/stderr";
    file.open(stderr_path, ios::out);
    file.close();

    int staprc = -1;
    vector<backend_base *> backends;
    get_backends(backends);
    bool backend_found = false;
    for (auto it = backends.begin(); it != backends.end(); it++) {
	if ((*it)->can_generate_module(crd)) {
	    backend_found = true;
	    staprc = (*it)->generate_module(crd, argv, get_uuid_str(),
					    stdout_path, stderr_path);
	    break;
	}
    }

    // If none of the backends can handle the request, send an error.
    if (!backend_found) {
	// Return an error.
	server_error("Error: No backends can satisfy this request,"
		     " returning a 501.");
	result_info *ri = new result_info(501, "<h1>Not implemented.</h1>");
	set_result(ri);
	return NULL;
    }

    // See if we built a module.
    string module_path;
    mode_t module_mode = 0;
    string module_sign_path;
    if (staprc == 0) {
	glob_t globber;
	string pattern = crd->client_dir + "/*.ko";
	int rc = glob(pattern.c_str(), GLOB_ERR, NULL, &globber);
	if (rc) {
	    server_error(_F("Unable to find a module in %s",
			    crd->client_dir.c_str()));
	}
	else {
	    if (globber.gl_pathc != 1) {
		server_error(_F("Too many modules (%ld) in %s",
				(long)globber.gl_pathc,
				crd->client_dir.c_str()));
	    }
	    else {
		module_path = globber.gl_pathv[0];
		// We've got a path. Also figure out the file mode by
		// calling stat().
		struct stat stbuf;
		if (stat(module_path.c_str(), &stbuf) == 0) {
		    module_mode = stbuf.st_mode & 07777;
		}
		else {
		    module_path.clear();
		}
	    }
	    globfree(&globber);
	}

	// If we've got a module, it might need signing.
	if (! module_path.empty()
	    && (pr_contains(crd->privilege, pr_stapusr)
		|| pr_contains(crd->privilege, pr_stapsys))) {
	    server_error("Signing file...");
	    module_sign_path = module_path + ".sgn";
	    sign_file(httpd->get_cert_db_path(), server_cert_nickname(),
		      module_path, module_sign_path);
	}
    }

    result_info *ri = new result_info(staprc, stdout_path, stderr_path);
    if (! module_path.empty()) {
	ri->add_file(module_path, module_mode);
	if (! module_sign_path.empty()) {
	    // We've got a module signature. Also figure out the file
	    // mode by calling stat().
	    struct stat stbuf;
	    mode_t mode = 0;
	    if (stat(module_sign_path.c_str(), &stbuf) == 0) {
		mode = stbuf.st_mode & 07777;
	    }
	    else {
		module_sign_path.clear();
	    }
	    if (! module_sign_path.empty()) {
		ri->add_file(module_sign_path, mode);
	    }
	}
    }
    set_result(ri);
    return NULL;
}

client_request_data::~client_request_data()
{
    if (!server_dir.empty()) {
	// Remove the temporary directory.
	vector<string> cleanupcmd { "rm", "-rf", server_dir };
	int rc = stap_system(0, cleanupcmd);
	if (rc != 0)
	    server_error (_("Error in tmpdir cleanup"));
	if (verbose > 1)
	    server_error(_F("Removed temporary directory \"%s\"",
			    server_dir.c_str()));
	server_dir.clear();
    }
    if (!client_dir.empty()) {
	// Remove the temporary directory.
	vector<string> cleanupcmd { "rm", "-rf", client_dir };
	int rc = stap_system(0, cleanupcmd);
	if (rc != 0)
	    server_error (_("Error in tmpdir cleanup"));
	if (verbose > 1)
	    server_error(_F("Removed temporary directory \"%s\"",
			    client_dir.c_str()));
	client_dir.clear();
    }
}

// Return a json representation of the client_request_data. The caller
// is responsible for calling json_object_put() on the returned object.
struct json_object *
client_request_data::get_json_object() const
{
    // To make sure we have all the latest changes, we always make a
    // "fresh" json object.
    struct json_object *root = json_object_new_object();

    struct json_object *item = json_object_new_string(kver.c_str());
    json_object_object_add(root, "kver", item);
    item = json_object_new_string(arch.c_str());
    json_object_object_add(root, "arch", item);
    item = json_object_new_string(server_dir.c_str());
    json_object_object_add(root, "server_dir", item);
    item = json_object_new_string(client_dir.c_str());
    json_object_object_add(root, "client_dir", item);
    item = json_object_new_string(distro_name.c_str());
    json_object_object_add(root, "distro_name", item);
    item = json_object_new_string(distro_version.c_str());
    json_object_object_add(root, "distro_version", item);

    struct json_object *array = json_object_new_array();
    for (auto it = cmd_args.begin(); it != cmd_args.end(); ++it) {
	item = json_object_new_string((*it).c_str());
	json_object_array_add(array, item);
    }
    json_object_object_add(root, "cmd_args", array);
    
    array = json_object_new_array();
    for (auto it = files.begin(); it != files.end(); ++it) {
	item = json_object_new_string((*it).c_str());
	json_object_array_add(array, item);
    }
    json_object_object_add(root, "files", array);

    array = json_object_new_array();
    for (auto it = file_info.begin(); it != file_info.end(); ++it) {
	struct json_object *name = json_object_new_string((*it)->name.c_str());
	struct json_object *pkg = json_object_new_string((*it)->pkg.c_str());
	struct json_object *build_id = json_object_new_string((*it)->build_id.c_str());
	item = json_object_new_object();
	json_object_object_add(item, "name", name);
	json_object_object_add(item, "pkg", pkg);
	json_object_object_add(item, "build_id", build_id);
	json_object_array_add(array, item);
    }
    json_object_object_add(root, "file_info", array);
    return root;
}

void api_cleanup()
{    
    kill_stap_spawn(SIGTERM);
    {
	// Use a lock_guard to ensure the mutex gets released even if an
	// exception is thrown.
	lock_guard<mutex> lock(builds_mutex);
	for (size_t idx = 0; idx < build_infos.size(); idx++) {
	    delete build_infos[idx];
	}
	build_infos.clear();
    }
}

void api_add_request_handlers(server &http)
{
    // Remember the server.
    httpd = &http;
    
    // Add the request handlers.
    http.add_request_handler("/builds$", builds_rh);
    http.add_request_handler("/builds/([0-9a-f]+)$", build_rh);
    http.add_request_handler("/results/([0-9a-f]+)$", result_rh);
    http.add_request_handler("/results/([^/]+)/([^/]+)$", result_file_rh);
}