mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / src / common / transporter / SHM_Transporter.unix.cpp
blob6466c57adf79992bb8bab289d0dfb7d5b3ba4455
1 /* Copyright (c) 2003-2005 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
17 #include <ndb_global.h>
19 #include "SHM_Transporter.hpp"
20 #include "TransporterInternalDefinitions.hpp"
21 #include <TransporterCallback.hpp>
22 #include <NdbSleep.h>
23 #include <NdbOut.hpp>
25 #include <sys/ipc.h>
26 #include <sys/shm.h>
28 void SHM_Transporter::make_error_info(char info[], int sz)
30 snprintf(info,sz,"Shm key=%d sz=%d id=%d",
31 shmKey, shmSize, shmId);
34 bool
35 SHM_Transporter::ndb_shm_create()
37 shmId = shmget(shmKey, shmSize, IPC_CREAT | 960);
38 if(shmId == -1) {
39 perror("shmget: ");
40 return false;
42 return true;
45 bool
46 SHM_Transporter::ndb_shm_get()
48 shmId = shmget(shmKey, shmSize, 0);
49 if(shmId == -1) {
50 perror("shmget: ");
51 return false;
53 return true;
56 bool
57 SHM_Transporter::ndb_shm_attach()
59 shmBuf = (char *)shmat(shmId, 0, 0);
60 if(shmBuf == 0) {
61 perror("shmat: ");
62 return false;
64 return true;
67 bool
68 SHM_Transporter::checkConnected(){
69 struct shmid_ds info;
70 const int res = shmctl(shmId, IPC_STAT, &info);
71 if(res == -1){
72 char buf[128];
73 int r= snprintf(buf, sizeof(buf),
74 "shmctl(%d, IPC_STAT) errno: %d(%s). ", shmId,
75 errno, strerror(errno));
76 make_error_info(buf+r, sizeof(buf)-r);
77 DBUG_PRINT("error",(buf));
78 switch (errno)
80 case EACCES:
81 report_error(TE_SHM_IPC_PERMANENT, buf);
82 break;
83 default:
84 report_error(TE_SHM_IPC_STAT, buf);
85 break;
87 return false;
90 if(info.shm_nattch != 2){
91 char buf[128];
92 make_error_info(buf, sizeof(buf));
93 report_error(TE_SHM_DISCONNECT);
94 DBUG_PRINT("error", ("Already connected to node %d",
95 remoteNodeId));
96 return false;
98 return true;
101 void
102 SHM_Transporter::disconnectImpl(){
103 if(_attached){
104 const int res = shmdt(shmBuf);
105 if(res == -1){
106 perror("shmdelete: ");
107 return;
109 _attached = false;
110 if(!isServer && _shmSegCreated)
111 _shmSegCreated = false;
114 if(isServer && _shmSegCreated){
115 const int res = shmctl(shmId, IPC_RMID, 0);
116 if(res == -1){
117 char buf[64];
118 make_error_info(buf, sizeof(buf));
119 report_error(TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
120 return;
122 _shmSegCreated = false;
124 setupBuffersDone=false;