1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
14 #include <boost/intrusive_ptr.hpp>
18 #include "common/ceph_argparse.h"
19 #include "global/global_init.h"
20 #include "global/signal_handler.h"
21 #include "common/config.h"
22 #include "common/errno.h"
23 #include "common/Timer.h"
24 #include "common/safe_io.h"
25 #include "include/compat.h"
26 #include "include/str_list.h"
27 #include "include/stringify.h"
28 #include "rgw_common.h"
29 #include "rgw_rados.h"
31 #include "rgw_period_pusher.h"
32 #include "rgw_realm_reloader.h"
34 #include "rgw_rest_s3.h"
35 #include "rgw_rest_swift.h"
36 #include "rgw_rest_admin.h"
37 #include "rgw_rest_usage.h"
38 #include "rgw_rest_user.h"
39 #include "rgw_rest_bucket.h"
40 #include "rgw_rest_metadata.h"
41 #include "rgw_rest_log.h"
42 #include "rgw_rest_opstate.h"
43 #include "rgw_replica_log.h"
44 #include "rgw_rest_replica_log.h"
45 #include "rgw_rest_config.h"
46 #include "rgw_rest_realm.h"
47 #include "rgw_swift_auth.h"
49 #include "rgw_tools.h"
50 #include "rgw_resolve.h"
52 #include "rgw_request.h"
53 #include "rgw_process.h"
54 #include "rgw_frontend.h"
55 #include "rgw_http_client_curl.h"
56 #if defined(WITH_RADOSGW_BEAST_FRONTEND)
57 #include "rgw_asio_frontend.h"
58 #endif /* WITH_RADOSGW_BEAST_FRONTEND */
65 #include "include/types.h"
66 #include "common/BackTrace.h"
68 #ifdef HAVE_SYS_PRCTL_H
69 #include <sys/prctl.h>
72 #define dout_subsys ceph_subsys_rgw
76 static sig_t sighandler_alrm
;
80 static int signal_fd
[2] = {0, 0};
81 static std::atomic
<int64_t> disable_signal_fd
= { 0 };
83 void signal_shutdown()
85 if (!disable_signal_fd
) {
87 int ret
= write(signal_fd
[0], (char *)&val
, sizeof(val
));
89 derr
<< "ERROR: " << __func__
<< ": write() returned "
90 << cpp_strerror(errno
) << dendl
;
95 static void wait_shutdown()
98 int r
= safe_read_exact(signal_fd
[1], &val
, sizeof(val
));
100 derr
<< "safe_read_exact returned with error" << dendl
;
104 static int signal_fd_init()
106 return socketpair(AF_UNIX
, SOCK_STREAM
, 0, signal_fd
);
109 static void signal_fd_finalize()
115 static void handle_sigterm(int signum
)
117 dout(1) << __func__
<< dendl
;
118 #if defined(WITH_RADOSGW_FCGI_FRONTEND)
119 FCGX_ShutdownPending();
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
) {
128 // safety net in case we get stuck doing an orderly shutdown.
129 uint64_t secs
= g_ceph_context
->_conf
->rgw_exit_timeout_secs
;
132 dout(1) << __func__
<< " set alarm for " << secs
<< dendl
;
137 static void godown_alarm(int signum
)
143 class C_InitTimeout
: public Context
{
146 void finish(int r
) override
{
147 derr
<< "Initialization timeout, failed to initialize" << dendl
;
154 cerr
<< "usage: radosgw [options...]" << std::endl
;
155 cerr
<< "options:\n";
156 cerr
<< " --rgw-region=<region> region in which radosgw runs\n";
157 cerr
<< " --rgw-zone=<zone> zone in which radosgw runs\n";
158 cerr
<< " --rgw-socket-path=<path> specify a unix domain socket path\n";
159 cerr
<< " -m monaddress[:port] connect to specified monitor\n";
160 cerr
<< " --keyring=<path> path to radosgw keyring\n";
161 cerr
<< " --logfile=<logfile> file to log debug output\n";
162 cerr
<< " --debug-rgw=<log-level>/<memory-level> set radosgw debug level\n";
163 generic_server_usage();
168 static RGWRESTMgr
*set_logging(RGWRESTMgr
*mgr
)
170 mgr
->set_logging(true);
174 static RGWRESTMgr
*rest_filter(RGWRados
*store
, int dialect
, RGWRESTMgr
*orig
)
176 RGWSyncModuleInstanceRef sync_module
= store
->get_sync_module();
178 return sync_module
->get_rest_filter(dialect
, orig
);
185 * start up the RADOS connection and then handle HTTP messages as they come in
187 #ifdef BUILDING_FOR_EMBEDDED
188 extern "C" int cephd_rgw(int argc
, const char **argv
)
190 int main(int argc
, const char **argv
)
193 // dout() messages will be sent to stderr, but FCGX wants messages on stdout
194 // Redirect stderr to stdout.
195 TEMP_FAILURE_RETRY(close(STDERR_FILENO
));
196 if (TEMP_FAILURE_RETRY(dup2(STDOUT_FILENO
, STDERR_FILENO
)) < 0) {
198 cout
<< "failed to redirect stderr to stdout: " << cpp_strerror(err
)
203 /* alternative default for module */
204 vector
<const char *> def_args
;
205 def_args
.push_back("--debug-rgw=1/5");
206 def_args
.push_back("--keyring=$rgw_data/keyring");
208 vector
<const char*> args
;
209 argv_to_vec(argc
, argv
, args
);
212 // First, let's determine which frontends are configured.
213 int flags
= CINIT_FLAG_UNPRIVILEGED_DAEMON_DEFAULTS
;
214 global_pre_init(&def_args
, args
, CEPH_ENTITY_TYPE_CLIENT
, CODE_ENVIRONMENT_DAEMON
,
217 list
<string
> frontends
;
218 get_str_list(g_conf
->rgw_frontends
, ",", frontends
);
219 multimap
<string
, RGWFrontendConfig
*> fe_map
;
220 list
<RGWFrontendConfig
*> configs
;
221 if (frontends
.empty()) {
222 frontends
.push_back("civetweb");
224 for (list
<string
>::iterator iter
= frontends
.begin(); iter
!= frontends
.end(); ++iter
) {
227 if (f
.find("civetweb") != string::npos
) {
228 // If civetweb is configured as a frontend, prevent global_init() from
229 // dropping permissions by setting the appropriate flag.
230 flags
|= CINIT_FLAG_DEFER_DROP_PRIVILEGES
;
231 if (f
.find("port") != string::npos
) {
232 // check for the most common ws problems
233 if ((f
.find("port=") == string::npos
) ||
234 (f
.find("port= ") != string::npos
)) {
235 derr
<< "WARNING: civetweb frontend config found unexpected spacing around 'port' "
236 << "(ensure civetweb port parameter has the form 'port=80' with no spaces "
237 << "before or after '=')" << dendl
;
242 RGWFrontendConfig
*config
= new RGWFrontendConfig(f
);
243 int r
= config
->init();
246 cerr
<< "ERROR: failed to init config: " << f
<< std::endl
;
250 configs
.push_back(config
);
252 string framework
= config
->get_framework();
253 fe_map
.insert(pair
<string
, RGWFrontendConfig
*>(framework
, config
));
256 // Now that we've determined which frontend(s) to use, continue with global
257 // initialization. Passing false as the final argument ensures that
258 // global_pre_init() is not invoked twice.
259 // claim the reference and release it after subsequent destructors have fired
260 auto cct
= global_init(&def_args
, args
, CEPH_ENTITY_TYPE_CLIENT
,
261 CODE_ENVIRONMENT_DAEMON
,
262 flags
, "rgw_data", false);
264 for (std::vector
<const char*>::iterator i
= args
.begin(); i
!= args
.end(); ++i
) {
265 if (ceph_argparse_flag(args
, i
, "-h", "--help", (char*)NULL
)) {
271 // maintain existing region root pool for new multisite objects
272 if (!g_conf
->rgw_region_root_pool
.empty()) {
273 const char *root_pool
= g_conf
->rgw_region_root_pool
.c_str();
274 if (g_conf
->rgw_zonegroup_root_pool
.empty()) {
275 g_conf
->set_val_or_die("rgw_zonegroup_root_pool", root_pool
);
277 if (g_conf
->rgw_period_root_pool
.empty()) {
278 g_conf
->set_val_or_die("rgw_period_root_pool", root_pool
);
280 if (g_conf
->rgw_realm_root_pool
.empty()) {
281 g_conf
->set_val_or_die("rgw_realm_root_pool", root_pool
);
285 // for region -> zonegroup conversion (must happen before common_init_finish())
286 if (!g_conf
->rgw_region
.empty() && g_conf
->rgw_zonegroup
.empty()) {
287 g_conf
->set_val_or_die("rgw_zonegroup", g_conf
->rgw_region
.c_str());
290 if (g_conf
->daemonize
) {
291 global_init_daemonize(g_ceph_context
);
294 SafeTimer
init_timer(g_ceph_context
, mutex
);
297 init_timer
.add_event_after(g_conf
->rgw_init_timeout
, new C_InitTimeout
);
300 // Enable the perf counter before starting the service thread
301 g_ceph_context
->enable_perf_counter();
303 common_init_finish(g_ceph_context
);
305 int r
= rgw_tools_init(g_ceph_context
);
307 derr
<< "ERROR: unable to initialize rgw tools" << dendl
;
312 rgw::curl::setup_curl(fe_map
);
314 #if defined(WITH_RADOSGW_FCGI_FRONTEND)
318 RGWRados
*store
= RGWStoreManager::get_storage(g_ceph_context
,
319 g_conf
->rgw_enable_gc_threads
, g_conf
->rgw_enable_lc_threads
, g_conf
->rgw_enable_quota_threads
,
320 g_conf
->rgw_run_sync_thread
, g_conf
->rgw_dynamic_resharding
, g_conf
->rgw_cache_enabled
);
323 init_timer
.cancel_all_events();
324 init_timer
.shutdown();
327 derr
<< "Couldn't init storage provider (RADOS)" << dendl
;
330 r
= rgw_perf_start(g_ceph_context
);
332 derr
<< "ERROR: failed starting rgw perf" << dendl
;
336 rgw_rest_init(g_ceph_context
, store
, store
->get_zonegroup());
339 init_timer
.cancel_all_events();
340 init_timer
.shutdown();
343 rgw_user_init(store
);
344 rgw_bucket_init(store
->meta_mgr
);
345 rgw_log_usage_init(g_ceph_context
, store
);
351 get_str_list(g_conf
->rgw_enable_apis
, apis
);
353 map
<string
, bool> apis_map
;
354 for (list
<string
>::iterator li
= apis
.begin(); li
!= apis
.end(); ++li
) {
355 apis_map
[*li
] = true;
358 // S3 website mode is a specialization of S3
359 const bool s3website_enabled
= apis_map
.count("s3website") > 0;
360 // Swift API entrypoint could placed in the root instead of S3
361 const bool swift_at_root
= g_conf
->rgw_swift_url_prefix
== "/";
362 if (apis_map
.count("s3") > 0 || s3website_enabled
) {
363 if (! swift_at_root
) {
364 rest
.register_default_mgr(set_logging(rest_filter(store
, RGW_REST_S3
,
365 new RGWRESTMgr_S3(s3website_enabled
))));
367 derr
<< "Cannot have the S3 or S3 Website enabled together with "
368 << "Swift API placed in the root of hierarchy" << dendl
;
373 if (apis_map
.count("swift") > 0) {
374 RGWRESTMgr_SWIFT
* const swift_resource
= new RGWRESTMgr_SWIFT
;
376 if (! g_conf
->rgw_cross_domain_policy
.empty()) {
377 swift_resource
->register_resource("crossdomain.xml",
378 set_logging(new RGWRESTMgr_SWIFT_CrossDomain
));
381 swift_resource
->register_resource("healthcheck",
382 set_logging(new RGWRESTMgr_SWIFT_HealthCheck
));
384 swift_resource
->register_resource("info",
385 set_logging(new RGWRESTMgr_SWIFT_Info
));
387 if (! swift_at_root
) {
388 rest
.register_resource(g_conf
->rgw_swift_url_prefix
,
389 set_logging(rest_filter(store
, RGW_REST_SWIFT
,
392 if (store
->get_zonegroup().zones
.size() > 1) {
393 derr
<< "Placing Swift API in the root of URL hierarchy while running"
394 << " multi-site configuration requires another instance of RadosGW"
395 << " with S3 API enabled!" << dendl
;
398 rest
.register_default_mgr(set_logging(swift_resource
));
402 if (apis_map
.count("swift_auth") > 0) {
403 rest
.register_resource(g_conf
->rgw_swift_auth_entry
,
404 set_logging(new RGWRESTMgr_SWIFT_Auth
));
407 if (apis_map
.count("admin") > 0) {
408 RGWRESTMgr_Admin
*admin_resource
= new RGWRESTMgr_Admin
;
409 admin_resource
->register_resource("usage", new RGWRESTMgr_Usage
);
410 admin_resource
->register_resource("user", new RGWRESTMgr_User
);
411 admin_resource
->register_resource("bucket", new RGWRESTMgr_Bucket
);
413 /*Registering resource for /admin/metadata */
414 admin_resource
->register_resource("metadata", new RGWRESTMgr_Metadata
);
415 admin_resource
->register_resource("log", new RGWRESTMgr_Log
);
416 admin_resource
->register_resource("opstate", new RGWRESTMgr_Opstate
);
417 admin_resource
->register_resource("replica_log", new RGWRESTMgr_ReplicaLog
);
418 admin_resource
->register_resource("config", new RGWRESTMgr_Config
);
419 admin_resource
->register_resource("realm", new RGWRESTMgr_Realm
);
420 rest
.register_resource(g_conf
->rgw_admin_entry
, admin_resource
);
423 /* Initialize the registry of auth strategies which will coordinate
424 * the dynamic reconfiguration. */
425 rgw::auth::ImplicitTenants implicit_tenant_context
{*g_conf
};
426 g_conf
->add_observer(&implicit_tenant_context
);
427 auto auth_registry
= \
428 rgw::auth::StrategyRegistry::create(g_ceph_context
, implicit_tenant_context
, store
);
430 /* Header custom behavior */
431 rest
.register_x_headers(g_conf
->rgw_log_http_headers
);
433 OpsLogSocket
*olog
= NULL
;
435 if (!g_conf
->rgw_ops_log_socket_path
.empty()) {
436 olog
= new OpsLogSocket(g_ceph_context
, g_conf
->rgw_ops_log_data_backlog
);
437 olog
->init(g_conf
->rgw_ops_log_socket_path
);
440 r
= signal_fd_init();
442 derr
<< "ERROR: unable to initialize signal fds" << dendl
;
446 init_async_signal_handler();
447 register_async_signal_handler(SIGHUP
, sighup_handler
);
448 register_async_signal_handler(SIGTERM
, handle_sigterm
);
449 register_async_signal_handler(SIGINT
, handle_sigterm
);
450 register_async_signal_handler(SIGUSR1
, handle_sigterm
);
451 sighandler_alrm
= signal(SIGALRM
, godown_alarm
);
453 map
<string
, string
> service_map_meta
;
454 service_map_meta
["pid"] = stringify(getpid());
456 list
<RGWFrontend
*> fes
;
460 for (multimap
<string
, RGWFrontendConfig
*>::iterator fiter
= fe_map
.begin();
461 fiter
!= fe_map
.end(); ++fiter
, ++fe_count
) {
462 RGWFrontendConfig
*config
= fiter
->second
;
463 string framework
= config
->get_framework();
464 RGWFrontend
*fe
= NULL
;
466 if (framework
== "civetweb" || framework
== "mongoose") {
467 framework
= "civetweb";
468 std::string uri_prefix
;
469 config
->get_val("prefix", "", &uri_prefix
);
471 RGWProcessEnv env
= { store
, &rest
, olog
, 0, uri_prefix
, auth_registry
};
473 fe
= new RGWCivetWebFrontend(env
, config
);
475 else if (framework
== "loadgen") {
477 config
->get_val("port", 80, &port
);
478 std::string uri_prefix
;
479 config
->get_val("prefix", "", &uri_prefix
);
481 RGWProcessEnv env
= { store
, &rest
, olog
, port
, uri_prefix
, auth_registry
};
483 fe
= new RGWLoadGenFrontend(env
, config
);
485 #if defined(WITH_RADOSGW_BEAST_FRONTEND)
486 else if ((framework
== "beast") &&
487 cct
->check_experimental_feature_enabled("rgw-beast-frontend")) {
489 config
->get_val("port", 80, &port
);
490 std::string uri_prefix
;
491 config
->get_val("prefix", "", &uri_prefix
);
492 RGWProcessEnv env
{ store
, &rest
, olog
, port
, uri_prefix
, auth_registry
};
493 fe
= new RGWAsioFrontend(env
, config
);
495 #endif /* WITH_RADOSGW_BEAST_FRONTEND */
496 #if defined(WITH_RADOSGW_FCGI_FRONTEND)
497 else if (framework
== "fastcgi" || framework
== "fcgi") {
498 framework
= "fastcgi";
499 std::string uri_prefix
;
500 config
->get_val("prefix", "", &uri_prefix
);
501 RGWProcessEnv fcgi_pe
= { store
, &rest
, olog
, 0, uri_prefix
, auth_registry
};
503 fe
= new RGWFCGXFrontend(fcgi_pe
, config
);
505 #endif /* WITH_RADOSGW_FCGI_FRONTEND */
507 service_map_meta
["frontend_type#" + stringify(fe_count
)] = framework
;
508 service_map_meta
["frontend_config#" + stringify(fe_count
)] = config
->get_config();
511 dout(0) << "WARNING: skipping unknown framework: " << framework
<< dendl
;
515 dout(0) << "starting handler: " << fiter
->first
<< dendl
;
518 derr
<< "ERROR: failed initializing frontend" << dendl
;
523 derr
<< "ERROR: failed run" << dendl
;
530 r
= store
->register_to_service_map("rgw", service_map_meta
);
532 derr
<< "ERROR: failed to register to service map: " << cpp_strerror(-r
) << dendl
;
538 // add a watcher to respond to realm configuration changes
539 RGWPeriodPusher
pusher(store
);
540 RGWFrontendPauser
pauser(fes
, implicit_tenant_context
, &pusher
);
541 RGWRealmReloader
reloader(store
, service_map_meta
, &pauser
);
543 RGWRealmWatcher
realm_watcher(g_ceph_context
, store
->realm
);
544 realm_watcher
.add_watcher(RGWRealmNotify::Reload
, reloader
);
545 realm_watcher
.add_watcher(RGWRealmNotify::ZonesNeedPeriod
, pusher
);
547 #if defined(HAVE_SYS_PRCTL_H)
548 if (prctl(PR_SET_DUMPABLE
, 1) == -1) {
549 cerr
<< "warning: unable to set dumpable flag: " << cpp_strerror(errno
) << std::endl
;
555 derr
<< "shutting down" << dendl
;
557 for (list
<RGWFrontend
*>::iterator liter
= fes
.begin(); liter
!= fes
.end();
559 RGWFrontend
*fe
= *liter
;
563 for (list
<RGWFrontend
*>::iterator liter
= fes
.begin(); liter
!= fes
.end();
565 RGWFrontend
*fe
= *liter
;
570 for (list
<RGWFrontendConfig
*>::iterator liter
= configs
.begin();
571 liter
!= configs
.end(); ++liter
) {
572 RGWFrontendConfig
*fec
= *liter
;
576 unregister_async_signal_handler(SIGHUP
, sighup_handler
);
577 unregister_async_signal_handler(SIGTERM
, handle_sigterm
);
578 unregister_async_signal_handler(SIGINT
, handle_sigterm
);
579 unregister_async_signal_handler(SIGUSR1
, handle_sigterm
);
580 shutdown_async_signal_handler();
582 rgw_log_usage_finalize();
586 RGWStoreManager::close_storage(store
);
589 rgw_shutdown_resolver();
590 rgw::curl::cleanup_curl();
591 g_conf
->remove_observer(&implicit_tenant_context
);
593 rgw_perf_stop(g_ceph_context
);
595 dout(1) << "final shutdown" << dendl
;
597 signal_fd_finalize();