/*for named semaphore: creates a new POSIX semaphore or opens an existing semaphore.*/ EC_BOOL csem_named_open(CSEM *csem, const uint8_t *name, const int oflag, const mode_t mode, const uint32_t value, const UINT32 location) { sem_t *sem; if(NULL_PTR != CSEM_NAME_CSTR(csem)) { dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_named_open: csem %p has already set name %s at %s:%ld\n", csem, cstring_get_str(CSEM_NAME_CSTR(csem)), MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_FALSE); } CSEM_NAME_CSTR(csem) = cstring_new(name, location); if(NULL_PTR == CSEM_NAME_CSTR(csem)) { dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_named_open: new cstring for csem %p failed where name %s\n", csem, name); return (EC_FALSE); } sem = sem_open((const char *)name, oflag, mode, value); if(SEM_FAILED == sem) { dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_named_open: open sem of %s oflag %d mode_t %o value %u failed where errno = %d, errstr = %s at %s:%ld\n", name, oflag, mode, value, errno, strerror(errno), MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_FALSE); } CSEM_SEM(csem) = sem; return (EC_TRUE);
EC_BOOL csem_named_clean(CSEM *csem, const UINT32 location) { uint8_t *sem_name; /*close*/ if(EC_FALSE == csem_named_close(csem, location)) { dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_named_clean: close csem %p failed where errno = %d, errstr = %s at %s:%ld\n", csem, errno, strerror(errno), MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_FALSE); } if(NULL_PTR == CSEM_NAME_CSTR(csem)) { return (EC_TRUE); } /*unlink*/ sem_name = cstring_get_str(CSEM_NAME_CSTR(csem)); if(EC_FALSE == csem_named_unlink(sem_name, location)) { dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_named_clean: unlink sem %s failed where errno = %d, errstr = %s at %s:%ld\n", sem_name, errno, strerror(errno), MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_FALSE); } cstring_free(CSEM_NAME_CSTR(csem)); CSEM_NAME_CSTR(csem) = NULL_PTR; return (EC_TRUE);
size_t rawFileRead(RawFile *raw_file, const offset_t offset, void *des, size_t size, size_t nmemb, const word_t location) { size_t count; count = rawDataRead(raw_file->raw_data, offset, des, size, nmemb); dbg_log(SEC_0132_RAW, 9)(LOGSTDNULL, "[DEBUG] rawFileRead: raw_file %lx: read %d bytes at offset %d, %s:%ld\n", raw_file, count * size, offset, MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return count; }
/*destroys the unnamed semaphore*/ EC_BOOL csem_unamed_clean(CSEM *csem, const UINT32 location) { if(0 == sem_destroy(CSEM_SEM(csem))) { return (EC_TRUE); } dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_unamed_clean: clean csem %p failed where errno = %d, errstr = %s at %s:%ld\n", csem, errno, strerror(errno), MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_FALSE); }
/*initialises the unnamed semaphore*/ EC_BOOL csem_unamed_init(CSEM *csem, const int pshared, const uint32_t value, const UINT32 location) { if(0 == sem_init(CSEM_SEM(csem), pshared, value)) { return (EC_TRUE); } dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_unamed_init: csem %p pshared %d value %u failed where errno = %d, errstr = %s at %s:%ld\n", csem, pshared, value, errno, strerror(errno), MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_FALSE); }
/*removes the named semaphore*/ EC_BOOL csem_named_unlink(const uint8_t *name, const UINT32 location) { if(NULL_PTR == name) { dbg_log(SEC_0029_CSEM, 1)(LOGSTDOUT, "warn:csem_named_unlink: unlink sem name is null at %s:%ld\n", name, MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_TRUE); } if(0 == sem_unlink((char *)name)) { return (EC_TRUE); } dbg_log(SEC_0029_CSEM, 0)(LOGSTDOUT, "error:csem_named_unlink: unlink sem %s failed where errno = %d, errstr = %s at %s:%ld\n", name, errno, strerror(errno), MM_LOC_FILE_NAME(location), MM_LOC_LINE_NO(location)); return (EC_FALSE);