1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "structmember.h"
#include "common.h"
#include "memory.h"
/****************** Internal use only **********************/
PyObject *
shm_str(SharedMemory *self) {
return PyUnicode_FromFormat("Key=%ld, id=%d", (long)self->key, self->id);
}
PyObject *
shm_repr(SharedMemory *self) {
return PyUnicode_FromFormat("sysv_ipc.SharedMemory(%ld)", (long)self->key);
}
PyObject *
shm_attach(SharedMemory *self, void *address, int shmat_flags) {
DPRINTF("attaching memory @ address %p with id %d using flags 0x%x\n",
address, self->id, shmat_flags);
self->address = shmat(self->id, address, shmat_flags);
if ((void *)-1 == self->address) {
self->address = NULL;
switch (errno) {
case EACCES:
PyErr_SetString(pPermissionsException, "No permission to attach");
break;
case ENOMEM:
PyErr_SetString(PyExc_MemoryError, "Not enough memory");
break;
case EINVAL:
PyErr_SetString(PyExc_ValueError, "Invalid id, address, or flags");
break;
default:
PyErr_SetFromErrno(PyExc_OSError);
break;
}
goto error_return;
}
else {
// memory was attached successfully
self->read_only = (shmat_flags & SHM_RDONLY) ? 1 : 0;
DPRINTF("set memory's internal read_only flag to %d\n", self->read_only);
}
Py_RETURN_NONE;
error_return:
return NULL;
}
PyObject *
shm_remove(int shared_memory_id) {
struct shmid_ds shm_info;
DPRINTF("removing shm with id %d\n", shared_memory_id);
if (-1 == shmctl(shared_memory_id, IPC_RMID, &shm_info)) {
switch (errno) {
case EIDRM:
case EINVAL:
PyErr_Format(pExistentialException,
"No shared memory with id %d exists",
shared_memory_id);
break;
case EPERM:
PyErr_SetString(pPermissionsException,
"You do not have permission to remove the shared memory");
break;
default:
PyErr_SetFromErrno(PyExc_OSError);
break;
}
goto error_return;
}
Py_RETURN_NONE;
error_return:
return NULL;
}
static PyObject *
shm_get_value(int shared_memory_id, enum GET_SET_IDENTIFIERS field) {
// Gets one of the values in GET_SET_IDENTIFIERS and returns it as a boxed Python int or long.
// The caller assumes responsibility for the reference.
// If an error occurs, sets the Python error and returns NULL.
struct shmid_ds shm_info;
PyObject *py_value = NULL;
DPRINTF("Calling shmctl(...IPC_STAT...), field = %d\n", field);
if (-1 == shmctl(shared_memory_id, IPC_STAT, &shm_info)) {
switch (errno) {
case EIDRM:
case EINVAL:
PyErr_Format(pExistentialException,
"No shared memory with id %d exists",
shared_memory_id);
break;
case EACCES:
PyErr_SetString(pPermissionsException,
"You do not have permission to read the shared memory attribute");
break;
default:
PyErr_SetFromErrno(PyExc_OSError);
break;
}
goto error_return;
}
switch (field) {
case SVIFP_SHM_SIZE:
py_value = SIZE_T_TO_PY(shm_info.shm_segsz);
break;
case SVIFP_SHM_LAST_ATTACH_TIME:
py_value = TIME_T_TO_PY(shm_info.shm_atime);
break;
case SVIFP_SHM_LAST_DETACH_TIME:
py_value = TIME_T_TO_PY(shm_info.shm_dtime);
break;
case SVIFP_SHM_LAST_CHANGE_TIME:
py_value = TIME_T_TO_PY(shm_info.shm_ctime);
break;
case SVIFP_SHM_CREATOR_PID:
py_value = PID_T_TO_PY(shm_info.shm_cpid);
break;
case SVIFP_SHM_LAST_AT_DT_PID:
py_value = PID_T_TO_PY(shm_info.shm_lpid);
break;
case SVIFP_SHM_NUMBER_ATTACHED:
// shm_nattch is unsigned
// ref: http://www.opengroup.org/onlinepubs/007908799/xsh/sysshm.h.html
py_value = PyLong_FromUnsignedLong(shm_info.shm_nattch);
break;
case SVIFP_IPC_PERM_UID:
py_value = UID_T_TO_PY(shm_info.shm_perm.uid);
break;
case SVIFP_IPC_PERM_GID:
py_value = GID_T_TO_PY(shm_info.shm_perm.gid);
break;
case SVIFP_IPC_PERM_CUID:
py_value = UID_T_TO_PY(shm_info.shm_perm.cuid);
break;
case SVIFP_IPC_PERM_CGID:
py_value = GID_T_TO_PY(shm_info.shm_perm.cgid);
break;
case SVIFP_IPC_PERM_MODE:
py_value = MODE_T_TO_PY(shm_info.shm_perm.mode);
break;
default:
PyErr_Format(pInternalException, "Bad field %d passed to shm_get_value", field);
goto error_return;
break;
}
return py_value;
error_return:
return NULL;
}
static int
shm_set_ipc_perm_value(int id, enum GET_SET_IDENTIFIERS field, union ipc_perm_value value) {
struct shmid_ds shm_info;
if (-1 == shmctl(id, IPC_STAT, &shm_info)) {
switch (errno) {
case EIDRM:
case EINVAL:
PyErr_Format(pExistentialException,
"No shared memory with id %d exists", id);
break;
case EACCES:
PyErr_SetString(pPermissionsException,
"You do not have permission to read the shared memory attribute");
break;
default:
PyErr_SetFromErrno(PyExc_OSError);
break;
}
goto error_return;
}
switch (field) {
case SVIFP_IPC_PERM_UID:
shm_info.shm_perm.uid = value.uid;
break;
case SVIFP_IPC_PERM_GID:
shm_info.shm_perm.gid = value.gid;
break;
case SVIFP_IPC_PERM_MODE:
shm_info.shm_perm.mode = value.mode;
break;
default:
PyErr_Format(pInternalException,
"Bad field %d passed to shm_set_ipc_perm_value",
field);
goto error_return;
break;
}
if (-1 == shmctl(id, IPC_SET, &shm_info)) {
switch (errno) {
case EIDRM:
case EINVAL:
PyErr_Format(pExistentialException,
"No shared memory with id %d exists", id);
break;
case EPERM:
PyErr_SetString(pPermissionsException,
"You do not have permission to change the shared memory's attributes");
break;
default:
PyErr_SetFromErrno(PyExc_OSError);
break;
}
goto error_return;
}
return 0;
error_return:
return -1;
}
int
shm_get_buffer(SharedMemory *self, Py_buffer *view, int flags)
// Implementation of buffer interface (getbufferproc). The buffer implementation in Python 3.x was
// backported to 2.7, and the 3.x documentation is more complete and easier to understand.
// https://docs.python.org/3/c-api/typeobj.html#buffer-structs
{
PyObject *py_size = shm_get_value(self->id, SVIFP_SHM_SIZE);
Py_ssize_t size;
if (!py_size) {
// If shm_get_value() failed, the Python error will already be set so there's no
// need for me to set it here.
return -1;
}
else {
size = PyLong_AsSsize_t(py_size);
Py_DECREF(py_size);
return PyBuffer_FillInfo(view,
(PyObject *)self,
self->address,
size,
0,
flags);
}
}
/****************** Class methods **********************/
void
SharedMemory_dealloc(SharedMemory *self) {
Py_TYPE(self)->tp_free((PyObject*)self);
}
PyObject *
SharedMemory_new(PyTypeObject *type, PyObject *args, PyObject *kwlist) {
SharedMemory *self;
self = (SharedMemory *)type->tp_alloc(type, 0);
if (NULL != self) {
self->key = (key_t)-1;
self->id = 0;
self->read_only = 0;
self->address = NULL;
}
return (PyObject *)self;
}
int
SharedMemory_init(SharedMemory *self, PyObject *args, PyObject *keywords) {
NoneableKey key;
int mode = 0600;
unsigned long size = 0;
int shmget_flags = 0;
int shmat_flags = 0;
char init_character = ' ';
char *keyword_list[ ] = {"key", "flags", "mode", "size", "init_character", NULL};
PyObject *py_size = NULL;
DPRINTF("Inside SharedMemory_init()\n");
if (!PyArg_ParseTupleAndKeywords(args, keywords, "O&|iikc", keyword_list,
&convert_key_param, &key,
&shmget_flags, &mode, &size,
&init_character))
goto error_return;
mode &= 0777;
shmget_flags &= ~0777;
DPRINTF("key is none = %d, key value = %ld\n", key.is_none, (long)key.value);
if ( !(shmget_flags & IPC_CREAT) && (shmget_flags & IPC_EXCL) ) {
PyErr_SetString(PyExc_ValueError,
"IPC_EXCL must be combined with IPC_CREAT");
goto error_return;
}
if (key.is_none && ((shmget_flags & IPC_EXCL) != IPC_EXCL)) {
PyErr_SetString(PyExc_ValueError,
"Key can only be None if IPC_EXCL is set");
goto error_return;
}
// When creating a new segment, the default size is PAGE_SIZE.
if (((shmget_flags & IPC_CREX) == IPC_CREX) && (!size))
size = PAGE_SIZE;
if (key.is_none) {
// (key == None) ==> generate a key for the caller
do {
errno = 0;
self->key = get_random_key();
DPRINTF("Calling shmget, key=%ld, size=%lu, mode=%o, flags=0x%x\n",
(long)self->key, size, mode, shmget_flags);
self->id = shmget(self->key, size, mode | shmget_flags);
} while ( (-1 == self->id) && (EEXIST == errno) );
}
else {
// (key != None) ==> use key supplied by the caller
self->key = key.value;
DPRINTF("Calling shmget, key=%ld, size=%lu, mode=%o, flags=0x%x\n",
(long)self->key, size, mode, shmget_flags);
self->id = shmget(self->key, size, mode | shmget_flags);
}
DPRINTF("id == %d\n", self->id);
if (self->id == -1) {
switch (errno) {
case EACCES:
PyErr_Format(pPermissionsException,
"Permission %o cannot be granted on the existing segment",
mode);
break;
case EEXIST:
PyErr_Format(pExistentialException,
"Shared memory with the key %ld already exists",
(long)self->key);
break;
case ENOENT:
PyErr_Format(pExistentialException,
"No shared memory exists with the key %ld", (long)self->key);
break;
case EINVAL:
PyErr_SetString(PyExc_ValueError, "The size is invalid");
break;
case ENOMEM:
PyErr_SetString(PyExc_MemoryError, "Not enough memory");
break;
case ENOSPC:
PyErr_SetString(PyExc_OSError,
"Not enough shared memory identifiers available (ENOSPC)");
break;
default:
PyErr_SetFromErrno(PyExc_OSError);
break;
}
goto error_return;
}
// Attach the memory. If no write permissions requested, attach read-only.
shmat_flags = (mode & 0200) ? 0 : SHM_RDONLY;
if (NULL == shm_attach(self, NULL, shmat_flags)) {
// Bad news, something went wrong.
goto error_return;
}
if ( ((shmget_flags & IPC_CREX) == IPC_CREX) && (!(shmat_flags & SHM_RDONLY)) ) {
// Initialize the memory.
py_size = shm_get_value(self->id, SVIFP_SHM_SIZE);
if (!py_size)
goto error_return;
else {
size = PyLong_AsUnsignedLongMask(py_size);
DPRINTF("memsetting address %p to %lu bytes of ASCII 0x%x (%c)\n", \
self->address, size, (int)init_character, init_character);
memset(self->address, init_character, size);
}
Py_DECREF(py_size);
}
return 0;
error_return:
return -1;
}
PyObject *
SharedMemory_attach(SharedMemory *self, PyObject *args, PyObject *keywords) {
PyObject *py_address = NULL;
void *address = NULL;
int flags = 0;
static char *keyword_list[ ] = {"address", "flags", NULL};
DPRINTF("Inside SharedMemory_attach()\n");
if (!PyArg_ParseTupleAndKeywords(args, keywords, "|Oi", keyword_list,
&py_address, &flags))
goto error_return;
if ((!py_address) || (py_address == Py_None))
address = NULL;
else {
if (PyLong_Check(py_address))
address = PyLong_AsVoidPtr(py_address);
else {
PyErr_SetString(PyExc_TypeError, "address must be a long");
goto error_return;
}
}
return shm_attach(self, address, flags);
error_return:
return NULL;
}
PyObject *
SharedMemory_detach(SharedMemory *self) {
if (-1 == shmdt(self->address)) {
self->address = NULL;
switch (errno) {
case EINVAL:
PyErr_SetNone(pNotAttachedException);
break;
default:
PyErr_SetFromErrno(PyExc_OSError);
break;
}
goto error_return;
}
self->address = NULL;
Py_RETURN_NONE;
error_return:
return NULL;
}
PyObject *
SharedMemory_read(SharedMemory *self, PyObject *args, PyObject *keywords) {
/* Tricky business here. A memory segment's size is a size_t which is
ulong or smaller. However, the largest string that Python can
construct is of ssize_t which is long or smaller. Therefore, the
size and offset variables must be ulongs while the byte_count
must be a long (and must not exceed LONG_MAX).
Mind your math!
*/
long byte_count = 0;
unsigned long offset = 0;
unsigned long size;
PyObject *py_size;
char *keyword_list[ ] = {"byte_count", "offset", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywords, "|lk", keyword_list,
&byte_count, &offset))
goto error_return;
if (self->address == NULL) {
PyErr_SetString(pNotAttachedException,
"Read attempt on unattached memory segment");
goto error_return;
}
if ( (py_size = shm_get_value(self->id, SVIFP_SHM_SIZE)) ) {
size = PyLong_AsUnsignedLongMask(py_size);
Py_DECREF(py_size);
}
else
goto error_return;
DPRINTF("offset = %lu, byte_count = %ld, size = %lu\n",
offset, byte_count, size);
if (offset >= size) {
PyErr_SetString(PyExc_ValueError, "The offset must be less than the segment size");
goto error_return;
}
if (byte_count < 0) {
PyErr_SetString(PyExc_ValueError, "The byte_count cannot be negative");
goto error_return;
}
/* If the caller didn't specify a byte count or specified one that would
read past the end of the segment, return everything from the offset to
the end of the segment.
Be careful here not to express the second if condition w/addition, e.g.
(byte_count + offset > size)
It might be more intuitive but since byte_count is a long and offset
is a ulong, their sum could cause an arithmetic overflow. */
if ((!byte_count) || ((unsigned long)byte_count > size - offset)) {
// byte_count needs to be calculated
if (size - offset <= (unsigned long)PY_STRING_LENGTH_MAX)
byte_count = size - offset;
else {
// Caller is asking for more bytes than I can stuff into
// a Python string.
PyErr_Format(PyExc_ValueError,
"The byte_count cannot exceed Python's max string length %ld",
(long)PY_STRING_LENGTH_MAX);
goto error_return;
}
}
return PyBytes_FromStringAndSize(self->address + offset, byte_count);
error_return:
return NULL;
}
PyObject *
SharedMemory_write(SharedMemory *self, PyObject *args, PyObject *kw) {
/* See comments for read() regarding "size issues". Note that here
Python provides the byte_count so it can't be negative.
In Python >= 2.5, the Python argument specifier 's#' expects a
py_ssize_t for its second parameter. A long is long enough. It might
be too big, though, on platforms where a long is larger than
py_ssize_t. Therefore I *must* initialize it to 0 so that whatever
Python doesn't write to is zeroed out.
*/
unsigned long offset = 0;
unsigned long size;
PyObject *py_size;
char *keyword_list[ ] = {"s", "offset", NULL};
static char args_format[] = "s*|k";
Py_buffer data;
if (!PyArg_ParseTupleAndKeywords(args, kw, args_format, keyword_list,
&data,
&offset))
goto error_return;
if (self->read_only) {
PyErr_SetString(PyExc_OSError, "Write attempt on read-only memory segment");
goto error_return;
}
if (self->address == NULL) {
PyErr_SetString(pNotAttachedException, "Write attempt on unattached memory segment");
goto error_return;
}
if ( (py_size = shm_get_value(self->id, SVIFP_SHM_SIZE)) ) {
size = PyLong_AsUnsignedLongMask(py_size);
Py_DECREF(py_size);
}
else
goto error_return;
DPRINTF("write size check; size=%lu, offset=%lu, dat.len=%ld\n",
size, offset, data.len);
// Remember that offset and size are both ulongs, so size > offset, then
// size - offset (as in the second part of the if expression) will evaluate
// to a "negative" number which is a very large ulong.
if ((offset > size) || ((unsigned long)data.len > size - offset)) {
PyErr_SetString(PyExc_ValueError, "Attempt to write past end of memory segment");
goto error_return;
}
memcpy((self->address + offset), data.buf, data.len);
PyBuffer_Release(&data);
Py_RETURN_NONE;
error_return:
PyBuffer_Release(&data);
return NULL;
}
PyObject *
SharedMemory_remove(SharedMemory *self) {
return shm_remove(self->id);
}
PyObject *
shm_get_key(SharedMemory *self) {
return KEY_T_TO_PY(self->key);
}
PyObject *
shm_get_size(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_SHM_SIZE);
}
PyObject *
shm_get_address(SharedMemory *self) {
return PyLong_FromVoidPtr(self->address);
}
PyObject *
shm_get_attached(SharedMemory *self) {
if (self->address)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
PyObject *
shm_get_last_attach_time(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_SHM_LAST_ATTACH_TIME);
}
PyObject *
shm_get_last_detach_time(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_SHM_LAST_DETACH_TIME);
}
PyObject *
shm_get_last_change_time(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_SHM_LAST_CHANGE_TIME);
}
PyObject *
shm_get_creator_pid(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_SHM_CREATOR_PID);
}
PyObject *
shm_get_last_pid(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_SHM_LAST_AT_DT_PID);
}
PyObject *
shm_get_number_attached(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_SHM_NUMBER_ATTACHED);
}
PyObject *
shm_get_uid(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_IPC_PERM_UID);
}
PyObject *
shm_get_cuid(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_IPC_PERM_CUID);
}
PyObject *
shm_get_cgid(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_IPC_PERM_CGID);
}
PyObject *
shm_get_mode(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_IPC_PERM_MODE);
}
int
shm_set_uid(SharedMemory *self, PyObject *py_value) {
union ipc_perm_value new_value;
if (!PyLong_Check(py_value))
{
PyErr_SetString(PyExc_TypeError, "Attribute 'uid' must be an integer");
goto error_return;
}
new_value.uid = PyLong_AsLong(py_value);
if (((uid_t)-1 == new_value.uid) && PyErr_Occurred()) {
// no idea what could have gone wrong -- punt it up to the caller
goto error_return;
}
return shm_set_ipc_perm_value(self->id, SVIFP_IPC_PERM_UID, new_value);
error_return:
return -1;
}
PyObject *
shm_get_gid(SharedMemory *self) {
return shm_get_value(self->id, SVIFP_IPC_PERM_GID);
}
int
shm_set_gid(SharedMemory *self, PyObject *py_value) {
union ipc_perm_value new_value;
if (!PyLong_Check(py_value))
{
PyErr_Format(PyExc_TypeError, "attribute 'gid' must be an integer");
goto error_return;
}
new_value.gid = PyLong_AsLong(py_value);
if (((gid_t)-1 == new_value.gid) && PyErr_Occurred()) {
// no idea what could have gone wrong -- punt it up to the caller
goto error_return;
}
return shm_set_ipc_perm_value(self->id, SVIFP_IPC_PERM_GID, new_value);
error_return:
return -1;
}
int
shm_set_mode(SharedMemory *self, PyObject *py_value) {
union ipc_perm_value new_value;
if (!PyLong_Check(py_value))
{
PyErr_Format(PyExc_TypeError, "attribute 'mode' must be an integer");
goto error_return;
}
new_value.mode = PyLong_AsLong(py_value);
if (((mode_t)-1 == new_value.mode) && PyErr_Occurred()) {
// no idea what could have gone wrong -- punt it up to the caller
goto error_return;
}
return shm_set_ipc_perm_value(self->id, SVIFP_IPC_PERM_MODE, new_value);
error_return:
return -1;
}
|