1 /*
2    Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef MGMAPI_SERVICE_HPP
26 #define MGMAPI_SERVICE_HPP
27 
28 #include <SocketServer.hpp>
29 #include <NdbSleep.h>
30 #include <Parser.hpp>
31 #include <OutputStream.hpp>
32 #include <InputStream.hpp>
33 
34 #include "MgmtSrvr.hpp"
35 
36 class MgmApiSession : public SocketServer::Session
37 {
38   static void list_session(SocketServer::Session *_s, void *data);
39   static void get_session(SocketServer::Session *_s, void *data);
40 private:
41   typedef Parser<MgmApiSession> Parser_t;
42 
43   class MgmtSrvr & m_mgmsrv;
44   InputStream *m_input;
45   OutputStream *m_output;
46   Parser_t *m_parser;
47   char m_err_str[1024];
48   int m_stopSelf; // -1 is restart, 0 do nothing, 1 stop
49   NdbMutex *m_mutex;
50 
51   // for listing sessions and other fun:
52   Parser_t::Context *m_ctx;
53   Uint64 m_session_id;
54 
55   int m_errorInsert;
56 
57   BaseString m_name;
name()58   const char* name() { return m_name.c_str(); }
59 
get_error_text(int err_no)60   const char *get_error_text(int err_no)
61   { return m_mgmsrv.getErrorText(err_no, m_err_str, sizeof(m_err_str)); }
62 
63   /* Client version info, m_vMajor != 0 if known */
64   unsigned int m_vMajor;
65   unsigned int m_vMinor;
66   unsigned int m_vBuild;
67 
68 public:
69   MgmApiSession(class MgmtSrvr & mgm, NDB_SOCKET_TYPE sock, Uint64 session_id);
70   virtual ~MgmApiSession();
71   void runSession();
72 
73   static const unsigned SOCKET_TIMEOUT = 30000;
74 
75   void getConfig(Parser_t::Context &ctx, const class Properties &args, bool v2);
76   void getConfig_v1(Parser_t::Context &ctx, const class Properties &args);
77   void getConfig_v2(Parser_t::Context &ctx, const class Properties &args);
78   void setConfig_v1(Parser_t::Context &ctx, const class Properties &args);
79   void setConfig_v2(Parser_t::Context &ctx, const class Properties &args);
80   void setConfig(Parser_t::Context &ctx, const class Properties &args, bool v2);
81   void showConfig(Parser_t::Context &ctx, const class Properties &args);
82   void reloadConfig(Parser_t::Context &ctx, const class Properties &args);
83 
84   void get_nodeid(Parser_t::Context &ctx, const class Properties &args);
85   void getVersion(Parser_t::Context &ctx, const class Properties &args);
86   void setClientVersion(Parser_t::Context &ctx, const class Properties &args);
87   void getStatus(Parser_t::Context &ctx, const class Properties &args);
88   void getInfoClusterLog(Parser_t::Context &ctx, const class Properties &args);
89   void restart(const class Properties &args, int version);
90   void restart_v1(Parser_t::Context &ctx, const class Properties &args);
91   void restart_v2(Parser_t::Context &ctx, const class Properties &args);
92   void restartAll(Parser_t::Context &ctx, const class Properties &args);
93   void insertError(Parser_t::Context &ctx, const class Properties &args);
94   void setTrace(Parser_t::Context &ctx, const class Properties &args);
95   void logSignals(Parser_t::Context &ctx, const class Properties &args);
96   void startSignalLog(Parser_t::Context &ctx, const class Properties &args);
97   void stopSignalLog(Parser_t::Context &ctx, const class Properties &args);
98   void dumpState(Parser_t::Context &ctx, const class Properties &args);
99   void startBackup(Parser_t::Context &ctx, const class Properties &args);
100   void abortBackup(Parser_t::Context &ctx, const class Properties &args);
101   void enterSingleUser(Parser_t::Context &ctx, const class Properties &args);
102   void exitSingleUser(Parser_t::Context &ctx, const class Properties &args);
103   void stop_v1(Parser_t::Context &ctx, const class Properties &args);
104   void stop_v2(Parser_t::Context &ctx, const class Properties &args);
105   void stop(const class Properties &args, int version);
106   void stopAll(Parser_t::Context &ctx, const class Properties &args);
107   void start(Parser_t::Context &ctx, const class Properties &args);
108   void startAll(Parser_t::Context &ctx, const class Properties &args);
109   void bye(Parser_t::Context &ctx, const class Properties &args);
110   void endSession(Parser_t::Context &ctx, const class Properties &args);
111   void setLogLevel(Parser_t::Context &ctx, const class Properties &args);
112   void getClusterLogLevel(Parser_t::Context &ctx,
113 			  const class Properties &args);
114   void setClusterLogLevel(Parser_t::Context &ctx,
115 			  const class Properties &args);
116   void setLogFilter(Parser_t::Context &ctx, const class Properties &args);
117 
118   void setParameter(Parser_t::Context &ctx, const class Properties &args);
119   void setConnectionParameter(Parser_t::Context &ctx,
120 			      const class Properties &args);
121   void getConnectionParameter(Parser_t::Context &ctx,
122 			      Properties const &args);
123 
124   void listen_event(Parser_t::Context &ctx, const class Properties &args);
125 
126   void purge_stale_sessions(Parser_t::Context &ctx, const class Properties &args);
127   void check_connection(Parser_t::Context &ctx, const class Properties &args);
128 
129   void transporter_connect(Parser_t::Context &ctx, Properties const &args);
130 
131   void get_mgmd_nodeid(Parser_t::Context &ctx, Properties const &args);
132 
133   void report_event(Parser_t::Context &ctx, Properties const &args);
134 
135   void listSessions(Parser_t::Context &ctx, Properties const &args);
136 
137   void getSessionId(Parser_t::Context &ctx, Properties const &args);
138   void getSession(Parser_t::Context &ctx, Properties const &args);
139 
140   void create_nodegroup(Parser_t::Context &ctx, Properties const &args);
141   void drop_nodegroup(Parser_t::Context &ctx, Properties const &args);
142 
143   void show_variables(Parser_t::Context &ctx, Properties const &args);
144 
145   void dump_events(Parser_t::Context &ctx, Properties const &args);
146 
147   void set_ports(Parser_t::Context &, Properties const &args);
148 };
149 
150 class MgmApiService : public SocketServer::Service {
151   MgmtSrvr& m_mgmsrv;
152   Uint64 m_next_session_id; // Protected by m_sessions mutex it SocketServer
153 public:
MgmApiService(MgmtSrvr & mgm)154   MgmApiService(MgmtSrvr& mgm):
155     m_mgmsrv(mgm),
156     m_next_session_id(1) {}
157 
newSession(NDB_SOCKET_TYPE socket)158   SocketServer::Session * newSession(NDB_SOCKET_TYPE socket){
159     return new MgmApiSession(m_mgmsrv, socket, m_next_session_id++);
160   }
161 };
162 
str_null(const char * str)163 static const char* str_null(const char* str)
164 {
165   return (str ? str : "(null)");
166 }
167 
yes_no(bool value)168 static const char* yes_no(bool value)
169 {
170   return (value ? "yes" : "no");
171 }
172 
173 
174 #endif
175