From: James Mckenzie Date: Thu, 3 Dec 2009 12:16:34 +0000 (+0000) Subject: Fix locking with a smiley :) X-Git-Url: http://xenbits.xen.org/gitweb?a=commitdiff_plain;h=f1fba3aa62f3cacac9bdd976dee5fc2191f2aeed;p=xenclient%2Fioemu-pq.git Fix locking with a smiley :) --- diff --git a/master/do-atapi-locking-properly b/master/do-atapi-locking-properly index 31e3170..249378c 100644 --- a/master/do-atapi-locking-properly +++ b/master/do-atapi-locking-properly @@ -1,5 +1,5 @@ diff --git a/hw/atapi-pt.c b/hw/atapi-pt.c -index 09cf6bb..aed333d 100644 +index 09cf6bb..53519cd 100644 --- a/hw/atapi-pt.c +++ b/hw/atapi-pt.c @@ -29,7 +29,6 @@ static int debug_enabled=-1; @@ -10,7 +10,7 @@ index 09cf6bb..aed333d 100644 #define IDE_ATAPI_PT_DEBUG_FILE_TEMPLATE "/var/log/cdrom-%d.log" -@@ -386,6 +385,90 @@ static const struct { +@@ -386,6 +385,96 @@ static const struct { { 0x000000, 0x000000, "Unrecognised sense data"} }; @@ -22,24 +22,28 @@ index 09cf6bb..aed333d 100644 +#define ATAPI_PT_LOCK_STATE_LOCKED_BY_ME 1 +#define ATAPI_PT_LOCK_STATE_LOCKED_BY_OTHER 2 + -+static int lock_fd = -1, we_have_lock = 0; ++static int we_have_lock = 0; + -+static int open_lock_file(void) ++static int get_lock_fd(void) +{ -+ lock_fd = open( IDE_ATAPI_PT_EXCLUSIVE_CD_FILE, O_RDWR | O_CREAT, 0666); -+ return (lock_fd<0) ? 1:0; ++ static int lock_fd=-1; ++ if (lock_fd <0) { ++ lock_fd = open( IDE_ATAPI_PT_EXCLUSIVE_CD_FILE, O_RDWR | O_CREAT, 0666); ++ } ++ return lock_fd; +} + + +static int get_atapi_pt_lock_state(void) +{ + struct flock lock = {0}; ++ int lock_fd=get_lock_fd(); ++ ++ if (lock_fd<0) return ATAPI_PT_LOCK_STATE_UNLOCKED; + + if (we_have_lock) + return ATAPI_PT_LOCK_STATE_LOCKED_BY_ME; + -+ if (open_lock_file()) return ATAPI_PT_LOCK_STATE_UNLOCKED; -+ + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_SET; + lock.l_start = 0; @@ -56,8 +60,9 @@ index 09cf6bb..aed333d 100644 +static int get_atapi_pt_lock(void) +{ + struct flock lock = {0}; -+ -+ if (open_lock_file()) return -1; ++ int lock_fd=get_lock_fd(); ++ ++ if (lock_fd<0) return -1; + + if (we_have_lock) + return 0; @@ -78,8 +83,9 @@ index 09cf6bb..aed333d 100644 +static int release_atapi_pt_lock(void) +{ + struct flock lock = {0}; -+ -+ if (open_lock_file()) return -1; ++ int lock_fd=get_lock_fd(); ++ ++ if (lock_fd<0) return -1; + + if (!we_have_lock) + return 0; @@ -101,7 +107,7 @@ index 09cf6bb..aed333d 100644 static const char *atapi_sense_to_str(int key, int asc, int ascq) { int i; -@@ -1007,8 +1090,7 @@ static int ide_atapi_pt_read_cd_block_size(const uint8_t *io_buffer) +@@ -1007,8 +1096,7 @@ static int ide_atapi_pt_read_cd_block_size(const uint8_t *io_buffer) static void ide_atapi_pt_ejected(void) { @@ -111,7 +117,7 @@ index 09cf6bb..aed333d 100644 } -@@ -1016,7 +1098,6 @@ static void ide_atapi_pt_cmd(IDEState *s) +@@ -1016,7 +1104,6 @@ static void ide_atapi_pt_cmd(IDEState *s) { uint8_t cmd_code; FILE *fp; @@ -119,7 +125,7 @@ index 09cf6bb..aed333d 100644 if(pthread_mutex_trylock(&s->atapi_pt.sgio_mutex)) { DEBUG_PRINTF("ide_atapi_pt_cmd() called with existing request processing - ignored!\n"); -@@ -1069,14 +1150,9 @@ static void ide_atapi_pt_cmd(IDEState *s) +@@ -1069,14 +1156,9 @@ static void ide_atapi_pt_cmd(IDEState *s) s->atapi_pt.din_xfer_len = ide_atapi_pt_get_data_size(ide_atapi_pt_size_buffer, cmd_code, s->atapi_pt.request); @@ -136,7 +142,7 @@ index 09cf6bb..aed333d 100644 { uint8_t sense[18] = {0x70, 0, 2, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0x3a, 1, 0, 0, 0, 0}; -@@ -1118,17 +1194,17 @@ static void ide_atapi_pt_cmd(IDEState *s) +@@ -1118,17 +1200,17 @@ static void ide_atapi_pt_cmd(IDEState *s) cmd_code == GPCMD_WRITE_10 || cmd_code == GPCMD_WRITE_12 || cmd_code == GPCMD_WRITE_AND_VERIFY_10 || cmd_code == GPCMD_WRITE_BUFFER) {