]> git.proxmox.com Git - ceph.git/blame - ceph/src/rgw/rgw_main.cc
bump version to 12.0.3-pve3
[ceph.git] / ceph / src / rgw / rgw_main.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <stdarg.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
11#include <errno.h>
12#include <signal.h>
13
14#include <curl/curl.h>
15
16#include <boost/intrusive_ptr.hpp>
17
18#include "acconfig.h"
19
20#include "common/ceph_argparse.h"
21#include "global/global_init.h"
22#include "global/signal_handler.h"
23#include "common/config.h"
24#include "common/errno.h"
25#include "common/Timer.h"
26#include "common/safe_io.h"
27#include "include/compat.h"
28#include "include/str_list.h"
29#include "rgw_common.h"
30#include "rgw_rados.h"
31#include "rgw_user.h"
32#include "rgw_period_pusher.h"
33#include "rgw_realm_reloader.h"
34#include "rgw_rest.h"
35#include "rgw_rest_s3.h"
36#include "rgw_rest_swift.h"
37#include "rgw_rest_admin.h"
38#include "rgw_rest_usage.h"
39#include "rgw_rest_user.h"
40#include "rgw_rest_bucket.h"
41#include "rgw_rest_metadata.h"
42#include "rgw_rest_log.h"
43#include "rgw_rest_opstate.h"
44#include "rgw_replica_log.h"
45#include "rgw_rest_replica_log.h"
46#include "rgw_rest_config.h"
47#include "rgw_rest_realm.h"
48#include "rgw_swift_auth.h"
49#include "rgw_log.h"
50#include "rgw_tools.h"
51#include "rgw_resolve.h"
52
53#include "rgw_request.h"
54#include "rgw_process.h"
55#include "rgw_frontend.h"
56#if defined(WITH_RADOSGW_BEAST_FRONTEND)
57#include "rgw_asio_frontend.h"
58#endif /* WITH_RADOSGW_BEAST_FRONTEND */
59
60#include <map>
61#include <string>
62#include <vector>
63#include <atomic>
64
65#include "include/types.h"
66#include "common/BackTrace.h"
67
68#ifdef HAVE_SYS_PRCTL_H
69#include <sys/prctl.h>
70#endif
71
72#define dout_subsys ceph_subsys_rgw
73
74using namespace std;
75
76static sig_t sighandler_alrm;
77
78class RGWProcess;
79
80static int signal_fd[2] = {0, 0};
81static std::atomic<int64_t> disable_signal_fd = { 0 };
82
83void signal_shutdown()
84{
85 if (!disable_signal_fd) {
86 int val = 0;
87 int ret = write(signal_fd[0], (char *)&val, sizeof(val));
88 if (ret < 0) {
89 derr << "ERROR: " << __func__ << ": write() returned "
90 << cpp_strerror(errno) << dendl;
91 }
92 }
93}
94
95static void wait_shutdown()
96{
97 int val;
98 int r = safe_read_exact(signal_fd[1], &val, sizeof(val));
99 if (r < 0) {
100 derr << "safe_read_exact returned with error" << dendl;
101 }
102}
103
104static int signal_fd_init()
105{
106 return socketpair(AF_UNIX, SOCK_STREAM, 0, signal_fd);
107}
108
109static void signal_fd_finalize()
110{
111 close(signal_fd[0]);
112 close(signal_fd[1]);
113}
114
115static void handle_sigterm(int signum)
116{
117 dout(1) << __func__ << dendl;
118#if defined(WITH_RADOSGW_FCGI_FRONTEND)
119 FCGX_ShutdownPending();
120#endif
121
122 // send a signal to make fcgi's accept(2) wake up. unfortunately the
123 // initial signal often isn't sufficient because we race with accept's
124 // check of the flag wet by ShutdownPending() above.
125 if (signum != SIGUSR1) {
126 signal_shutdown();
127
128 // safety net in case we get stuck doing an orderly shutdown.
129 uint64_t secs = g_ceph_context->_conf->rgw_exit_timeout_secs;
130 if (secs)
131 alarm(secs);
132 dout(1) << __func__ << " set alarm for " << secs << dendl;
133 }
134
135}
136
137static void godown_alarm(int signum)
138{
139 _exit(0);
140}
141
142#ifdef HAVE_CURL_MULTI_WAIT
143static void check_curl()
144{
145}
146#else
147static void check_curl()
148{
149 derr << "WARNING: libcurl doesn't support curl_multi_wait()" << dendl;
150 derr << "WARNING: cross zone / region transfer performance may be affected" << dendl;
151}
152#endif
153
154class C_InitTimeout : public Context {
155public:
156 C_InitTimeout() {}
157 void finish(int r) override {
158 derr << "Initialization timeout, failed to initialize" << dendl;
159 exit(1);
160 }
161};
162
163static int usage()
164{
165 cerr << "usage: radosgw [options...]" << std::endl;
166 cerr << "options:\n";
167 cerr << " --rgw-region=<region> region in which radosgw runs\n";
168 cerr << " --rgw-zone=<zone> zone in which radosgw runs\n";
169 cerr << " --rgw-socket-path=<path> specify a unix domain socket path\n";
170 cerr << " -m monaddress[:port] connect to specified monitor\n";
171 cerr << " --keyring=<path> path to radosgw keyring\n";
172 cerr << " --logfile=<logfile> file to log debug output\n";
173 cerr << " --debug-rgw=<log-level>/<memory-level> set radosgw debug level\n";
174 generic_server_usage();
175
176 return 0;
177}
178
179static RGWRESTMgr *set_logging(RGWRESTMgr *mgr)
180{
181 mgr->set_logging(true);
182 return mgr;
183}
184
185RGWRealmReloader *preloader = NULL;
186
187static void reloader_handler(int signum)
188{
189 if (preloader) {
190 bufferlist bl;
191 bufferlist::iterator p = bl.begin();
192 preloader->handle_notify(RGWRealmNotify::Reload, p);
193 }
194 sighup_handler(signum);
195}
196
197/*
198 * start up the RADOS connection and then handle HTTP messages as they come in
199 */
200#ifdef BUILDING_FOR_EMBEDDED
201extern "C" int cephd_rgw(int argc, const char **argv)
202#else
203int main(int argc, const char **argv)
204#endif
205{
206 // dout() messages will be sent to stderr, but FCGX wants messages on stdout
207 // Redirect stderr to stdout.
208 TEMP_FAILURE_RETRY(close(STDERR_FILENO));
209 if (TEMP_FAILURE_RETRY(dup2(STDOUT_FILENO, STDERR_FILENO)) < 0) {
210 int err = errno;
211 cout << "failed to redirect stderr to stdout: " << cpp_strerror(err)
212 << std::endl;
213 return ENOSYS;
214 }
215
216 /* alternative default for module */
217 vector<const char *> def_args;
218 def_args.push_back("--debug-rgw=1/5");
219 def_args.push_back("--keyring=$rgw_data/keyring");
220
221 vector<const char*> args;
222 argv_to_vec(argc, argv, args);
223 env_to_vec(args);
224
225 // First, let's determine which frontends are configured.
226 int flags = CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS;
227 global_pre_init(&def_args, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_DAEMON,
228 flags);
229
230 list<string> frontends;
231 get_str_list(g_conf->rgw_frontends, ",", frontends);
232 multimap<string, RGWFrontendConfig *> fe_map;
233 list<RGWFrontendConfig *> configs;
234 if (frontends.empty()) {
235 frontends.push_back("fastcgi");
236 }
237 for (list<string>::iterator iter = frontends.begin(); iter != frontends.end(); ++iter) {
238 string& f = *iter;
239
240 if (f.find("civetweb") != string::npos) {
241 // If civetweb is configured as a frontend, prevent global_init() from
242 // dropping permissions by setting the appropriate flag.
243 flags |= CINIT_FLAG_DEFER_DROP_PRIVILEGES;
244 if (f.find("port") != string::npos) {
245 // check for the most common ws problems
246 if ((f.find("port=") == string::npos) ||
247 (f.find("port= ") != string::npos)) {
248 derr << "WARNING: civetweb frontend config found unexpected spacing around 'port' "
249 << "(ensure civetweb port parameter has the form 'port=80' with no spaces "
250 << "before or after '=')" << dendl;
251 }
252 }
253 }
254
255 RGWFrontendConfig *config = new RGWFrontendConfig(f);
256 int r = config->init();
257 if (r < 0) {
258 delete config;
259 cerr << "ERROR: failed to init config: " << f << std::endl;
260 return EINVAL;
261 }
262
263 configs.push_back(config);
264
265 string framework = config->get_framework();
266 fe_map.insert(pair<string, RGWFrontendConfig*>(framework, config));
267 }
268
269 // Now that we've determined which frontend(s) to use, continue with global
270 // initialization. Passing false as the final argument ensures that
271 // global_pre_init() is not invoked twice.
272 // claim the reference and release it after subsequent destructors have fired
273 auto cct = global_init(&def_args, args, CEPH_ENTITY_TYPE_CLIENT,
274 CODE_ENVIRONMENT_DAEMON,
275 flags, "rgw_data", false);
276
277 for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ++i) {
278 if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
279 usage();
280 return 0;
281 }
282 }
283
284 // maintain existing region root pool for new multisite objects
285 if (!g_conf->rgw_region_root_pool.empty()) {
286 const char *root_pool = g_conf->rgw_region_root_pool.c_str();
287 if (g_conf->rgw_zonegroup_root_pool.empty()) {
288 g_conf->set_val_or_die("rgw_zonegroup_root_pool", root_pool);
289 }
290 if (g_conf->rgw_period_root_pool.empty()) {
291 g_conf->set_val_or_die("rgw_period_root_pool", root_pool);
292 }
293 if (g_conf->rgw_realm_root_pool.empty()) {
294 g_conf->set_val_or_die("rgw_realm_root_pool", root_pool);
295 }
296 }
297
298 // for region -> zonegroup conversion (must happen before common_init_finish())
299 if (!g_conf->rgw_region.empty() && g_conf->rgw_zonegroup.empty()) {
300 g_conf->set_val_or_die("rgw_zonegroup", g_conf->rgw_region.c_str());
301 }
302
303 check_curl();
304
305 if (g_conf->daemonize) {
306 global_init_daemonize(g_ceph_context);
307 }
308 Mutex mutex("main");
309 SafeTimer init_timer(g_ceph_context, mutex);
310 init_timer.init();
311 mutex.Lock();
312 init_timer.add_event_after(g_conf->rgw_init_timeout, new C_InitTimeout);
313 mutex.Unlock();
314
315 // Enable the perf counter before starting the service thread
316 g_ceph_context->enable_perf_counter();
317
318 common_init_finish(g_ceph_context);
319
320 int r = rgw_tools_init(g_ceph_context);
321 if (r < 0) {
322 derr << "ERROR: unable to initialize rgw tools" << dendl;
323 return -r;
324 }
325
326 rgw_init_resolver();
327
328 curl_global_init(CURL_GLOBAL_ALL);
329
330#if defined(WITH_RADOSGW_FCGI_FRONTEND)
331 FCGX_Init();
332#endif
333
334 RGWRados *store = RGWStoreManager::get_storage(g_ceph_context,
335 g_conf->rgw_enable_gc_threads, g_conf->rgw_enable_lc_threads, g_conf->rgw_enable_quota_threads,
336 g_conf->rgw_run_sync_thread);
337 if (!store) {
338 mutex.Lock();
339 init_timer.cancel_all_events();
340 init_timer.shutdown();
341 mutex.Unlock();
342
343 derr << "Couldn't init storage provider (RADOS)" << dendl;
344 return EIO;
345 }
346 r = rgw_perf_start(g_ceph_context);
347 if (r < 0) {
348 derr << "ERROR: failed starting rgw perf" << dendl;
349 return -r;
350 }
351
352 rgw_rest_init(g_ceph_context, store, store->get_zonegroup());
353
354 mutex.Lock();
355 init_timer.cancel_all_events();
356 init_timer.shutdown();
357 mutex.Unlock();
358
359 rgw_user_init(store);
360 rgw_bucket_init(store->meta_mgr);
361 rgw_log_usage_init(g_ceph_context, store);
362
363 RGWREST rest;
364
365 list<string> apis;
366
367 get_str_list(g_conf->rgw_enable_apis, apis);
368
369 map<string, bool> apis_map;
370 for (list<string>::iterator li = apis.begin(); li != apis.end(); ++li) {
371 apis_map[*li] = true;
372 }
373
374 // S3 website mode is a specialization of S3
375 const bool s3website_enabled = apis_map.count("s3website") > 0;
376 // Swift API entrypoint could placed in the root instead of S3
377 const bool swift_at_root = g_conf->rgw_swift_url_prefix == "/";
378 if (apis_map.count("s3") > 0 || s3website_enabled) {
379 if (! swift_at_root) {
380 rest.register_default_mgr(set_logging(new RGWRESTMgr_S3(s3website_enabled)));
381 } else {
382 derr << "Cannot have the S3 or S3 Website enabled together with "
383 << "Swift API placed in the root of hierarchy" << dendl;
384 return EINVAL;
385 }
386 }
387
388 if (apis_map.count("swift") > 0) {
389 RGWRESTMgr_SWIFT* const swift_resource = new RGWRESTMgr_SWIFT;
390
391 if (! g_conf->rgw_cross_domain_policy.empty()) {
392 swift_resource->register_resource("crossdomain.xml",
393 set_logging(new RGWRESTMgr_SWIFT_CrossDomain));
394 }
395
396 swift_resource->register_resource("healthcheck",
397 set_logging(new RGWRESTMgr_SWIFT_HealthCheck));
398
399 swift_resource->register_resource("info",
400 set_logging(new RGWRESTMgr_SWIFT_Info));
401
402 if (! swift_at_root) {
403 rest.register_resource(g_conf->rgw_swift_url_prefix,
404 set_logging(swift_resource));
405 } else {
406 if (store->get_zonegroup().zones.size() > 1) {
407 derr << "Placing Swift API in the root of URL hierarchy while running"
408 << " multi-site configuration requires another instance of RadosGW"
409 << " with S3 API enabled!" << dendl;
410 }
411
412 rest.register_default_mgr(set_logging(swift_resource));
413 }
414 }
415
416 if (apis_map.count("swift_auth") > 0) {
417 rest.register_resource(g_conf->rgw_swift_auth_entry,
418 set_logging(new RGWRESTMgr_SWIFT_Auth));
419 }
420
421 if (apis_map.count("admin") > 0) {
422 RGWRESTMgr_Admin *admin_resource = new RGWRESTMgr_Admin;
423 admin_resource->register_resource("usage", new RGWRESTMgr_Usage);
424 admin_resource->register_resource("user", new RGWRESTMgr_User);
425 admin_resource->register_resource("bucket", new RGWRESTMgr_Bucket);
426
427 /*Registering resource for /admin/metadata */
428 admin_resource->register_resource("metadata", new RGWRESTMgr_Metadata);
429 admin_resource->register_resource("log", new RGWRESTMgr_Log);
430 admin_resource->register_resource("opstate", new RGWRESTMgr_Opstate);
431 admin_resource->register_resource("replica_log", new RGWRESTMgr_ReplicaLog);
432 admin_resource->register_resource("config", new RGWRESTMgr_Config);
433 admin_resource->register_resource("realm", new RGWRESTMgr_Realm);
434 rest.register_resource(g_conf->rgw_admin_entry, admin_resource);
435 }
436
437 /* Initialize the registry of auth strategies which will coordinate
438 * the dynamic reconfiguration. */
439 auto auth_registry = \
440 rgw::auth::StrategyRegistry::create(g_ceph_context, store);
441
442 /* Header custom behavior */
443 rest.register_x_headers(g_conf->rgw_log_http_headers);
444
445 OpsLogSocket *olog = NULL;
446
447 if (!g_conf->rgw_ops_log_socket_path.empty()) {
448 olog = new OpsLogSocket(g_ceph_context, g_conf->rgw_ops_log_data_backlog);
449 olog->init(g_conf->rgw_ops_log_socket_path);
450 }
451
452 r = signal_fd_init();
453 if (r < 0) {
454 derr << "ERROR: unable to initialize signal fds" << dendl;
455 exit(1);
456 }
457
458 init_async_signal_handler();
459 register_async_signal_handler(SIGHUP, reloader_handler);
460 register_async_signal_handler(SIGTERM, handle_sigterm);
461 register_async_signal_handler(SIGINT, handle_sigterm);
462 register_async_signal_handler(SIGUSR1, handle_sigterm);
463 sighandler_alrm = signal(SIGALRM, godown_alarm);
464
465 list<RGWFrontend *> fes;
466
467 for (multimap<string, RGWFrontendConfig *>::iterator fiter = fe_map.begin();
468 fiter != fe_map.end(); ++fiter) {
469 RGWFrontendConfig *config = fiter->second;
470 string framework = config->get_framework();
471 RGWFrontend *fe = NULL;
472
473 if (framework == "civetweb" || framework == "mongoose") {
474 std::string uri_prefix;
475 config->get_val("prefix", "", &uri_prefix);
476
477 RGWProcessEnv env = { store, &rest, olog, 0, uri_prefix, auth_registry };
478
479 fe = new RGWCivetWebFrontend(env, config);
480 }
481 else if (framework == "loadgen") {
482 int port;
483 config->get_val("port", 80, &port);
484 std::string uri_prefix;
485 config->get_val("prefix", "", &uri_prefix);
486
487 RGWProcessEnv env = { store, &rest, olog, port, uri_prefix, auth_registry };
488
489 fe = new RGWLoadGenFrontend(env, config);
490 }
491#if defined(WITH_RADOSGW_BEAST_FRONTEND)
492 else if ((framework == "beast") &&
493 cct->check_experimental_feature_enabled("rgw-beast-frontend")) {
494 int port;
495 config->get_val("port", 80, &port);
496 std::string uri_prefix;
497 config->get_val("prefix", "", &uri_prefix);
498 RGWProcessEnv env{ store, &rest, olog, port, uri_prefix, auth_registry };
499 fe = new RGWAsioFrontend(env);
500 }
501#endif /* WITH_RADOSGW_BEAST_FRONTEND */
502#if defined(WITH_RADOSGW_FCGI_FRONTEND)
503 else if (framework == "fastcgi" || framework == "fcgi") {
504 std::string uri_prefix;
505 config->get_val("prefix", "", &uri_prefix);
506 RGWProcessEnv fcgi_pe = { store, &rest, olog, 0, uri_prefix, auth_registry };
507
508 fe = new RGWFCGXFrontend(fcgi_pe, config);
509 }
510#endif /* WITH_RADOSGW_FCGI_FRONTEND */
511
512 if (fe == NULL) {
513 dout(0) << "WARNING: skipping unknown framework: " << framework << dendl;
514 continue;
515 }
516
517 dout(0) << "starting handler: " << fiter->first << dendl;
518 int r = fe->init();
519 if (r < 0) {
520 derr << "ERROR: failed initializing frontend" << dendl;
521 return -r;
522 }
523 r = fe->run();
524 if (r < 0) {
525 derr << "ERROR: failed run" << dendl;
526 return -r;
527 }
528
529 fes.push_back(fe);
530 }
531
532 // add a watcher to respond to realm configuration changes
533 RGWPeriodPusher pusher(store);
534 RGWFrontendPauser pauser(fes, &pusher);
535 RGWRealmReloader reloader(store, &pauser);
536
537 preloader = &reloader;
538
539 RGWRealmWatcher realm_watcher(g_ceph_context, store->realm);
540 realm_watcher.add_watcher(RGWRealmNotify::Reload, reloader);
541 realm_watcher.add_watcher(RGWRealmNotify::ZonesNeedPeriod, pusher);
542
543#if defined(HAVE_SYS_PRCTL_H)
544 if (prctl(PR_SET_DUMPABLE, 1) == -1) {
545 cerr << "warning: unable to set dumpable flag: " << cpp_strerror(errno) << std::endl;
546 }
547#endif
548
549 wait_shutdown();
550
551 derr << "shutting down" << dendl;
552
553 for (list<RGWFrontend *>::iterator liter = fes.begin(); liter != fes.end();
554 ++liter) {
555 RGWFrontend *fe = *liter;
556 fe->stop();
557 }
558
559 for (list<RGWFrontend *>::iterator liter = fes.begin(); liter != fes.end();
560 ++liter) {
561 RGWFrontend *fe = *liter;
562 fe->join();
563 delete fe;
564 }
565
566 for (list<RGWFrontendConfig *>::iterator liter = configs.begin();
567 liter != configs.end(); ++liter) {
568 RGWFrontendConfig *fec = *liter;
569 delete fec;
570 }
571
572 unregister_async_signal_handler(SIGHUP, reloader_handler);
573 unregister_async_signal_handler(SIGTERM, handle_sigterm);
574 unregister_async_signal_handler(SIGINT, handle_sigterm);
575 unregister_async_signal_handler(SIGUSR1, handle_sigterm);
576 shutdown_async_signal_handler();
577
578 rgw_log_usage_finalize();
579
580 delete olog;
581
582 RGWStoreManager::close_storage(store);
583
584 rgw_tools_cleanup();
585 rgw_shutdown_resolver();
586 curl_global_cleanup();
587
588 rgw_perf_stop(g_ceph_context);
589
590 dout(1) << "final shutdown" << dendl;
591
592 signal_fd_finalize();
593
594 return 0;
595}