Skip to content

Added support for setting a group for SysV Shared Memory #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ext/sysvshm/php_sysvshm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
extern zend_module_entry sysvshm_module_entry;
#define sysvshm_module_ptr &sysvshm_module_entry

#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <grp.h>

#define PHP_SHM_RSRC_NAME "sysvshm"

Expand Down Expand Up @@ -58,6 +60,13 @@ typedef struct {
sysvshm_chunk_head *ptr; /* memory address of shared memory */
} sysvshm_shm;

typedef struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It's already defined in grp.h

char *gr_name; /* group name */
char *gr_passwd; /* group password */
gid_t gr_gid; /* group ID */
char **gr_mem; /* group members */
} group;

PHP_MINIT_FUNCTION(sysvshm);
PHP_FUNCTION(shm_attach);
PHP_FUNCTION(shm_detach);
Expand Down
30 changes: 30 additions & 0 deletions ext/sysvshm/sysvshm.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_attach, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, memsize)
ZEND_ARG_INFO(0, perm)
ZEND_ARG_INFO(0, grp)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_shm_detach, 0, 0, 1)
Expand Down Expand Up @@ -149,8 +150,12 @@ PHP_FUNCTION(shm_attach)
{
sysvshm_shm *shm_list_ptr;
char *shm_ptr;
char *group_name = NULL;
int grp_len;
sysvshm_chunk_head *chunk_ptr;
long shm_key, shm_id, shm_size = php_sysvshm.init_mem, shm_flag = 0666;
struct group *group_info;
struct shmid_ds shm_info;

if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|ll", &shm_key, &shm_size, &shm_flag)) {
return;
Expand All @@ -177,6 +182,31 @@ PHP_FUNCTION(shm_attach)
}
}

if ( group_name != NULL ) {
if (shmctl(shm_id, IPC_STAT, &shm_info) == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to stat SHM id %ld: %s", shm_id, strerror(errno));
efree(shm_list_ptr);
RETURN_FALSE;
}

group_info = emalloc(sizeof(group));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wat? That doesn't look right at all, you allocate memory, overwrite the pointer with the return value of getgrnam and then even call efree on NULL.

if ( !(group_info = getgrnam(group_name)) ) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to get group ID for group %s: %s", group_name, strerror(errno));
efree(group_info);
efree(shm_list_ptr);
RETURN_FALSE;
}

shm_info.shm_perm.gid = group_info->gr_gid;
efree(group_info);

if (shmctl(shm_id, IPC_SET, &shm_info) == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to set group to SHM id %ld: %s", shm_id, strerror(errno));
efree(shm_list_ptr);
RETURN_FALSE;
}
}

if ((shm_ptr = shmat(shm_id, NULL, 0)) == (void *) -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", shm_key, strerror(errno));
efree(shm_list_ptr);
Expand Down
4 changes: 2 additions & 2 deletions ext/sysvshm/tests/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ shm_attach() tests
$key = ftok(__FILE__, 't');

var_dump(shm_attach());
var_dump(shm_attach(1,2,3,4));
var_dump(shm_attach(1,2,3,4,5));

var_dump(shm_attach(-1, 0));
var_dump(shm_attach(0, -1));
Expand Down Expand Up @@ -36,7 +36,7 @@ echo "Done\n";
Warning: shm_attach() expects at least 1 parameter, 0 given in %s on line %d
NULL

Warning: shm_attach() expects at most 3 parameters, 4 given in %s on line %d
Warning: shm_attach() expects at most 4 parameters, 5 given in %s on line %d
NULL

Warning: shm_attach(): Segment size must be greater than zero in %s on line %d
Expand Down