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