summaryrefslogtreecommitdiffstats
path: root/src/rgw/librgw_admin_user.cc
blob: 928f04cb3294f711de7b9694f098b4642071671e (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

/*
 * create rgw admin user
 *
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "include/compat.h"
#include <sys/types.h>
#include <string.h>
#include <chrono>
#include <errno.h>
#include <thread>
#include <string>
#include <mutex>

#include "include/types.h"
#include "include/rgw/librgw_admin_user.h"
#include "include/str_list.h"
#include "include/stringify.h"
#include "global/global_init.h"
#include "global/signal_handler.h"
#include "common/config.h"
#include "common/errno.h"
#include "common/Timer.h"
#include "common/Throttle.h"
#include "common/WorkQueue.h"
#include "common/ceph_argparse.h"
#include "common/ceph_context.h"
#include "common/common_init.h"
#include "common/dout.h"

#include "rgw_admin_user.h"
#include "rgw_rados.h"
#include "rgw_os_lib.h"
#include "rgw_auth.h"
#include "rgw_auth_s3.h"

#define dout_subsys ceph_subsys_rgw

bool global_stop = false;

static void handle_sigterm(int signum)
{
  dout(20) << __func__ << " SIGUSR1 ignored" << dendl;
}

namespace rgw {

  using std::string;

  static std::mutex librgw_admin_user_mtx;

  RGWLibAdmin rgw_lib_admin;

  class C_InitTimeout : public Context {
  public:
    C_InitTimeout() {}
    void finish(int r) override {
      derr << "Initialization timeout, failed to initialize" << dendl;
      exit(1);
    }
  };

  int RGWLibAdmin::init()
  {
    vector<const char*> args;
    return init(args);
  }

  int RGWLibAdmin::init(vector<const char*>& args)
  {
    /* alternative default for module */
    map<string,string> defaults = {
      { "debug_rgw", "1/5" },
      { "keyring", "$rgw_data/keyring" },
      { "log_file", "/var/log/radosgw/$cluster-$name.log" }
    };

    cct = global_init(&defaults, args,
		      CEPH_ENTITY_TYPE_CLIENT,
		      CODE_ENVIRONMENT_UTILITY, 0);

    Mutex mutex("main");
    SafeTimer init_timer(g_ceph_context, mutex);
    init_timer.init();
    mutex.Lock();
    init_timer.add_event_after(g_conf()->rgw_init_timeout, new C_InitTimeout);
    mutex.Unlock();

    common_init_finish(g_ceph_context);

    store = RGWStoreManager::get_storage(g_ceph_context, false, false, false, false, false);

    if (!store) {
      mutex.Lock();
      init_timer.cancel_all_events();
      init_timer.shutdown();
      mutex.Unlock();

      derr << "Couldn't init storage provider (RADOS)" << dendl;
      return -EIO;
    }

    mutex.Lock();
    init_timer.cancel_all_events();
    init_timer.shutdown();
    mutex.Unlock();

    rgw_user_init(store);

    init_async_signal_handler();
    register_async_signal_handler(SIGUSR1, handle_sigterm);

    return 0;
  } /* RGWLibAdmin::init() */

  int RGWLibAdmin::stop()
  {
    derr << "shutting down" << dendl;

    unregister_async_signal_handler(SIGUSR1, handle_sigterm);
    shutdown_async_signal_handler();

    RGWStoreManager::close_storage(store);

    dout(1) << "final shutdown" << dendl;
    cct.reset();

    return 0;
  } /* RGWLibAdmin::stop() */

} /* namespace rgw */

extern "C" {

int librgw_admin_user_create(librgw_admin_user_t* rgw_admin_user, int argc, char **argv)
{
  using namespace rgw;

  int rc = -EINVAL;

  if (! g_ceph_context) {
    std::lock_guard<std::mutex> lg(librgw_admin_user_mtx);
    if (! g_ceph_context) {
      vector<const char*> args;
      std::vector<std::string> spl_args;
      // last non-0 argument will be split and consumed
      if (argc > 1) {
	const std::string spl_arg{argv[(--argc)]};
	get_str_vec(spl_arg, " \t", spl_args);
      }
      argv_to_vec(argc, const_cast<const char**>(argv), args);
      // append split args, if any
      for (const auto& elt : spl_args) {
	args.push_back(elt.c_str());
      }
      rc = rgw_lib_admin.init(args);
    }
  }

  *rgw_admin_user = g_ceph_context->get();

  return rc;
}

void librgw_admin_user_shutdown(librgw_admin_user_t rgw_admin_user)
{
  using namespace rgw;

  CephContext* cct = static_cast<CephContext*>(rgw_admin_user);
  rgw_lib_admin.stop();
  cct->put();
}

} /* extern "C" */