diff --git a/Changes b/Changes index 9c3329e..523464d 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension Data::UUID. +AS OF YET UNRELEASED + - exceptions are thrown when flock/lockf/fread fails + 1.220 2014-12-15 - improve chances it'll work on Android (thanks, Brian Fraser) diff --git a/UUID.h b/UUID.h index dc5ea28..60bd697 100644 --- a/UUID.h +++ b/UUID.h @@ -100,14 +100,14 @@ typedef unsigned64_t perl_uuid_time_t; /* Android's lic provides neither lockf nor any of the related constants */ #if (defined __solaris__ || defined __linux__) && !defined(__android__) -# define LOCK(f) lockf(fileno(f),F_LOCK,0); -# define UNLOCK(f) lockf(fileno(f),F_ULOCK,0); +# define LOCK(f) lockf(fileno(f),F_LOCK,0) +# define UNLOCK(f) lockf(fileno(f),F_ULOCK,0) #elif defined __darwin__ -# define LOCK(f) flock(fileno(f),LOCK_EX); -# define UNLOCK(f) flock(fileno(f),LOCK_UN); +# define LOCK(f) flock(fileno(f),LOCK_EX) +# define UNLOCK(f) flock(fileno(f),LOCK_UN) #else -# define LOCK(f) -# define UNLOCK(f) +# define LOCK(f) 0 +# define UNLOCK(f) 0 #endif #undef perl_uuid_t diff --git a/UUID.xs b/UUID.xs index 6667919..5d5cee4 100644 --- a/UUID.xs +++ b/UUID.xs @@ -357,16 +357,19 @@ PREINIT: CODE: RETVAL = (uuid_context_t *)PerlMemShared_malloc(sizeof(uuid_context_t)); if ((fd = fopen(UUID_STATE_NV_STORE, "rb"))) { - fread(&(RETVAL->state), sizeof(uuid_state_t), 1, fd); + if (! fread(&(RETVAL->state), sizeof(uuid_state_t), 1, fd)) { + croak("fread failed"); + } fclose(fd); get_current_time(×tamp); RETVAL->next_save = timestamp; } if ((fd = fopen(UUID_NODEID_NV_STORE, "rb"))) { pid_t *hate = (pid_t *) &(RETVAL->nodeid); - fread(&(RETVAL->nodeid), sizeof(uuid_node_t), 1, fd ); + if (! fread(&(RETVAL->nodeid), sizeof(uuid_node_t), 1, fd )) { + croak("fread failed"); + } fclose(fd); - *hate += getpid(); } else { get_random_info(seed); @@ -418,9 +421,13 @@ PPCODE: if (timestamp > self->next_save ) { mask = umask(_DEFAULT_UMASK); if((fd = fopen(UUID_STATE_NV_STORE, "wb"))) { - LOCK(fd); + if (-1 == LOCK(fd)) { + croak("Could not lock"); + } fwrite(&(self->state), sizeof(uuid_state_t), 1, fd); - UNLOCK(fd); + if (-1 == UNLOCK(fd)) { + croak("Could not unlock"); + } fclose(fd); } umask(mask); @@ -585,9 +592,15 @@ CODE: if (count == 0) { #endif if ((fd = fopen(UUID_STATE_NV_STORE, "wb"))) { - LOCK(fd); + if (-1 == LOCK(fd)) { + PerlMemShared_free(self); + croak("Could not lock"); + } fwrite(&(self->state), sizeof(uuid_state_t), 1, fd); - UNLOCK(fd); + if (-1 == UNLOCK(fd)) { + PerlMemShared_free(self); + croak("Could not unlock"); + } fclose(fd); }; PerlMemShared_free(self);