Skip to content

Commit 718244e

Browse files
committed
wl#9819 patch #3: Declare & implement ProcessInfo and OwnProcessInfo
1 parent 92f3f38 commit 718244e

File tree

5 files changed

+451
-0
lines changed

5 files changed

+451
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright (c) 2016, 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 as published by
6+
the Free Software Foundation; version 2 of the License.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*/
17+
18+
#ifndef NDB_OWNPROCESSINFO_HPP
19+
#define NDB_OWNPROCESSINFO_HPP
20+
21+
class ProcessInfo;
22+
23+
void setOwnProcessInfoName(const char * pathname);
24+
void setOwnProcessInfoAngelPid(Uint32);
25+
void setOwnProcessInfoServerAddress(const struct sockaddr_in *);
26+
void setOwnProcessInfoPort(Uint16);
27+
28+
ProcessInfo * getOwnProcessInfo(Uint16 nodeId);
29+
30+
#endif
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright (c) 2016, 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 as published by
6+
the Free Software Foundation; version 2 of the License.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*/
17+
18+
#ifndef NDB_PROCESSINFO_HPP
19+
#define NDB_PROCESSINFO_HPP
20+
21+
22+
/* Forward Declarations */
23+
class ProcessInfoRep;
24+
25+
/* Class ProcessInfo */
26+
class ProcessInfo {
27+
friend void getNameFromEnvironment();
28+
friend ProcessInfo * getOwnProcessInfo(Uint16);
29+
30+
public:
31+
ProcessInfo();
32+
~ProcessInfo() {};
33+
34+
static ProcessInfo * forNodeId(Uint16);
35+
static void release(ProcessInfo *);
36+
37+
bool isValid() const;
38+
void invalidate();
39+
40+
void setConnectionName(const char *);
41+
void setConnectionName(Uint32 *signal_data);
42+
void setProcessName(const char *);
43+
void setHostAddress(const struct sockaddr *, socklen_t);
44+
void setHostAddress(const struct in_addr *);
45+
void setHostAddress(Uint32 *signal_data);
46+
void setHostAddress(const char *);
47+
void setPid();
48+
void setAngelPid(Uint32 pid);
49+
void setPort(Uint16);
50+
void setNodeId(Uint16);
51+
const char * getConnectionName() const { return connection_name; };
52+
const char * getProcessName() const { return process_name; };
53+
const char * getHostAddress() const;
54+
int getPid() const;
55+
int getAngelPid() const { return angel_process_id; };
56+
int getPort() const { return application_port; };
57+
int getNodeId() const { return node_id; };
58+
59+
60+
/* Interface for Qmgr to build ProcessInfo for remote node
61+
from received signal */
62+
void initializeFromProcessInfoRep(ProcessInfoRep *);
63+
64+
STATIC_CONST( ConnectionNameLength = 128 );
65+
STATIC_CONST( ConnectionNameLengthInWords = 32);
66+
STATIC_CONST( ProcessNameLength = 48 );
67+
STATIC_CONST( AddressStringLength = 48 ); // Long enough for IPv6
68+
STATIC_CONST( AddressStringLengthInWords = 12);
69+
70+
// /* Interface for ClusterManager to create signal */
71+
// void buildProcessInfoReport(ProcessInfoRep *);
72+
//
73+
74+
private: /* Data Members */
75+
char connection_name[ConnectionNameLength];
76+
char host_address[AddressStringLength];
77+
char process_name[ProcessNameLength];
78+
Uint32 node_id;
79+
Uint32 process_id;
80+
Uint32 angel_process_id;
81+
Uint32 application_port;
82+
}; // 240 bytes per node
83+
84+
85+
inline bool ProcessInfo::isValid() const {
86+
return process_id;
87+
}
88+
89+
inline const char * ProcessInfo::getHostAddress() const {
90+
return host_address[0] ? host_address : 0;
91+
}
92+
93+
94+
#endif

storage/ndb/src/common/util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ ADD_CONVENIENCE_LIBRARY(ndbgeneral
6262
CharsetMap.cpp
6363
CharsetMapImpl.cpp
6464
parse_mask.cpp
65+
ProcessInfo.cpp
66+
OwnProcessInfo.cpp
6567
)
6668
SET(NDBGENERAL_LIBS ndbtrace ${ZLIB_LIBRARY} mysys mysys_ssl)
6769
TARGET_LINK_LIBRARIES(ndbgeneral ${NDBGENERAL_LIBS})
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Copyright (c) 2016, 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 as published by
6+
the Free Software Foundation; version 2 of the License.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16+
*/
17+
18+
#include "ndb_global.h"
19+
#include "ProcessInfo.hpp"
20+
#include "OwnProcessInfo.hpp"
21+
#include <EventLogger.hpp>
22+
#include <NdbMutex.h>
23+
24+
extern EventLogger * g_eventLogger;
25+
const char * ndb_basename(const char *path);
26+
27+
extern "C" {
28+
extern const char * my_progname;
29+
}
30+
31+
/* Static storage; constructor called at process startup by C++ runtime. */
32+
ProcessInfo singletonInfo;
33+
NdbLockable theApiMutex;
34+
35+
/* Public API
36+
*
37+
*/
38+
void setOwnProcessInfoName(const char *pathname)
39+
{
40+
theApiMutex.lock();
41+
singletonInfo.setProcessName(ndb_basename(pathname));
42+
theApiMutex.unlock();
43+
}
44+
45+
void setOwnProcessInfoAngelPid(Uint32 pid)
46+
{
47+
theApiMutex.lock();
48+
singletonInfo.setAngelPid(pid);
49+
theApiMutex.unlock();
50+
}
51+
52+
void setOwnProcessInfoServerAddress(const struct sockaddr_in * addr)
53+
{
54+
theApiMutex.lock();
55+
singletonInfo.setHostAddress((const struct sockaddr *) addr, sizeof(addr));
56+
theApiMutex.unlock();
57+
}
58+
59+
void setOwnProcessInfoPort(Uint16 port)
60+
{
61+
theApiMutex.lock();
62+
singletonInfo.setPort(port);
63+
theApiMutex.unlock();
64+
}
65+
66+
67+
/* Fill in missing parts of ProcessInfo before providing it to QMgr
68+
or ClusterMgr
69+
*/
70+
71+
#ifdef WIN32
72+
#include "psapi.h"
73+
74+
void getNameFromEnvironment()
75+
{
76+
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
77+
FALSE, singletonInfo.getPid());
78+
GetModuleFileNameEx(handle, 0, singletonInfo.process_name,
79+
singletonInfo.ProcessNameLength);
80+
}
81+
#else
82+
void getNameFromEnvironment()
83+
{
84+
const char * path = getenv("_");
85+
if(path)
86+
{
87+
singletonInfo.setProcessName(ndb_basename(path));
88+
}
89+
}
90+
#endif
91+
92+
93+
/* On unix only, if we are not a daemon, and also not a process group leader,
94+
set parent pid as angel pid.
95+
*/
96+
static Uint32 getParentPidAsAngel()
97+
{
98+
#ifndef WIN32
99+
pid_t parent_process_id = getppid();
100+
if((parent_process_id != 1) && (getpgrp() != singletonInfo.getPid()))
101+
{
102+
return parent_process_id;
103+
}
104+
#endif
105+
return 0;
106+
}
107+
108+
109+
/* Public API for QMgr and ClusterMgr.
110+
*/
111+
ProcessInfo * getOwnProcessInfo(Uint16 nodeId) {
112+
Guard locked(theApiMutex);
113+
if(singletonInfo.process_id == 0)
114+
{
115+
/* Finalize */
116+
singletonInfo.setPid();
117+
singletonInfo.node_id = nodeId;
118+
if(singletonInfo.angel_process_id == 0)
119+
singletonInfo.angel_process_id = getParentPidAsAngel();
120+
if(singletonInfo.process_name[0] == 0)
121+
{
122+
if(my_progname)
123+
singletonInfo.setProcessName(ndb_basename(my_progname));
124+
else
125+
getNameFromEnvironment();
126+
}
127+
128+
g_eventLogger->info("getOwnProcessInfo: pid %d, name %s, addr %s, node %d",
129+
singletonInfo.getPid(), singletonInfo.getProcessName(),
130+
singletonInfo.getHostAddress(), singletonInfo.getNodeId());
131+
}
132+
133+
return & singletonInfo;
134+
}

0 commit comments

Comments
 (0)