Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit10cee1e

Browse files
committed
Fix Bug: the basic Linux implementation and shm error judgement in SHM_create()
1 parenteb25e27 commit10cee1e

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

‎apache2/ag_mdb/ag_mdb.cpp‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,19 +259,34 @@ int SHM_create(PTR_VOID* new_shm_base, const char* db_name, int db_name_length,
259259
return AGMDB_ERROR_SHM_ENTRY_NUM_NEGATIVE;
260260

261261
shm_id =shmget(shm_key, shm_size, IPC_CREAT | IPC_EXCL);
262-
263-
if(shm_id == -1){// create a new SHM
264-
shm_id =shmget(shm_key, shm_size, IPC_CREAT);
265-
if(shm_id == -1)
266-
return AGMDB_ERROR_SHM_LINUX_CREATE_FAIL;
262+
if(shm_id == -1) {
263+
if(errno == EEXIST) {// There has been a SHM with the given name
264+
shm_id =shmget(shm_key, shm_size, IPC_CREAT);
265+
if(shm_id == -1)
266+
return AGMDB_ERROR_SHM_LINUX_CREATE_FAIL;
267+
268+
// Map the SHM into the address space of this process
269+
*new_shm_base = (PTR_VOID)shmat(shm_id,NULL,0);
270+
if(*new_shm_base == (PTR_VOID)-1)
271+
return AGMDB_ERROR_SHM_LINUX_MAP_FAIL;
272+
return AGMDB_SUCCESS_SHM_OPEN;
273+
}
274+
elseif(errno == EACCES) {
275+
return AGMDB_ERROR_SHM_LINUX_OPEN_ACCESS_FAIL;
276+
}
277+
else {
278+
return AGMDB_ERROR_SHM_LINUX_OPEN_FAIL;
279+
}
280+
}
281+
else {// Map the new SHM into the address space of this process
282+
*new_shm_base = (PTR_VOID)shmat(shm_id,NULL,0);
283+
if(*new_shm_base == (PTR_VOID)-1)
284+
return AGMDB_ERROR_SHM_LINUX_MAP_FAIL;
285+
rc =SHM_init(*new_shm_base, shm_size, entry_num);
286+
if(rc != AGMDB_SUCCESS)
287+
return rc;
288+
return AGMDB_SUCCESS_SHM_CREATE;
267289
}
268-
*new_shm_base = (PTR_VOID)shmat(shm_id,NULL,0);
269-
if(*new_shm_base == (PTR_VOID)-1)
270-
return AGMDB_ERROR_SHM_LINUX_MAP_FAIL;
271-
rc =SHM_init(*new_shm_base, shm_size, entry_num);
272-
if(rc != AGMDB_SUCCESS)
273-
return rc;
274-
return AGMDB_SUCCESS_SHM_OPEN;
275290
#else
276291
HANDLE shm_handle;
277292
if (AGMDB_isstring(db_name, db_name_length) != AGMDB_SUCCESS)
@@ -300,7 +315,7 @@ int SHM_create(PTR_VOID* new_shm_base, const char* db_name, int db_name_length,
300315
return rc;
301316
return AGMDB_SUCCESS_SHM_CREATE;
302317
}
303-
else{// There has been a SHM with the given name
318+
else{// There has been a SHM with the given name
304319
return AGMDB_SUCCESS_SHM_OPEN;
305320
}
306321
#endif
@@ -1126,6 +1141,10 @@ const char* AGMDB_getErrorInfo(int error_no){
11261141

11271142
case AGMDB_ERROR_SHM_LINUX_CREATE_FAIL:
11281143
return"In Linux system, failed when creating the shared memory.";
1144+
case AGMDB_ERROR_SHM_LINUX_OPEN_ACCESS_FAIL:
1145+
return"In Linux system, no access permission when opening the shared memory.";
1146+
case AGMDB_ERROR_SHM_LINUX_OPEN_FAIL:
1147+
return"In Linux system, failed when opening the shared memory.";
11291148
case AGMDB_ERROR_SHM_LINUX_MAP_FAIL:
11301149
return"In Linux system, failed when mapping the shared memory into the process's address space.";
11311150

‎apache2/ag_mdb/ag_mdb_external.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ extern "C" {
5151
#defineAGMDB_ERROR_SHM_ENTRY_NUM_NEGATIVE 11003
5252

5353
#defineAGMDB_ERROR_SHM_LINUX_CREATE_FAIL 11100
54-
#defineAGMDB_ERROR_SHM_LINUX_MAP_FAIL 11101
54+
#defineAGMDB_ERROR_SHM_LINUX_OPEN_FAIL 11101
55+
#defineAGMDB_ERROR_SHM_LINUX_OPEN_ACCESS_FAIL 11102
56+
#defineAGMDB_ERROR_SHM_LINUX_MAP_FAIL 11103
5557

5658
#defineAGMDB_ERROR_SHM_WIN_CREATE_FAIL 11200
5759
#defineAGMDB_ERROR_SHM_WIN_MAP_FAIL 11201

‎apache2/ag_mdb/ag_mdb_internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include<sys/shm.h>
1111
#include<sys/sem.h>
1212
#include<sys/ipc.h>
13+
#include<errno.h>
1314
#endif
1415
#include<cstring>
1516
#include<stdlib.h>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp