forked from percona/percona-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wl#9819 patch #3: Declare & implement ProcessInfo and OwnProcessInfo
- Loading branch information
Showing
5 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; version 2 of the License. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef NDB_OWNPROCESSINFO_HPP | ||
#define NDB_OWNPROCESSINFO_HPP | ||
|
||
class ProcessInfo; | ||
|
||
void setOwnProcessInfoName(const char * pathname); | ||
void setOwnProcessInfoAngelPid(Uint32); | ||
void setOwnProcessInfoServerAddress(const struct sockaddr_in *); | ||
void setOwnProcessInfoPort(Uint16); | ||
|
||
ProcessInfo * getOwnProcessInfo(Uint16 nodeId); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; version 2 of the License. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef NDB_PROCESSINFO_HPP | ||
#define NDB_PROCESSINFO_HPP | ||
|
||
|
||
/* Forward Declarations */ | ||
class ProcessInfoRep; | ||
|
||
/* Class ProcessInfo */ | ||
class ProcessInfo { | ||
friend void getNameFromEnvironment(); | ||
friend ProcessInfo * getOwnProcessInfo(Uint16); | ||
|
||
public: | ||
ProcessInfo(); | ||
~ProcessInfo() {}; | ||
|
||
static ProcessInfo * forNodeId(Uint16); | ||
static void release(ProcessInfo *); | ||
|
||
bool isValid() const; | ||
void invalidate(); | ||
|
||
void setConnectionName(const char *); | ||
void setConnectionName(Uint32 *signal_data); | ||
void setProcessName(const char *); | ||
void setHostAddress(const struct sockaddr *, socklen_t); | ||
void setHostAddress(const struct in_addr *); | ||
void setHostAddress(Uint32 *signal_data); | ||
void setHostAddress(const char *); | ||
void setPid(); | ||
void setAngelPid(Uint32 pid); | ||
void setPort(Uint16); | ||
void setNodeId(Uint16); | ||
const char * getConnectionName() const { return connection_name; }; | ||
const char * getProcessName() const { return process_name; }; | ||
const char * getHostAddress() const; | ||
int getPid() const; | ||
int getAngelPid() const { return angel_process_id; }; | ||
int getPort() const { return application_port; }; | ||
int getNodeId() const { return node_id; }; | ||
|
||
|
||
/* Interface for Qmgr to build ProcessInfo for remote node | ||
from received signal */ | ||
void initializeFromProcessInfoRep(ProcessInfoRep *); | ||
|
||
STATIC_CONST( ConnectionNameLength = 128 ); | ||
STATIC_CONST( ConnectionNameLengthInWords = 32); | ||
STATIC_CONST( ProcessNameLength = 48 ); | ||
STATIC_CONST( AddressStringLength = 48 ); // Long enough for IPv6 | ||
STATIC_CONST( AddressStringLengthInWords = 12); | ||
|
||
// /* Interface for ClusterManager to create signal */ | ||
// void buildProcessInfoReport(ProcessInfoRep *); | ||
// | ||
|
||
private: /* Data Members */ | ||
char connection_name[ConnectionNameLength]; | ||
char host_address[AddressStringLength]; | ||
char process_name[ProcessNameLength]; | ||
Uint32 node_id; | ||
Uint32 process_id; | ||
Uint32 angel_process_id; | ||
Uint32 application_port; | ||
}; // 240 bytes per node | ||
|
||
|
||
inline bool ProcessInfo::isValid() const { | ||
return process_id; | ||
} | ||
|
||
inline const char * ProcessInfo::getHostAddress() const { | ||
return host_address[0] ? host_address : 0; | ||
} | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; version 2 of the License. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "ndb_global.h" | ||
#include "ProcessInfo.hpp" | ||
#include "OwnProcessInfo.hpp" | ||
#include <EventLogger.hpp> | ||
#include <NdbMutex.h> | ||
|
||
extern EventLogger * g_eventLogger; | ||
const char * ndb_basename(const char *path); | ||
|
||
extern "C" { | ||
extern const char * my_progname; | ||
} | ||
|
||
/* Static storage; constructor called at process startup by C++ runtime. */ | ||
ProcessInfo singletonInfo; | ||
NdbLockable theApiMutex; | ||
|
||
/* Public API | ||
* | ||
*/ | ||
void setOwnProcessInfoName(const char *pathname) | ||
{ | ||
theApiMutex.lock(); | ||
singletonInfo.setProcessName(ndb_basename(pathname)); | ||
theApiMutex.unlock(); | ||
} | ||
|
||
void setOwnProcessInfoAngelPid(Uint32 pid) | ||
{ | ||
theApiMutex.lock(); | ||
singletonInfo.setAngelPid(pid); | ||
theApiMutex.unlock(); | ||
} | ||
|
||
void setOwnProcessInfoServerAddress(const struct sockaddr_in * addr) | ||
{ | ||
theApiMutex.lock(); | ||
singletonInfo.setHostAddress((const struct sockaddr *) addr, sizeof(addr)); | ||
theApiMutex.unlock(); | ||
} | ||
|
||
void setOwnProcessInfoPort(Uint16 port) | ||
{ | ||
theApiMutex.lock(); | ||
singletonInfo.setPort(port); | ||
theApiMutex.unlock(); | ||
} | ||
|
||
|
||
/* Fill in missing parts of ProcessInfo before providing it to QMgr | ||
or ClusterMgr | ||
*/ | ||
|
||
#ifdef WIN32 | ||
#include "psapi.h" | ||
|
||
void getNameFromEnvironment() | ||
{ | ||
HANDLE handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, | ||
FALSE, singletonInfo.getPid()); | ||
GetModuleFileNameEx(handle, 0, singletonInfo.process_name, | ||
singletonInfo.ProcessNameLength); | ||
} | ||
#else | ||
void getNameFromEnvironment() | ||
{ | ||
const char * path = getenv("_"); | ||
if(path) | ||
{ | ||
singletonInfo.setProcessName(ndb_basename(path)); | ||
} | ||
} | ||
#endif | ||
|
||
|
||
/* On unix only, if we are not a daemon, and also not a process group leader, | ||
set parent pid as angel pid. | ||
*/ | ||
static Uint32 getParentPidAsAngel() | ||
{ | ||
#ifndef WIN32 | ||
pid_t parent_process_id = getppid(); | ||
if((parent_process_id != 1) && (getpgrp() != singletonInfo.getPid())) | ||
{ | ||
return parent_process_id; | ||
} | ||
#endif | ||
return 0; | ||
} | ||
|
||
|
||
/* Public API for QMgr and ClusterMgr. | ||
*/ | ||
ProcessInfo * getOwnProcessInfo(Uint16 nodeId) { | ||
Guard locked(theApiMutex); | ||
if(singletonInfo.process_id == 0) | ||
{ | ||
/* Finalize */ | ||
singletonInfo.setPid(); | ||
singletonInfo.node_id = nodeId; | ||
if(singletonInfo.angel_process_id == 0) | ||
singletonInfo.angel_process_id = getParentPidAsAngel(); | ||
if(singletonInfo.process_name[0] == 0) | ||
{ | ||
if(my_progname) | ||
singletonInfo.setProcessName(ndb_basename(my_progname)); | ||
else | ||
getNameFromEnvironment(); | ||
} | ||
|
||
g_eventLogger->info("getOwnProcessInfo: pid %d, name %s, addr %s, node %d", | ||
singletonInfo.getPid(), singletonInfo.getProcessName(), | ||
singletonInfo.getHostAddress(), singletonInfo.getNodeId()); | ||
} | ||
|
||
return & singletonInfo; | ||
} |
Oops, something went wrong.