int main(int ac, char **av) { int fail = 0; char *msg; /* message returned from parse_opts */ /* parse standard options */ if ((msg = parse_opts(ac, av, (option_t *) NULL, NULL)) != (char *)NULL) { tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); } setup(); /* global setup */ /* //block1: *//* Error: when no lock is set */ tst_resm(TINFO, "Enter block 1"); fail = 0; #ifdef LINUX_FILE_REGION_LOCK if (fcntl(fd, F_RGETLK, &tl) < 0) { if (errno == EINVAL) { tst_resm(TINFO, "fcntl remote locking feature not " "implemented in the kernel: exitting"); cleanup(); /*NOTREACHED*/} else { tst_resm(TPASS, "fcntl on file failed: Test " "PASSED"); } } /* * Add a write lock to the middle of the file and unlock a section * just before the lock */ if (do_lock(F_RSETLK, (short)F_WRLCK, (short)0, 10, 5) < 0) { tst_resm(TFAIL, "F_RSETLK WRLCK failed"); fail = 1; } if (do_lock(F_RSETLK, (short)F_UNLCK, (short)0, 5, 5) < 0) { tst_resm(TFAIL, "F_RSETLK UNLOCK failed"); fail = 1; } unlock_file(); #endif if (fail) { tst_resm(TFAIL, "Block 1 FAILED"); } else { tst_resm(TPASS, "Block 1 PASSED"); } close(fd); tst_resm(TINFO, "Exit block 1"); cleanup(); return 0; }
void test() { pid_t pid; if((pid = fork()) < 0) { Perror("fork error"); } else if(pid == 0) { do_lock("lock.dat"); } else { sleep(2); do_lock("lock.dat"); } }
struct ZSTLIterator *ZSTLMapEnum(struct ZSTLMap *pm) { ZSTLIterator_t *iterator; pthread_mutex_lock(&(pm->enum_mutex)); do_lock(&(pm->mutex)); iterator = get_iterator(pm); zstlmap_assert(iterator); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapEnum: pm=%p, iterator=%p, ", pm, iterator); #endif iterator->enum_entry = pm->lru_head; #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "pme=%p, enum_entry=%p\n", pm, iterator->enum_entry); #endif do_unlock(&(pm->mutex)); return(iterator); }
// Decrement the reference count for this entry // rc=1 if entry is found, rc=0 otherwise int ZSTLMapRelease(struct ZSTLMap *pm, char *key, uint32_t keylen) { ZSTLMapEntry_t *pme; int rc = 0; do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapRelease: pm=%p, key=0x%lx, keylen=%d", pm, *((uint64_t *) key), keylen); #endif pme = find_pme(pm, key, keylen, NULL); if (pme != NULL) { // fprintf(stderr, ", after release [0x%lx] =%d\n", *((uint64_t *) key), pme->refcnt - 1); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, ", after release refcnt=%d\n", pme->refcnt - 1); #endif (pme->refcnt)--; rc = 1; } else { #ifdef SANDISK_PRINTSTUFF fprintf(stderr, " (KEY NOT FOUND!)\n"); #endif } do_unlock(&(pm->mutex)); return(rc); }
int lock_main(int argc, char **argv) { char **args = &argv[1]; int c = argc - 1; while ((*args != NULL) && (*args)[0] == '-') { char *ch = *args; while (*(++ch) > 0) { switch(*ch) { case 'w': waitonly = 1; break; case 's': shared = 1; break; case 'u': unlock = 1; break; } } c--; args++; } if (c != 1) usage(argv[0]); file = *args; if (unlock) return do_unlock(); else return do_lock(); }
// Return non-NULL if success, NULL if object does not exist struct ZSTLMapEntry *ZSTLMapUpdate(struct ZSTLMap *pm, char *pkey, uint32_t keylen, char *pdata, uint64_t datalen) { ZSTLMapEntry_t *pme; do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapUpdate: pm=%p, key=0x%lx, keylen=%d, pdata=%p, datalen=%ld\n", pm, *((uint64_t *) pkey), keylen, pdata, datalen); #endif pme = find_pme(pm, pkey, keylen, NULL); if (pme == NULL) { do_unlock(&(pm->mutex)); return(NULL); } /* Update an existing entry. */ // update the LRU list if necessary if (pm->max_entries != 0) { update_lru(pm, pme); } if (pm->replacement_callback) { // return NULL for key so that it is not freed! (pm->replacement_callback)(pm->replacement_callback_data, pme->key, pme->keylen, pme->contents, pme->datalen); } pme->contents = pdata; pme->datalen = datalen; do_unlock(&(pm->mutex)); return(pme); }
void queue_push( int data, queue_t *queue ) { int done = 0; do_lock( &queue->operation_lock ); while ( queue_is_full( queue ) ) { do_unlock( &queue->operation_lock ); __asm__("hlt"); do_lock( &queue->operation_lock ); } queue->data[queue->head] = data; queue->head = QUEUE_WRAP( queue->head + 1 ); done = 1; do_unlock( &queue->operation_lock ); }
NTSTATUS do_lock_spin(files_struct *fsp,connection_struct *conn, uint16 lock_pid, SMB_BIG_UINT count,SMB_BIG_UINT offset,enum brl_type lock_type) { int j, maxj = lp_lock_spin_count(); int sleeptime = lp_lock_sleep_time(); NTSTATUS status, ret; if (maxj <= 0) maxj = 1; ret = NT_STATUS_OK; /* to keep dumb compilers happy */ for (j = 0; j < maxj; j++) { status = do_lock(fsp, conn, lock_pid, count, offset, lock_type); if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) && !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) { return status; } /* if we do fail then return the first error code we got */ if (j == 0) { ret = status; } if (sleeptime) sys_usleep(sleeptime); } return ret; }
void flockfile(FILE * fp) { int idx = file_idx(fp); struct file_lock *p; /* Lock the hash table: */ _SPINLOCK(&hash_lock); /* Check if the static array has not been initialised: */ if (!init_done) { /* Initialise the global array: */ memset(flh,0,sizeof(flh)); /* Flag the initialisation as complete: */ init_done = 1; } /* Get a pointer to any existing lock for the file: */ if ((p = find_lock(idx, fp)) == NULL) { /* * The file is not locked, so this thread can * grab the lock: */ p = do_lock(idx, fp); /* Unlock the hash table: */ _SPINUNLOCK(&hash_lock); /* * The file is already locked, so check if the * running thread is the owner: */ } else if (p->owner == _thread_run) { /* * The running thread is already the * owner, so increment the count of * the number of times it has locked * the file: */ p->count++; /* Unlock the hash table: */ _SPINUNLOCK(&hash_lock); } else { /* * The file is locked for another thread. * Append this thread to the queue of * threads waiting on the lock. */ TAILQ_INSERT_TAIL(&p->l_head,_thread_run,qe); /* Unlock the hash table: */ _SPINUNLOCK(&hash_lock); /* Wait on the FILE lock: */ _thread_kern_sched_state(PS_FILE_WAIT, "", 0); } }
void char_walk( CHAR_DATA *ch ) { int dir = -1; bool close = FALSE; bool lock = FALSE; ROOM_INDEX_DATA *prev_room; if ( !ch || ch->walking == 0 ) return; if ( ch->in_room->vnum == ch->walking || ch->in_room->vnum == -1 * ch->walking ) { ch->walking = 0; return; } if ( ch->walking > 0 ) dir = find_path( ch->in_room->vnum, ch->walking, ch, 40000, TRUE ); else if ( ch->walking < 0 ) dir = find_path( ch->in_room->vnum, -1 * ch->walking, ch, 40000, FALSE ); if ( dir < 0 || dir >= MAX_DIR ) { ch->walking = 0; return; } if ( IS_SET( ch->in_room->exit[dir]->exit_info, EX_LOCKED ) ) { do_unlock( ch, (char *) eng_dir_name[dir] ); lock = TRUE; } if ( IS_SET( ch->in_room->exit[dir]->exit_info, EX_CLOSED ) ) { do_open( ch, (char *) eng_dir_name[dir] ); close = TRUE; } if ( IS_SET( sector_table[ch->in_room->exit[dir]->u1.to_room->sector_type].flag, SECT_UNDERWATER ) ) return; prev_room = ch->in_room; move_char( ch, dir, FALSE, NULL ); if ( ch->in_room->vnum == ch->walking || ch->in_room->vnum == -1 * ch->walking ) ch->walking = 0; if ( ch->in_room == prev_room ) return; if ( close ) do_close( ch, (char *) eng_dir_name[rev_dir[dir]] ); if ( lock ) do_lock( ch, (char *) eng_dir_name[rev_dir[dir]] ); return; }
int main(int ac, char **av) { int fail = 0; const char *msg; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); /* global setup */ fail = 0; #ifdef LINUX_FILE_REGION_LOCK if (fcntl(fd, F_RGETLK, &tl) == -1) { if (errno == EINVAL) tst_brkm(TCONF, cleanup, "fcntl remote locking feature not implemented in " "the kernel"); else { /* * FIXME (garrcoop): having it always pass on * non-EINVAL is a bad test. */ tst_resm(TPASS, "fcntl on file failed"); } } /* * Add a write lock to the middle of the file and unlock a section * just before the lock */ if (do_lock(F_RSETLK, F_WRLCK, 0, 10, 5) == -1) tst_resm(TFAIL, "F_RSETLK WRLCK failed"); if (do_lock(F_RSETLK, F_UNLCK, 0, 5, 5) == -1) tst_resm(TFAIL | TERRNO, "F_RSETLK UNLOCK failed"); unlock_file(); #else tst_resm(TCONF, "system doesn't have LINUX_LOCK_FILE_REGION support"); #endif cleanup(); tst_exit(); }
dbref create_guest(char *name, char *password) { dbref player; char *buff; if(!Wizard(mudconf.guest_nuker) || !Good_obj(mudconf.guest_nuker)) mudconf.guest_nuker = 1; buff = alloc_lbuf("create_guest"); /* * Make the player. */ player = create_player(name, password, mudconf.guest_nuker, 0, 1); if(player == NOTHING) { log_text("GUEST: failed in create_player\n"); return NOTHING; } /* * Turn the player into a guest. */ s_Guest(player); move_object(player, mudconf.start_room); s_Flags(player, Flags(player) & ~WIZARD); s_Pennies(player, Pennies(mudconf.guest_char)); s_Zone(player, Zone(mudconf.guest_char)); s_Parent(player, Parent(mudconf.guest_char)); /* * Make sure the guest is locked. */ do_lock(player, player, A_LOCK, tprintf("#%d", player), "me"); do_lock(player, player, A_LENTER, tprintf("#%d", player), "me"); /* * Copy all attributes. */ atr_cpy(GOD, player, mudconf.guest_char); free_lbuf(buff); return player; }
// Returns 1 if successful, 0 otherwise // Caller is responsible for freeing key and data int ZSTLMapNextEnum(struct ZSTLMap *pm, struct ZSTLIterator *iterator, char **key, uint32_t *keylen, char **data, uint64_t *datalen) { ZSTLMapEntry_t *pme_return; do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapNextEnum: pm=%p, iterator=%p, ", pm, iterator); #endif if (iterator->enum_entry == NULL) { do_unlock(&(pm->mutex)); ZSTLFinishEnum(pm, iterator); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "pdata=NULL, datalen=0\n"); #endif *key = NULL; *keylen = 0; *data = NULL; *datalen = 0; return(0); } pme_return = iterator->enum_entry; iterator->enum_entry = pme_return->next_lru; if (pme_return != NULL) { *key = pme_return->key; *keylen = pme_return->keylen; *data = (char *) pme_return->contents; *datalen = pme_return->datalen; #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "key=%p, keylen=%d, pdata=%p, datalen=%ld, pme_return=%p\n", *key, *keylen, *data, *datalen, pme_return); #endif do_unlock(&(pm->mutex)); return(1); } else { ZSTLFinishEnum(pm, iterator); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "pdata=NULL, datalen=0\n"); #endif *key = NULL; *keylen = 0; *data = NULL; *datalen = 0; do_unlock(&(pm->mutex)); return(0); } }
int queue_pull( queue_t *queue ) { int ret = 0x12344321; int done = 0; do_lock( &queue->operation_lock ); while ( queue_is_empty( queue ) ) { do_unlock( &queue->operation_lock ); __asm__("hlt"); do_lock( &queue->operation_lock ); } ret = queue->data[queue->tail]; queue->tail = QUEUE_WRAP( queue->tail + 1 ); done = 1; do_unlock( &queue->operation_lock ); return ret; }
// Returns non-NULL if successful, NULL otherwise struct ZSTLMapEntry *ZSTLMapGet(struct ZSTLMap *pm, char *key, uint32_t keylen, char **pdata, uint64_t *pdatalen) { uint64_t datalen; ZSTLMapEntry_t *pme; char *data; do_lock(&(pm->mutex)); pme = find_pme(pm, key, keylen, NULL); if (pme != NULL) { data = pme->contents; datalen = pme->datalen; // update the LRU list if necessary if (pm->max_entries != 0) { update_lru(pm, pme); } } else { data = NULL; datalen = 0; } #ifdef SANDISK_PRINTSTUFF if (pme != NULL) { fprintf(stderr, "ZSTLMapGet: pm=%p, key=0x%lx, keylen=%d, pdata=%p, datalen=%ld, refcnt=%d, pme=%p\n", pm, *((uint64_t *) key), keylen, data, datalen, pme->refcnt, pme); } else { fprintf(stderr, "ZSTLMapGet: pm=%p, key=0x%lx, keylen=%d, pdata=%p, datalen=%ld, pme=%p\n", pm, *((uint64_t *) key), keylen, data, datalen, pme); } #ifdef notdef void* tracePtrs[100]; int count = backtrace( tracePtrs, 100 ); char** funcNames = backtrace_symbols( tracePtrs, count ); // Print the stack trace printf("---------------------------------------------------------------------------\n"); for( int ii = 0; ii < count; ii++ ) { printf( "%s\n", funcNames[ii] ); } printf("---------------------------------------------------------------------------\n"); // Free the string pointers free( funcNames ); #endif #endif do_unlock(&(pm->mutex)); *pdatalen = datalen; *pdata = data; return(pme); }
void ZSTLFinishEnum(struct ZSTLMap *pm, struct ZSTLIterator *iterator) { do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLFinishEnum: pm=%p, iterator=%p\n", pm, iterator); #endif free_iterator(pm, iterator); pthread_mutex_unlock(&(pm->enum_mutex)); do_unlock(&(pm->mutex)); }
// Return non-NULL if success, NULL if object exists struct ZSTLMapEntry *ZSTLMapCreate(struct ZSTLMap *pm, char *pkey, uint32_t keylen, char *pdata, uint64_t datalen) { ZSTLMapEntry_t *pme; ZSTLMapBucket_t *pb; do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapCreate: pm=%p, key=0x%lx, keylen=%d, pdata=%p, datalen=%ld, ", pm, *((uint64_t *) pkey), keylen, pdata, datalen); #endif pme = find_pme(pm, pkey, keylen, &pb); if (pme != NULL) { #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "pme=%p\n", pme); #endif do_unlock(&(pm->mutex)); return(NULL); } /* Create a new entry. */ pme = create_pme(pm, pkey, keylen, pdata, datalen); /* put myself on the bucket list */ pme->bucket = pb; pme->next = pb->entry; pb->entry = pme; #ifdef LISTCHECK check_list(pb, pme); #endif pm->n_entries++; /* put myself on the lru list */ { insert_lru(pm, pme); if ((pm->max_entries > 0) && (pm->n_entries > pm->max_entries)) { // do an LRU replacement replace_lru(pm, pme); } } do_unlock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "pme=%p\n", pme); #endif return(pme); }
int ploop_global_lock(void) { if (access(PLOOP_GLOBAL_LOCK_FILE, F_OK)) { if (access(PLOOP_LOCK_DIR, F_OK) && mkdir(PLOOP_LOCK_DIR, 0700) && errno != EEXIST) { ploop_err(errno, "Failed to create " PLOOP_LOCK_DIR); return -1; } if (create_file(PLOOP_GLOBAL_LOCK_FILE)) return -1; } return do_lock(PLOOP_GLOBAL_LOCK_FILE, 0); }
NTSTATUS do_lock_spin(files_struct *fsp, uint16 lock_pid, SMB_BIG_UINT count, SMB_BIG_UINT offset, enum brl_type lock_type, enum brl_flavour lock_flav, BOOL *my_lock_ctx) { int j, maxj = lp_lock_spin_count(); int sleeptime = lp_lock_sleep_time(); NTSTATUS status, ret; if (maxj <= 0) { maxj = 1; } ret = NT_STATUS_OK; /* to keep dumb compilers happy */ for (j = 0; j < maxj; j++) { status = do_lock(fsp, lock_pid, count, offset, lock_type, lock_flav, my_lock_ctx); if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED) && !NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) { return status; } /* if we do fail then return the first error code we got */ if (j == 0) { ret = status; /* Don't spin if we blocked ourselves. */ if (*my_lock_ctx) { return ret; } /* Only spin for Windows locks. */ if (lock_flav == POSIX_LOCK) { return ret; } } if (sleeptime) { sys_usleep(sleeptime); } } return ret; }
int (ftrylockfile)(FILE * fp) { int ret = -1; int idx = file_idx(fp); struct file_lock *p; /* Lock the hash table: */ _spinlock(&hash_lock); /* Get a pointer to any existing lock for the file: */ if ((p = find_lock(idx, fp)) == NULL) { /* * The file is not locked, so this thread can * grab the lock: */ p = do_lock(idx, fp); /* * The file is already locked, so check if the * running thread is the owner: */ } else if (p->owner == pthread_self()) { /* * The running thread is already the * owner, so increment the count of * the number of times it has locked * the file: */ p->count++; } else { /* * The file is locked for another thread, * so this try fails. */ p = NULL; } /* Unlock the hash table: */ _spinunlock(&hash_lock); /* Check if the lock was obtained: */ if (p != NULL) /* Return success: */ ret = 0; return (ret); }
int ploop_lock_di(struct ploop_disk_images_data *di) { char fname[PATH_MAX]; if (di == NULL) return 0; get_disk_descriptor_lock_fname(di, fname, sizeof(fname)); if (access(fname, F_OK)) { if (create_file(fname)) return -1; } di->runtime->lckfd = do_lock(fname, LOCK_TIMEOUT); if (di->runtime->lckfd == -1) return -1; return 0; }
void (flockfile)(FILE * fp) { int idx = file_idx(fp); struct file_lock *p; pthread_t self = pthread_self(); /* Lock the hash table: */ _spinlock(&hash_lock); /* Get a pointer to any existing lock for the file: */ if ((p = find_lock(idx, fp)) == NULL) { /* * The file is not locked, so this thread can * grab the lock: */ do_lock(idx, fp); /* * The file is already locked, so check if the * running thread is the owner: */ } else if (p->owner == self) { /* * The running thread is already the * owner, so increment the count of * the number of times it has locked * the file: */ p->count++; } else { /* * The file is locked for another thread. * Append this thread to the queue of * threads waiting on the lock. */ TAILQ_INSERT_TAIL(&p->lockers,self,waiting); while (p->owner != self) { __thrsleep(self, 0 | _USING_TICKETS, NULL, &hash_lock.ticket, NULL); _spinlock(&hash_lock); } } /* Unlock the hash table: */ _spinunlock(&hash_lock); }
// Decrement the reference count for this entry int ZSTLMapReleaseEntry(struct ZSTLMap *pm, struct ZSTLMapEntry *pme) { int rc = 0; do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapReleaseEntry: pm=%p, pme=%p\n", pm, pme); #endif if (pme != NULL) { (pme->refcnt)--; rc = 1; } do_unlock(&(pm->mutex)); return(rc); }
int create_fv_user(int argc, char * const *argv) { char *keychainName = NULL; int ch, result = 0; Boolean lockAll = FALSE; while ((ch = getopt(argc, argv, "ah")) != -1) { switch (ch) { case 'a': lockAll = TRUE; break; case '?': default: return 2; /* @@@ Return 2 triggers usage message. */ } } argc -= optind; argv += optind; if (argc == 1 && !lockAll) { keychainName = argv[0]; if (*keychainName == '\0') { result = 2; goto loser; } } else if (argc != 0) return 2; if (lockAll) result = do_lock_all(); else result = do_lock(keychainName); loser: return result; }
void ZSTLMapCheckRefcnts(struct ZSTLMap *pm) { uint64_t i; ZSTLMapEntry_t *pme; do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapCheckRefcnts: pm=%p\n", pm); #endif for (i=0; i<pm->nbuckets; i++) { for (pme = pm->buckets[i].entry; pme != NULL; pme = pme->next) { if (pme->refcnt != 0) { fprintf(stderr, "***************** ZSTLMapCheckRefcnts: [pm=%p]: key 0x%lx refcnt=%d!\n", pm, *((uint64_t *) pme->key), pme->refcnt); } } } do_unlock(&(pm->mutex)); }
static bool process_trans2(struct blocking_lock_record *blr) { char params[2]; NTSTATUS status; struct byte_range_lock *br_lck = do_lock( blr->fsp->conn->sconn->msg_ctx, blr->fsp, blr->smblctx, blr->count, blr->offset, blr->lock_type, blr->lock_flav, True, &status, &blr->blocking_smblctx, blr); TALLOC_FREE(br_lck); if (!NT_STATUS_IS_OK(status)) { if (ERROR_WAS_LOCK_DENIED(status)) { /* Still can't get the lock, just keep waiting. */ return False; } /* * We have other than a "can't get lock" * error. Send an error and return True so we get dequeued. */ blocking_lock_reply_error(blr, status); return True; } /* We finally got the lock, return success. */ SSVAL(params,0,0); /* Fake up max_data_bytes here - we know it fits. */ send_trans2_replies(blr->fsp->conn, blr->req, NT_STATUS_OK, params, 2, NULL, 0, 0xffff); return True; }
/* Return 0 if succeeds, 1 if object doesn't exist. */ int ZSTLMapDelete(struct ZSTLMap *pm, char *key, uint32_t keylen) { uint64_t h; ZSTLMapEntry_t **ppme; ZSTLMapEntry_t *pme; ZSTLMapBucket_t *pb; char *key2; key2 = (char *) *((uint64_t *) key); do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapDelete: pm=%p, key=0x%lx, keylen=%d\n", pm, *((uint64_t *) key), keylen); #endif h = zs_hash((const unsigned char *) key, sizeof(uint64_t), 0) % pm->nbuckets; pb = &(pm->buckets[h]); zstlmap_assert(keylen == 8); // remove this! xxxzzz for (ppme = &(pb->entry); (*ppme) != NULL; ppme = &((*ppme)->next)) { pme = *ppme; if (pme->key == key2) { // Remove from the LRU list if necessary remove_lru(pm, pme); pm->n_entries--; *ppme = pme->next; free_pme(pm, pme); do_unlock(&(pm->mutex)); return (0); } } do_unlock(&(pm->mutex)); return (1); }
void ZSTLMapClear(struct ZSTLMap *pm) { ZSTLMapEntry_t *pme, *pme_next; do_lock(&(pm->mutex)); #ifdef SANDISK_PRINTSTUFF fprintf(stderr, "ZSTLMapClear: pm=%p\n", pm); #endif // Go through LRU list and free entries // This is much faster than examining all of the buckets! for (pme=pm->lru_head; pme != NULL; pme = pme_next) { pme_next = pme->next_lru; pme->bucket->entry = NULL; free_pme(pm, pme); } pm->n_entries = 0; pm->lru_head = NULL; pm->lru_tail = NULL; do_unlock(&(pm->mutex)); }
static void do_action(int action, struct cardstate *cs, struct bc_state *bcs, struct at_state_t **p_at_state, char **pp_command, int *p_genresp, int *p_resp_code, struct event_t *ev) { struct at_state_t *at_state = *p_at_state; struct bc_state *bcs2; unsigned long flags; int channel; unsigned char *s, *e; int i; unsigned long val; switch (action) { case ACT_NOTHING: break; case ACT_TIMEOUT: at_state->waiting = 1; break; case ACT_INIT: cs->at_state.pending_commands &= ~PC_INIT; cs->cur_at_seq = SEQ_NONE; cs->mode = M_UNIMODEM; spin_lock_irqsave(&cs->lock, flags); if (!cs->cidmode) { spin_unlock_irqrestore(&cs->lock, flags); gigaset_free_channels(cs); cs->mstate = MS_READY; break; } spin_unlock_irqrestore(&cs->lock, flags); cs->at_state.pending_commands |= PC_CIDMODE; gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE"); cs->commands_pending = 1; break; case ACT_FAILINIT: dev_warn(cs->dev, "Could not initialize the device.\n"); cs->dle = 0; init_failed(cs, M_UNKNOWN); cs->cur_at_seq = SEQ_NONE; break; case ACT_CONFIGMODE: init_failed(cs, M_CONFIG); cs->cur_at_seq = SEQ_NONE; break; case ACT_SETDLE1: cs->dle = 1; /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */ cs->inbuf[0].inputstate &= ~(INS_command | INS_DLE_command); break; case ACT_SETDLE0: cs->dle = 0; cs->inbuf[0].inputstate = (cs->inbuf[0].inputstate & ~INS_DLE_command) | INS_command; break; case ACT_CMODESET: if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) { gigaset_free_channels(cs); cs->mstate = MS_READY; } cs->mode = M_CID; cs->cur_at_seq = SEQ_NONE; break; case ACT_UMODESET: cs->mode = M_UNIMODEM; cs->cur_at_seq = SEQ_NONE; break; case ACT_FAILCMODE: cs->cur_at_seq = SEQ_NONE; if (cs->mstate == MS_INIT || cs->mstate == MS_RECOVER) { init_failed(cs, M_UNKNOWN); break; } if (reinit_and_retry(cs, -1) < 0) schedule_init(cs, MS_RECOVER); break; case ACT_FAILUMODE: cs->cur_at_seq = SEQ_NONE; schedule_init(cs, MS_RECOVER); break; case ACT_HUPMODEM: /* send "+++" (hangup in unimodem mode) */ if (cs->connected) { struct cmdbuf_t *cb; cb = kmalloc(sizeof(struct cmdbuf_t) + 3, GFP_ATOMIC); if (!cb) { dev_err(cs->dev, "%s: out of memory\n", __func__); return; } memcpy(cb->buf, "+++", 3); cb->len = 3; cb->offset = 0; cb->next = NULL; cb->wake_tasklet = NULL; cs->ops->write_cmd(cs, cb); } break; case ACT_RING: /* get fresh AT state structure for new CID */ at_state = get_free_channel(cs, ev->parameter); if (!at_state) { dev_warn(cs->dev, "RING ignored: could not allocate channel structure\n"); break; } /* initialize AT state structure * note that bcs may be NULL if no B channel is free */ at_state->ConState = 700; for (i = 0; i < STR_NUM; ++i) { kfree(at_state->str_var[i]); at_state->str_var[i] = NULL; } at_state->int_var[VAR_ZCTP] = -1; spin_lock_irqsave(&cs->lock, flags); at_state->timer_expires = RING_TIMEOUT; at_state->timer_active = 1; spin_unlock_irqrestore(&cs->lock, flags); break; case ACT_ICALL: handle_icall(cs, bcs, at_state); break; case ACT_FAILSDOWN: dev_warn(cs->dev, "Could not shut down the device.\n"); /* fall through */ case ACT_FAKESDOWN: case ACT_SDOWN: cs->cur_at_seq = SEQ_NONE; finish_shutdown(cs); break; case ACT_CONNECT: if (cs->onechannel) { at_state->pending_commands |= PC_DLE1; cs->commands_pending = 1; break; } bcs->chstate |= CHS_D_UP; gigaset_isdn_connD(bcs); cs->ops->init_bchannel(bcs); break; case ACT_DLE1: cs->cur_at_seq = SEQ_NONE; bcs = cs->bcs + cs->curchannel; bcs->chstate |= CHS_D_UP; gigaset_isdn_connD(bcs); cs->ops->init_bchannel(bcs); break; case ACT_FAKEHUP: at_state->int_var[VAR_ZSAU] = ZSAU_NULL; /* fall through */ case ACT_DISCONNECT: cs->cur_at_seq = SEQ_NONE; at_state->cid = -1; if (!bcs) { disconnect_nobc(p_at_state, cs); } else if (cs->onechannel && cs->dle) { /* Check for other open channels not needed: * DLE only used for M10x with one B channel. */ at_state->pending_commands |= PC_DLE0; cs->commands_pending = 1; } else { disconnect_bc(at_state, cs, bcs); } break; case ACT_FAKEDLE0: at_state->int_var[VAR_ZDLE] = 0; cs->dle = 0; /* fall through */ case ACT_DLE0: cs->cur_at_seq = SEQ_NONE; bcs2 = cs->bcs + cs->curchannel; disconnect_bc(&bcs2->at_state, cs, bcs2); break; case ACT_ABORTHUP: cs->cur_at_seq = SEQ_NONE; dev_warn(cs->dev, "Could not hang up.\n"); at_state->cid = -1; if (!bcs) disconnect_nobc(p_at_state, cs); else if (cs->onechannel) at_state->pending_commands |= PC_DLE0; else disconnect_bc(at_state, cs, bcs); schedule_init(cs, MS_RECOVER); break; case ACT_FAILDLE0: cs->cur_at_seq = SEQ_NONE; dev_warn(cs->dev, "Error leaving DLE mode.\n"); cs->dle = 0; bcs2 = cs->bcs + cs->curchannel; disconnect_bc(&bcs2->at_state, cs, bcs2); schedule_init(cs, MS_RECOVER); break; case ACT_FAILDLE1: cs->cur_at_seq = SEQ_NONE; dev_warn(cs->dev, "Could not enter DLE mode. Trying to hang up.\n"); channel = cs->curchannel; cs->bcs[channel].at_state.pending_commands |= PC_HUP; cs->commands_pending = 1; break; case ACT_CID: /* got cid; start dialing */ cs->cur_at_seq = SEQ_NONE; channel = cs->curchannel; if (ev->parameter > 0 && ev->parameter <= 65535) { cs->bcs[channel].at_state.cid = ev->parameter; cs->bcs[channel].at_state.pending_commands |= PC_DIAL; cs->commands_pending = 1; break; } /* bad cid: fall through */ case ACT_FAILCID: cs->cur_at_seq = SEQ_NONE; channel = cs->curchannel; if (reinit_and_retry(cs, channel) < 0) { dev_warn(cs->dev, "Could not get a call ID. Cannot dial.\n"); bcs2 = cs->bcs + channel; disconnect_bc(&bcs2->at_state, cs, bcs2); } break; case ACT_ABORTCID: cs->cur_at_seq = SEQ_NONE; bcs2 = cs->bcs + cs->curchannel; disconnect_bc(&bcs2->at_state, cs, bcs2); break; case ACT_DIALING: case ACT_ACCEPTED: cs->cur_at_seq = SEQ_NONE; break; case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL procssng */ if (bcs) disconnect_bc(at_state, cs, bcs); else disconnect_nobc(p_at_state, cs); break; case ACT_ABORTDIAL: /* error/timeout during dial preparation */ cs->cur_at_seq = SEQ_NONE; at_state->pending_commands |= PC_HUP; cs->commands_pending = 1; break; case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */ case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */ case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */ at_state->pending_commands |= PC_HUP; cs->commands_pending = 1; break; case ACT_GETSTRING: /* warning: RING, ZDLE, ... are not handled properly anymore */ at_state->getstring = 1; break; case ACT_SETVER: if (!ev->ptr) { *p_genresp = 1; *p_resp_code = RSP_ERROR; break; } s = ev->ptr; if (!strcmp(s, "OK")) { /* OK without version string: assume old response */ *p_genresp = 1; *p_resp_code = RSP_NONE; break; } for (i = 0; i < 4; ++i) { val = simple_strtoul(s, (char **) &e, 10); if (val > INT_MAX || e == s) break; if (i == 3) { if (*e) break; } else if (*e != '.') break; else s = e + 1; cs->fwver[i] = val; } if (i != 4) { *p_genresp = 1; *p_resp_code = RSP_ERROR; break; } cs->gotfwver = 0; break; case ACT_GOTVER: if (cs->gotfwver == 0) { cs->gotfwver = 1; gig_dbg(DEBUG_EVENT, "firmware version %02d.%03d.%02d.%02d", cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]); break; } /* fall through */ case ACT_FAILVER: cs->gotfwver = -1; dev_err(cs->dev, "could not read firmware version.\n"); break; case ACT_ERROR: gig_dbg(DEBUG_ANY, "%s: ERROR response in ConState %d", __func__, at_state->ConState); cs->cur_at_seq = SEQ_NONE; break; case ACT_DEBUG: gig_dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d", __func__, ev->type, at_state->ConState); break; case ACT_WARN: dev_warn(cs->dev, "%s: resp_code %d in ConState %d!\n", __func__, ev->type, at_state->ConState); break; case ACT_ZCAU: dev_warn(cs->dev, "cause code %04x in connection state %d.\n", ev->parameter, at_state->ConState); break; /* events from the LL */ case ACT_DIAL: if (!ev->ptr) { *p_genresp = 1; *p_resp_code = RSP_ERROR; break; } start_dial(at_state, ev->ptr, ev->parameter); break; case ACT_ACCEPT: start_accept(at_state); break; case ACT_HUP: at_state->pending_commands |= PC_HUP; gig_dbg(DEBUG_EVENT, "Scheduling PC_HUP"); cs->commands_pending = 1; break; /* hotplug events */ case ACT_STOP: do_stop(cs); break; case ACT_START: do_start(cs); break; /* events from the interface */ case ACT_IF_LOCK: cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs); cs->waiting = 0; wake_up(&cs->waitqueue); break; case ACT_IF_VER: if (ev->parameter != 0) cs->cmd_result = -EINVAL; else if (cs->gotfwver != 1) { cs->cmd_result = -ENOENT; } else { memcpy(ev->arg, cs->fwver, sizeof cs->fwver); cs->cmd_result = 0; } cs->waiting = 0; wake_up(&cs->waitqueue); break; /* events from the proc file system */ case ACT_PROC_CIDMODE: spin_lock_irqsave(&cs->lock, flags); if (ev->parameter != cs->cidmode) { cs->cidmode = ev->parameter; if (ev->parameter) { cs->at_state.pending_commands |= PC_CIDMODE; gig_dbg(DEBUG_EVENT, "Scheduling PC_CIDMODE"); } else { cs->at_state.pending_commands |= PC_UMMODE; gig_dbg(DEBUG_EVENT, "Scheduling PC_UMMODE"); } cs->commands_pending = 1; } spin_unlock_irqrestore(&cs->lock, flags); cs->waiting = 0; wake_up(&cs->waitqueue); break; /* events from the hardware drivers */ case ACT_NOTIFY_BC_DOWN: bchannel_down(bcs); break; case ACT_NOTIFY_BC_UP: bchannel_up(bcs); break; case ACT_SHUTDOWN: do_shutdown(cs); break; default: if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) { *pp_command = at_state->bcs->commands[action - ACT_CMD]; if (!*pp_command) { *p_genresp = 1; *p_resp_code = RSP_NULL; } } else dev_err(cs->dev, "%s: action==%d!\n", __func__, action); } }
static int exec_lock_unlock(int argc, char **argv, enum action action) { struct pkgdb *db = NULL; struct pkgdb_it *it = NULL; struct pkg *pkg = NULL; const char *pkgname; int match = MATCH_EXACT; int retcode; int exitcode = EX_OK; int ch; bool show_locked = false; struct option longopts[] = { { "all", no_argument, NULL, 'a' }, { "case-sensitive", no_argument, NULL, 'C' }, { "glob", no_argument, NULL, 'g' }, { "show-locked", no_argument, NULL, 'l' }, { "quiet", no_argument, NULL, 'q' }, { "regex", no_argument, NULL, 'x' }, { "yes", no_argument, NULL, 'y' }, { NULL, 0, NULL, 0 }, }; while ((ch = getopt_long(argc, argv, "aCgilqxy", longopts, NULL)) != -1) { switch (ch) { case 'a': match = MATCH_ALL; break; case 'C': pkgdb_set_case_sensitivity(true); break; case 'g': match = MATCH_GLOB; break; case 'i': pkgdb_set_case_sensitivity(false); break; case 'l': show_locked = true; break; case 'q': quiet = true; break; case 'x': match = MATCH_REGEX; break; case 'y': yes = true; break; default: usage_lock(); return (EX_USAGE); } } argc -= optind; argv += optind; if (!(match == MATCH_ALL && argc == 0) && argc != 1 && !show_locked) { usage_lock(); return (EX_USAGE); } if (match == MATCH_ALL) pkgname = NULL; else pkgname = argv[0]; retcode = pkgdb_access(PKGDB_MODE_READ|PKGDB_MODE_WRITE, PKGDB_DB_LOCAL); if (retcode == EPKG_ENODB) { if (match == MATCH_ALL) return (EX_OK); if (!quiet) warnx("No packages installed. Nothing to do!"); return (EX_OK); } else if (retcode == EPKG_ENOACCESS) { warnx("Insufficient privileges to modify the package database"); return (EX_NOPERM); } else if (retcode != EPKG_OK) { warnx("Error accessing the package database"); return (EX_SOFTWARE); } retcode = pkgdb_open(&db, PKGDB_DEFAULT); if (retcode != EPKG_OK) return (EX_IOERR); if (pkgdb_obtain_lock(db, PKGDB_LOCK_EXCLUSIVE) != EPKG_OK) { pkgdb_close(db); warnx("Cannot get an exclusive lock on database. " "It is locked by another process"); return (EX_TEMPFAIL); } if (match == MATCH_ALL || argc != 0) { if ((it = pkgdb_query(db, pkgname, match)) == NULL) { exitcode = EX_IOERR; goto cleanup; } while ((retcode = pkgdb_it_next(it, &pkg, 0)) == EPKG_OK) { if (action == LOCK) retcode = do_lock(db, pkg); else retcode = do_unlock(db, pkg); if (retcode != EPKG_OK) { exitcode = EX_IOERR; goto cleanup; } } } if (show_locked) retcode = list_locked(db); if (retcode != EPKG_END) exitcode = EX_IOERR; cleanup: if (pkg != NULL) pkg_free(pkg); if (it != NULL) pkgdb_it_free(it); pkgdb_release_lock(db, PKGDB_LOCK_EXCLUSIVE); pkgdb_close(db); return (exitcode); }