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; }
static bool surface_attach(struct ctx *context, struct wlc_context *bound, struct wlc_surface *surface, struct wlc_buffer *buffer) { assert(context && bound && surface); struct wl_resource *wl_buffer; if (!buffer || !(wl_buffer = convert_to_wl_resource(buffer, "buffer"))) { surface_destroy(context, bound, surface); return true; } EGLint format; bool attached = false; struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(wl_buffer); if (shm_buffer) { attached = shm_attach(surface, buffer, shm_buffer); } else if (wlc_context_query_buffer(bound, (void*)wl_buffer, EGL_TEXTURE_FORMAT, &format)) { attached = egl_attach(context, bound, surface, buffer, format); } else { /* unknown buffer */ wlc_log(WLC_LOG_WARN, "Unknown buffer"); } if (attached) wlc_dlog(WLC_DBG_RENDER, "-> Attached surface (%" PRIuWLC ") with buffer of size (%ux%u)", convert_to_wlc_resource(surface), buffer->size.w, buffer->size.h); return attached; }
int create_shm_index() { void *pindex = NULL; unsigned int i = 0; struct shm_attr attr = {0}; attr.entry_size = sizeof(rc_index); attr.n_entry = MAX_INDEX; p_indexs = (rc_index *)calloc(MAX_INDEX, sizeof(rc_index)); if (p_indexs == NULL) { return -1; } index_handle = shm_attach((char *)SHM_DIRCOUNTER_INDEX); if (index_handle == SHM_HANDLE_FAIL) { index_handle = shm_create((char *)SHM_DIRCOUNTER_INDEX, &attr); if (index_handle == NULL) { free(p_indexs); return -1; } } shm_wlock(index_handle); for_each_shm_obj(index_handle, pindex, i) { memset(pindex, 0, sizeof(rc_index)); }
/*** Attach to a shared memory area ***/ void _0x94_shm_attach(void) { uint8_t key = (uint8_t)current_process->cpu.ebx; uint32_t mode = (uint32_t)current_process->cpu.ecx; current_process->cpu.edx = (uint32_t) shm_attach(key, mode, current_process); // return value current_process->state = READY; }
void Queue::queue_write_init(){ int shmid = shm_create(); _shmid = shmid; _head = (Head *)(shm_attach(shmid)); _head->front = 0; _head->rear = 0; _arr = (Text *)(_head+1); // cout<<"++"<<endl; }
/** Initializes this process. @param first Whether shared resources should also be initialized. @return 0 if successful and -1 otherwise. **/ int init(const bool first) { /* Initializes the configuration. */ if (cfg_init_lib() == -1) { return -1; } /* Initializes the functions. */ if (lib_init() == -1) { return -1; } /* Initializes the save-quit-load emulation. */ if (cfg_emulate_sql) { if (asm_inject(&save_quit_load) == -1) { return -1; } } else { if (asm_inject(NULL) == -1) { return -1; } } /* Initializes the shared memory segment. */ if (first) { if (shm_init() == -1) { return -1; } } if (shm_attach() == -1) { return -1; } /* Sets variables that should be automatic. */ record.timestamp = cfg_timestamp; options.play_on = cfg_play_instantly; options.play_paused = FALSE; options.gui_menu = FALSE; options.gui_info = FALSE; options.gui_overlay = FALSE; options.gui_condensed = FALSE; options.gui_hidden = FALSE; options.roll_on = FALSE; options.roll_cataloged = FALSE; return 0; }
/** Forks things. @return 0 if successful and -1 otherwise. **/ int init_fork(void) { /* Creates a process monitor to avoid accidental daemonization. */ const pid_t child_pid = fork(); if (child_pid == -1) { probno = log_error(FORK_PROBLEM); return -1; } else { pid = getpid(); if (child_pid == 0) {//child shm_attach(); /* Synchronizes with the parent process. */ while (shared.pids[0] != pid); return NO_PROBLEM; } else {//parent *shared.ppid = pid; shared.pids[0] = child_pid; /* Prevents defunct processes from appearing. */ signal(SIGCHLD, SIG_IGN); while (*shared.state != HAD_ENOUGH) { napms(NAP_RESOLUTION / frame_rate); } do { bool done_quitting = TRUE; for (int save = 0; save < cfg_saves; save++) { if (shared.pids[save] != 0) { done_quitting = FALSE; } } if (done_quitting) break; napms(NAP_RESOLUTION / frame_rate); } while (TRUE); uninit(TRUE); exit(NO_PROBLEM); } } return 0; }
void Queue::queue_read_init(){ int shmid = shm_get(); _shmid = shmid; _head = (Head *)(shm_attach(shmid)); _arr = (Text *)(_head+1); }
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 { #if PY_MAJOR_VERSION > 2 size = PyLong_AsUnsignedLongMask(py_size); #else size = PyInt_AsUnsignedLongMask(py_size); #endif 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; }
static void *shm_init(int size) { if (shm_creat(size) == -1) return NULL; return shm_attach(); }
/** Saves the game to memory. @param save The desired save state. @return 0 if successful and -1 otherwise. **/ int save_state(const int save) { do { const pid_t child_pid = fork(); if (child_pid == -1) { probno = log_error(FORK_PROBLEM); return -1; } else { pid = getpid(); if (child_pid == 0) {//child shm_attach(); while (shared.pids[0] != pid); return NO_PROBLEM; } else {//parent shared.pids[0] = child_pid; shared.pids[save] = pid; signal(SIGCHLD, SIG_IGN); /* Saves the temporary files. */ copy_temporary(save, TRUE); while (TRUE) { if (shared.pids[0] == pid) { break;//someone activated this slot } if (shared.pids[save] != pid) { uninit(FALSE);//someone took this slot exit(NO_PROBLEM); } if (*shared.state == HAD_ENOUGH) { options.progress = EXIT; shared.pids[save] = 0;//(pid_t ) uninit(FALSE);//everyone is shutting down exit(NO_PROBLEM); } napms(NAP_RESOLUTION / frame_rate); } /* Stores the position of the cursor. */ short int y, x; getyx(stdscr, y, x); /* Saves the screen. */ for (int row = 0; row < cfg_rows; row++) { for (int col = 0; col < cfg_cols; col++) { shared.chs[save][row][col] = mvwinch(stdscr, row, col); } } /* Restores the position of the cursor. */ wmove(stdscr, y, x); /* Redraws the window. */ redrawwin(stdscr); wrefresh(stdscr); } } } while (cfg_keep_saves); return 0; }
int pa_shm_cleanup(void) { #ifdef HAVE_SHM_OPEN #ifdef SHM_PATH DIR *d; struct dirent *de; if (!(d = opendir(SHM_PATH))) { pa_log_warn("Failed to read "SHM_PATH": %s", pa_cstrerror(errno)); return -1; } while ((de = readdir(d))) { pa_shm seg; unsigned id; pid_t pid; char fn[128]; struct shm_marker *m; #if defined(__sun) if (strncmp(de->d_name, ".SHMDpulse-shm-", SHM_ID_LEN)) #else if (strncmp(de->d_name, "pulse-shm-", SHM_ID_LEN)) #endif continue; if (pa_atou(de->d_name + SHM_ID_LEN, &id) < 0) continue; if (shm_attach(&seg, PA_MEM_TYPE_SHARED_POSIX, id, -1, false, true) < 0) continue; if (seg.size < shm_marker_size(seg.type)) { pa_shm_free(&seg); continue; } m = (struct shm_marker*) ((uint8_t*) seg.ptr + seg.size - shm_marker_size(seg.type)); if (pa_atomic_load(&m->marker) != SHM_MARKER) { pa_shm_free(&seg); continue; } if (!(pid = (pid_t) pa_atomic_load(&m->pid))) { pa_shm_free(&seg); continue; } if (kill(pid, 0) == 0 || errno != ESRCH) { pa_shm_free(&seg); continue; } pa_shm_free(&seg); /* Ok, the owner of this shms segment is dead, so, let's remove the segment */ segment_name(fn, sizeof(fn), id); if (shm_unlink(fn) < 0 && errno != EACCES && errno != ENOENT) pa_log_warn("Failed to remove SHM segment %s: %s\n", fn, pa_cstrerror(errno)); } closedir(d); #endif /* SHM_PATH */ #endif /* HAVE_SHM_OPEN */ return 0; }
/* Caller owns passed @memfd_fd and must close it down when appropriate. */ int pa_shm_attach(pa_shm *m, pa_mem_type_t type, unsigned id, int memfd_fd, bool writable) { return shm_attach(m, type, id, memfd_fd, writable, false); }
/** Carries out simulation setup and management. @param argc The number of arguments @param argv The array of arguments */ int main(int argc, char *argv[]) { int *results, *shm_states; int i, op_count, proc_id; char *tmp_operator, *cmd; list *commands; operation *shm_operations; if(signal(SIGTERM, &stop_execution) == SIG_ERR) { write_to_fd(2, "Failed to register signal\n"); exit(1); } if(argc != 3) { write_to_fd(2, "Usage: main.x <source file> <results file>\n"); exit(1); } commands = parse_file(argv[1]); processors = atoi(list_extract(commands)); if (processors <= 0) { write_to_fd(2, "Invalid number of processors\n"); exit(1); } write_with_int(1, "Number of processors: ", processors); op_count = list_count(commands); if (op_count == 0) { write_to_fd(2, "No operations provided\n"); exit(1); } write_with_int(1, "Number of operations: ", op_count); results = (int *) malloc(op_count * sizeof(int)); if (results == NULL) { write_to_fd(2, "Failed to allocate results array\n"); exit(1); } init_ipc(2 * processors + 2, processors * sizeof(operation), processors * sizeof(int), 0666 | IPC_CREAT | IPC_EXCL); write_with_int(1, "Created semaphore set with ID ", ipc_id[0]); write_with_int(1, "Created shm for operations with ID ", ipc_id[1]); write_with_int(1, "Created shm for states with ID ", ipc_id[2]); init_sems(processors); shm_operations = (operation *) shm_attach(ipc_id[1]); shm_states = (int *) shm_attach(ipc_id[2]); for (i = 0; i < processors; ++i) shm_states[i] = 0; start_processors(); for (i = 1; list_count(commands) > 0; ++i) { cmd = list_extract(commands); write_with_int(1, "\nOperation #", i); proc_id = atoi(strtok(cmd, " ")); sem_p(2 * processors + 1); if (proc_id-- == 0) { proc_id = find_proc(shm_states); } write_with_int(1, "Waiting for processor ", proc_id + 1); sem_p(2 * proc_id); write_with_int(1, "Delivering operation to processor ", proc_id + 1); if (shm_states[proc_id] != 0) { results[(shm_states[proc_id] + 1) * -1] = shm_operations[proc_id].num1; write_with_int(1, "Previous result: ", shm_operations[proc_id].num1); } shm_operations[proc_id].num1 = atoi(strtok(NULL, " ")); tmp_operator = strtok(NULL, " "); shm_operations[proc_id].op = *tmp_operator; shm_operations[proc_id].num2 = atoi(strtok(NULL, " ")); shm_states[proc_id] = i; write_with_int(1, "Operation delivered. Unblocking processor ", proc_id + 1); sem_v((2 * proc_id) + 1); free(cmd); } list_destruct(commands); for (i = 0; i < processors; ++i) { sem_p(2 * i); write_with_int(1, "\nPassing termination command to processor #", i + 1); if (shm_states[i] != 0) { results[(shm_states[i] + 1) * -1] = shm_operations[i].num1; write_with_int(1, "Last result: ", shm_operations[i].num1); } shm_operations[i].op = 'K'; sem_v((2 * i) + 1); } for (i = 0; i < processors; ++i) if(wait(NULL) == -1) write_to_fd(2, "Wait failed\n"); write_to_fd(1, "\nAll processors exited. Writing output file\n"); write_results(argv[2], results, op_count); free(results); write_to_fd(1, "Closing IPCs\n"); shm_detach((void *) shm_operations); shm_detach((void *) shm_states); close_ipc(); exit(0); }