1 /*
2 Copyright (C) 2003-2006 MySQL AB
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
29 #include "SHM_Transporter.hpp"
30 #include "TransporterInternalDefinitions.hpp"
31 #include <TransporterCallback.hpp>
32 #include <NdbSleep.h>
33 #include <NdbOut.hpp>
34
35 #include <sys/ipc.h>
36 #include <sys/shm.h>
37
make_error_info(char info[],int sz)38 void SHM_Transporter::make_error_info(char info[], int sz)
39 {
40 snprintf(info,sz,"Shm key=%d sz=%d id=%d",
41 shmKey, shmSize, shmId);
42 }
43
44 bool
ndb_shm_create()45 SHM_Transporter::ndb_shm_create()
46 {
47 shmId = shmget(shmKey, shmSize, IPC_CREAT | 960);
48 if(shmId == -1) {
49 perror("shmget: ");
50 return false;
51 }
52 return true;
53 }
54
55 bool
ndb_shm_get()56 SHM_Transporter::ndb_shm_get()
57 {
58 shmId = shmget(shmKey, shmSize, 0);
59 if(shmId == -1) {
60 perror("shmget: ");
61 return false;
62 }
63 return true;
64 }
65
66 bool
ndb_shm_attach()67 SHM_Transporter::ndb_shm_attach()
68 {
69 shmBuf = (char *)shmat(shmId, 0, 0);
70 if(shmBuf == 0) {
71 perror("shmat: ");
72 return false;
73 }
74 return true;
75 }
76
77 bool
checkConnected()78 SHM_Transporter::checkConnected(){
79 struct shmid_ds info;
80 const int res = shmctl(shmId, IPC_STAT, &info);
81 if(res == -1){
82 char buf[128];
83 int r= snprintf(buf, sizeof(buf),
84 "shmctl(%d, IPC_STAT) errno: %d(%s). ", shmId,
85 errno, strerror(errno));
86 make_error_info(buf+r, sizeof(buf)-r);
87 DBUG_PRINT("error",("%s", buf));
88 switch (errno)
89 {
90 case EACCES:
91 report_error(TE_SHM_IPC_PERMANENT, buf);
92 break;
93 default:
94 report_error(TE_SHM_IPC_STAT, buf);
95 break;
96 }
97 return false;
98 }
99
100 if(info.shm_nattch != 2){
101 char buf[128];
102 make_error_info(buf, sizeof(buf));
103 report_error(TE_SHM_DISCONNECT);
104 DBUG_PRINT("error", ("Already connected to node %d",
105 remoteNodeId));
106 return false;
107 }
108 return true;
109 }
110
111 void
disconnectImpl()112 SHM_Transporter::disconnectImpl(){
113 if(_attached){
114 const int res = shmdt(shmBuf);
115 if(res == -1){
116 perror("shmdelete: ");
117 return;
118 }
119 _attached = false;
120 if(!isServer && _shmSegCreated)
121 _shmSegCreated = false;
122 }
123
124 if(isServer && _shmSegCreated){
125 const int res = shmctl(shmId, IPC_RMID, 0);
126 if(res == -1){
127 char buf[64];
128 make_error_info(buf, sizeof(buf));
129 report_error(TE_SHM_UNABLE_TO_REMOVE_SEGMENT);
130 return;
131 }
132 _shmSegCreated = false;
133 }
134 setupBuffersDone=false;
135 }
136