1 /*
2    Copyright (c) 2003, 2021, Oracle and/or its affiliates.
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 
27 #include <ndb_global.h>
28 #include "../CpcClient.hpp"
29 #include <Vector.hpp>
30 
31 SimpleCpcClient g_client("localhost", 1234);
32 Vector<SimpleCpcClient::Process> g_procs;
33 
34 void define();
35 void start(SimpleCpcClient::Process & p);
36 void stop(SimpleCpcClient::Process & p);
37 void undefine(SimpleCpcClient::Process & p);
38 void list();
39 SimpleCpcClient::Process* find(int id);
40 
41 #define ABORT() {ndbout_c("ABORT"); while(true); abort();}
42 
43 int name = 0;
44 
45 int
main(void)46 main(void){
47 
48   g_client.connect();
49 
50   srand(time(0));
51   for(int i = 0; i<1000; i++){
52     int sz = g_procs.size();
53     int test = rand() % 100;
54     if(sz == 0 || test < 10){
55       define();
56       continue;
57     }
58 
59     list();
60 
61     int proc = rand() % g_procs.size();
62     SimpleCpcClient::Process & p = g_procs[proc];
63     if(p.m_status == "running" && test > 50){
64       ndbout_c("undefine %d: %s (running)", p.m_id, p.m_name.c_str());
65       undefine(p);
66       g_procs.erase(proc);
67       continue;
68     }
69     if(p.m_status == "running" && test <= 50){
70       ndbout_c("stop %d: %s(running)", p.m_id, p.m_name.c_str());
71       stop(p);
72       continue;
73     }
74     if(p.m_status == "stopped" && test > 50){
75       ndbout_c("undefine %d: %s(stopped)", p.m_id, p.m_name.c_str());
76       undefine(p);
77       g_procs.erase(proc);
78       continue;
79     }
80     if(p.m_status == "stopped" && test <= 50){
81       ndbout_c("start %d %s(stopped)", p.m_id, p.m_name.c_str());
82       start(p);
83       continue;
84     }
85     ndbout_c("Unknown: %s", p.m_status.c_str());
86   }
87 }
88 
define()89 void define(){
90   SimpleCpcClient::Process m_proc;
91   m_proc.m_id = -1;
92   m_proc.m_type = "temporary";
93   m_proc.m_owner = "atrt";
94   m_proc.m_group = "group";
95   //m_proc.m_cwd
96   //m_proc.m_env
97   //proc.m_proc.m_stdout = "log.out";
98   //proc.m_proc.m_stderr = "2>&1";
99   //proc.m_proc.m_runas = proc.m_host->m_user;
100   m_proc.m_ulimit = "c:unlimited";
101   if((rand() & 15) >= 0){
102     m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "sleep");
103     m_proc.m_path.assign("/bin/sleep");
104     m_proc.m_args = "600";
105   } else {
106     m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "test.sh");
107     m_proc.m_path.assign("/home/jonas/run/cpcd/test.sh");
108     m_proc.m_args = "600";
109   }
110   g_procs.push_back(m_proc);
111 
112   Properties reply;
113   if(g_client.define_process(g_procs.back(), reply) != 0){
114     ndbout_c("define %s -> ERR", m_proc.m_name.c_str());
115     reply.print();
116     ABORT();
117   }
118   ndbout_c("define %s -> %d", m_proc.m_name.c_str(), m_proc.m_id);
119 }
120 
start(SimpleCpcClient::Process & p)121 void start(SimpleCpcClient::Process & p){
122   Properties reply;
123   if(g_client.start_process(p.m_id, reply) != 0){
124     reply.print();
125     ABORT();
126   }
127 }
128 
stop(SimpleCpcClient::Process & p)129 void stop(SimpleCpcClient::Process & p){
130   Properties reply;
131   if(g_client.stop_process(p.m_id, reply) != 0){
132     reply.print();
133     ABORT();
134   }
135 }
136 
undefine(SimpleCpcClient::Process & p)137 void undefine(SimpleCpcClient::Process & p){
138   Properties reply;
139   if(g_client.undefine_process(p.m_id, reply) != 0){
140     reply.print();
141     ABORT();
142   }
143 }
144 
list()145 void list(){
146   Properties reply;
147   Vector<SimpleCpcClient::Process> procs;
148   if(g_client.list_processes(procs, reply) != 0){
149     reply.print();
150     ABORT();
151   }
152 
153   for(Uint32 i = 0; i<procs.size(); i++){
154     SimpleCpcClient::Process * p = find(procs[i].m_id);
155     if(p != 0){
156       p->m_status = procs[i].m_status;
157     }
158   }
159 }
find(int id)160 SimpleCpcClient::Process* find(int id){
161   for(Uint32 i = 0; i<g_procs.size(); i++){
162     if(g_procs[i].m_id == id)
163       return &g_procs[i];
164   }
165   return 0;
166 }
167