Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_fcgi_process.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "common/errno.h"
5 #include "common/Throttle.h"
6 #include "common/WorkQueue.h"
7
8 #include "rgw_rados.h"
9 #include "rgw_rest.h"
10 #include "rgw_frontend.h"
11 #include "rgw_request.h"
12 #include "rgw_process.h"
13 #include "rgw_loadgen.h"
14 #include "rgw_client_io.h"
15 #include "rgw_client_io_filters.h"
16
17 #define dout_subsys ceph_subsys_rgw
18
19 void RGWFCGXProcess::run()
20 {
21   string socket_path;
22   string socket_port;
23   string socket_host;
24   int socket_backlog;
25
26   conf->get_val("socket_path", "", &socket_path);
27   conf->get_val("socket_port", g_conf->rgw_port, &socket_port);
28   conf->get_val("socket_host", g_conf->rgw_host, &socket_host);
29   socket_backlog = g_conf->rgw_fcgi_socket_backlog;
30
31   if (socket_path.empty() && socket_port.empty() && socket_host.empty()) {
32     socket_path = g_conf->rgw_socket_path;
33     if (socket_path.empty()) {
34       dout(0) << "ERROR: no socket server point defined, cannot "
35         "start fcgi frontend" << dendl;
36       return;
37     }
38   }
39
40   if (!socket_path.empty()) {
41     string path_str = socket_path;
42
43     /* this is necessary, as FCGX_OpenSocket might not return an
44          * error, but rather ungracefully exit */
45     int fd = open(path_str.c_str(), O_CREAT, 0644);
46     if (fd < 0) {
47       int err = errno;
48       /* ENXIO is actually expected, we'll get that if we try to open
49            * a unix domain socket */
50       if (err != ENXIO) {
51                   dout(0) << "ERROR: cannot create socket: path=" << path_str
52                                   << " error=" << cpp_strerror(err) << dendl;
53                   return;
54       }
55     } else {
56                 close(fd);
57     }
58
59     const char *path = path_str.c_str();
60     sock_fd = FCGX_OpenSocket(path, socket_backlog);
61     if (sock_fd < 0) {
62       dout(0) << "ERROR: FCGX_OpenSocket (" << path << ") returned "
63               << sock_fd << dendl;
64       return;
65     }
66     if (chmod(path, 0777) < 0) {
67       dout(0) << "WARNING: couldn't set permissions on unix domain socket"
68               << dendl;
69     }
70   } else if (!socket_port.empty()) {
71     string bind = socket_host + ":" + socket_port;
72     sock_fd = FCGX_OpenSocket(bind.c_str(), socket_backlog);
73     if (sock_fd < 0) {
74       dout(0) << "ERROR: FCGX_OpenSocket (" << bind.c_str() << ") returned "
75               << sock_fd << dendl;
76       return;
77     }
78   }
79
80   m_tp.start();
81
82   FCGX_Request fcgx_reqs[max_connections];
83
84   QueueRing<FCGX_Request*> qr(max_connections);
85   for (int i = 0; i < max_connections; i++) {
86     FCGX_Request* fcgx = &fcgx_reqs[i];
87     FCGX_InitRequest(fcgx, sock_fd, 0);
88     qr.enqueue(fcgx);
89   }
90
91   for (;;) {
92     RGWFCGXRequest* req = new RGWFCGXRequest(store->get_new_req_id(), &qr);
93     dout(10) << "allocated request req=" << hex << req << dec << dendl;
94     req_throttle.get(1);
95     int ret = FCGX_Accept_r(req->fcgx);
96     if (ret < 0) {
97       delete req;
98       dout(0) << "ERROR: FCGX_Accept_r returned " << ret << dendl;
99       req_throttle.put(1);
100       break;
101     }
102     req_wq.queue(req);
103   }
104
105   m_tp.drain(&req_wq);
106   m_tp.stop();
107
108   dout(20) << "cleaning up fcgx connections" << dendl;
109
110   for (int i = 0; i < max_connections; i++) {
111     FCGX_Finish_r(&fcgx_reqs[i]);
112   }
113 } /* RGWFCGXProcess::run */
114
115 void RGWFCGXProcess::handle_request(RGWRequest* r)
116 {
117   RGWFCGXRequest* const req = static_cast<RGWFCGXRequest*>(r);
118
119   RGWFCGX fcgxfe(req->fcgx);
120   auto real_client_io = rgw::io::add_reordering(
121                           rgw::io::add_buffering(cct,
122                             rgw::io::add_chunking(
123                               &fcgxfe)));
124   RGWRestfulIO client_io(cct, &real_client_io);
125
126  
127   int ret = process_request(store, rest, req, uri_prefix,
128                             *auth_registry, &client_io, olog);
129   if (ret < 0) {
130     /* we don't really care about return code */
131     dout(20) << "process_request() returned " << ret << dendl;
132   }
133
134   FCGX_Finish_r(req->fcgx);
135
136   delete req;
137 } /* RGWFCGXProcess::handle_request */