void dlthread_comm_finalize( dlthread_comm_t const comm_idx) { size_t i, myid; comm_t * comm; if (comm_idx != DLTHREAD_COMM_SINGLE) { myid = dlthread_get_id(comm_idx); dlthread_barrier(comm_idx); comm = my_comms+comm_idx; if (comm->larray) { /* destroy locks if they exist */ for (i=0;i<comm->nlarray;++i) { free_lock(comm->larray[myid]+i); } dl_free(comm->larray[myid]); dlthread_barrier(comm_idx); if (myid == 0) { dl_free(comm->larray); } } if (myid == 0) { dl_free(comm->buffer); free_barrier(&(comm->bar)); free_lock(&(comm->loc)); comm->in_use = 0; set_lock(ncomms_lock); if (comm_idx < last_free_comm) { last_free_comm = comm_idx; } unset_lock(ncomms_lock); } } else { /* clear this threads local buffer if it exists */ if (__local_buffer) { dl_free(__local_buffer); __local_buffer = NULL; __local_buffer_size = 0; } } }
static int rootfs_unmount(void *_ns) { nspace *ns; vnode *vn, *avn; ns = (nspace *) _ns; vn = ns->root; while (TRUE) { while(vn->head) vn = vn->head; vn = vn->parent; if (vn == ns->root) break; avn = vn->head; if (avn->prev) avn->prev->next = avn->next; else vn->head = avn->next; if (avn->next) avn->next->prev = avn->prev; rootfs_remove_vnode(ns, avn, TRUE); } free(ns->root); free_lock(&ns->lock); FreeSL(ns->skiplist); free(ns); return 0; }
static void __del_lock(struct lock_lookup *lock) { ll_pthread_rwlock_wrlock(&locks_rwlock); rb_erase(&lock->node, &locks); ll_pthread_rwlock_unlock(&locks_rwlock); free_lock(lock); }
int Y_Reclaim(int id) { util_t *util = util_rm(id); if(util == NULL) { log_err("Unable to remove util, id %d", id); return ERROR; } int rc = 0; void * data = util->data; switch(util->type) { case LOCK: rc = free_lock((lock_t*)data); break; case CVAR: rc = free_cvar((cvar_t*)data); break; case PIPE: rc = free_pipe((pipe_t*)data); break; default: break; } if(rc) { log_err("Fail to release util %d", id); return ERROR; } return 0; }
/* Very primitive. */ static void free_one_lock_list(lock_list *ll) { if (ll == NULL) return; free_boolexp(ll->key); st_delete(ll->type, &lock_names); free_lock(ll); }
void FcCacheFini (void) { int i; for (i = 0; i < FC_CACHE_MAX_LEVEL; i++) assert (fcCacheChains[i] == NULL); assert (fcCacheMaxLevel == 0); free_lock (); }
EXTERN void neko_global_free() { neko_free_jit(); free_root((value*)kind_names); free_root(apply_string); free_root(neko_builtins); free_root((value*)neko_fields); apply_string = NULL; free_local(neko_vm_context); free_lock(neko_fields_lock); neko_gc_major(); }
static int rootfs_free_dircookie(void *_ns, void *_node, void *_cookie) { nspace *ns; vnode *node; dirpos *cookie; ns = (nspace *) _ns; node = (vnode *) _node; cookie = (dirpos *) _cookie; free_lock(&cookie->lock); free(cookie); return 0; }
void fcntl_remove_locks(struct task_struct *task, struct file *filp, unsigned int fd) { struct file_lock *fl; struct file_lock **before; /* Find first lock owned by caller ... */ before = &filp->f_inode->i_flock; while ((fl = *before) && (task != fl->fl_owner || fd != fl->fl_fd)) before = &fl->fl_next; /* The list is sorted by owner and fd ... */ while ((fl = *before) && task == fl->fl_owner && fd == fl->fl_fd) free_lock(before); }
int vicc_exit(struct vicc_ctx *ctx) { int r = vicc_eject(ctx); if (ctx) { free_lock(ctx->io_lock); free(ctx->hostname); if (ctx->server_sock > 0) { ctx->server_sock = close(ctx->server_sock); if (ctx->server_sock < 0) { r -= 1; } } #ifdef _WIN32 WSACleanup(); #endif } return r; }
void one_job(int lpnumber) { int lp, open_sleep = 10; struct sockaddr_storage client; socklen_t clientlen = sizeof(client); if (getpeername(0, (struct sockaddr *)&client, &clientlen) >= 0) { char host[INET6_ADDRSTRLEN]; dolog(LOG_NOTICE, "Connection from %s port %hu\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client)); } if (get_lock(lpnumber) == 0) return; /* Make sure lp device is open... */ while ((lp = open_printer(lpnumber)) == -1) { sleep(open_sleep); if (open_sleep < 320) /* ~5 min interval to avoid spam in syslog */ open_sleep *= 2; } if (copy_stream(0, lp) < 0) dolog(LOGOPTS, "copy_stream: %m\n"); close(lp); free_lock(); }
void s_print(void) { printf("s_print called\n"); get_lock(); //synchronization //starting from the head hole, print out the entire hole list void *tmp_hole = head_hole; while ( tmp_hole != NULL ) { printf("Hole address: %lx, size: %d Bytes, next hole address: %lx\n", (long) tmp_hole, get_size_of_hole(tmp_hole), (long) get_next_hole_of(tmp_hole)); if ( tmp_hole - get_next_hole_of(tmp_hole) != 0 ) tmp_hole = get_next_hole_of(tmp_hole); else break; } free_lock(); //synchronization return; }
void *s_alloc(int req_size) { // minimum alloc 64 B and maximum 64 KB // hole size, hole start, next hole address // SYNCHRONIZE THIS FCN. WITH S_FREE() and OTHER THREADS get_lock(); printf("s_alloc called with size: %d\n", req_size); // round the requested size to avoid unusable memory fragments int unit = 12 + sizeof(long); // define the unit size req_size = (req_size < 64) ? 64 : req_size; req_size = (req_size > 65536) ? 65536 : req_size; req_size = ((req_size / unit) + 2) * unit; //req_size = req_size + 4; // additional 4-bytes just for holding size of block printf("new req_size: %d\n", req_size); if( curr_hole == NULL && head_hole == NULL ) { printf("We're out of space (totally!). Please try again later..\n"); free_lock(); return NULL; } //printf( "next_hole address: %lu - size: %d\n", next_hole_add, hole_size); void *search_starting_hole = curr_hole; void *prev_hole = NULL; do { int curr_hole_size = get_size_of_hole(curr_hole); // if we find a hole greater than requested space // just truncate the hole and return the requested amount of it if( curr_hole_size > req_size ) { void *allocated = curr_hole; // if that was the first and only hole if ( curr_hole == get_next_hole_of(curr_hole) && curr_hole == head_hole ) { curr_hole = curr_hole + req_size; //update our new smaller hole's attributes //make the next hole field point to itself again set_params_of_curr_hole( (long) curr_hole, curr_hole_size - req_size); head_hole = curr_hole; } // if it was one of the numerous holes // set the next_hole ptr. of previous hole accordingly else { // and if it was the last hole int is_last_hole = ( curr_hole == get_next_hole_of(curr_hole) ) ? 1 : 0; prev_hole = find_closest_hole_from_bottom(curr_hole); curr_hole = curr_hole + req_size; //if last --> make the next hole field point to itself again // else point to the next hole of old hole long next_add = (is_last_hole) ? (long) curr_hole : (long) get_next_hole_of(curr_hole - req_size); //update our new smaller hole's attributes set_params_of_curr_hole( next_add, curr_hole_size - req_size); // we may be looking at a hole which is head and // one of the numerous holes so in this case we shouldn't // try to set prev's next hole because there is no hole as previous if( prev_hole == NULL ) head_hole = curr_hole; else set_next_hole_of(prev_hole, (long) curr_hole); } printf("Alloc from %lx, new hole: %lx and new hole size: %d\n", (long)allocated, (long) curr_hole, get_size_of_hole(curr_hole)); //We're doing something a little bit different. //We're setting the size of allocated block into its bytes btw 8,11 //And returning an address starting from right after the size field set_size_of_allocated_block(allocated, req_size); allocated = allocated + 4; free_lock(); //synchronization return allocated; //curr_hole = ((void*) (next_hole_add + req_size)); } //if the requested memory exactly fits in our hole else if ( curr_hole_size == req_size ) { void *allocated = curr_hole; //and if that hole is the first and only hole remaining if ( curr_hole == get_next_hole_of(curr_hole) && curr_hole == head_hole ) { // we're out of holes as of now curr_hole = NULL; head_hole = NULL; } // and if that hole is one of the numerous holes else { //and this hole is one of the numerous holes and // it was the hole at the most bottom (head) if(curr_hole == head_hole) { // the next hole is the new head hole curr_hole = get_next_hole_of(curr_hole); head_hole = curr_hole; } // and that hole was in somewhere between the first and last holes else { prev_hole = find_closest_hole_from_bottom(curr_hole); curr_hole = get_next_hole_of(curr_hole); set_next_hole_of(prev_hole, (long) curr_hole); } } if( curr_hole == NULL && head_hole == NULL) printf("Alloc from %lx, no new hole, memory full.\n", (long)allocated ); else printf("Alloc from %lx, new hole add: %lx and new hole size: %d\n", (long)allocated, (long) curr_hole, get_size_of_hole(curr_hole)); set_size_of_allocated_block(allocated, req_size); allocated = allocated + 4; free_lock(); //synchronization return allocated; } // if this hole is smaller than the requested space else { // and that was the first and only hole if ( curr_hole == get_next_hole_of(curr_hole) && curr_hole == head_hole ) { //so.. there's no hope :( printf("We're out of space. Try again later..\n"); free_lock(); //synchronization return NULL; } // if there isn't a next hole anymore // just return to the head hole to search over the remaining holes else if ( curr_hole == get_next_hole_of(curr_hole) ) { curr_hole = head_hole; } // just pass to the next hole to keep searching else { prev_hole = curr_hole; curr_hole = get_next_hole_of(curr_hole); } } } while ( curr_hole != search_starting_hole ); // suitable hole not found printf("Suitable hole not found..\n"); free_lock(); //synchronization return NULL; }
void s_free(void *objectptr) { printf("*********************************\ns_free called with objectptr:%lx\n", (long) objectptr); printf("Deallocating block %lx , block size: %d\n", (long) (objectptr - 4), get_size_of_allocated_block(objectptr)); get_lock(); //synchronization //if there has been no hole until now if ( head_hole == NULL && curr_hole == NULL ) { // make this block the head hole head_hole = curr_hole = objectptr - 4; // new hole points to itself set_params_of_curr_hole( (long) curr_hole, get_size_of_allocated_block( objectptr)); } //if there have been holes else { void *closest_hole = NULL; // and every hole is above the space to be freed if( (closest_hole = find_closest_hole_from_bottom( objectptr)) == NULL ) { void *neighbor = find_neighbor_hole_of_block_from_up(objectptr ); // if head node is a neighbor of our new hole, merge them if (neighbor != NULL && neighbor == head_hole) { // adjust our new hole, it becomes the new head hole // new hole points to the old head hole printf("merging %lx with %lx\n", (long) objectptr - 4, (long) neighbor); set_size_of_hole(objectptr - 4, get_size_of_allocated_block(objectptr) + get_size_of_hole(neighbor) ); //if the head hole was the only hole and pointing to itself if( get_next_hole_of(neighbor) == neighbor ) set_next_hole_of(objectptr - 4, (long) objectptr - 4 ); // if the head hole was pointing to another hole else set_next_hole_of(objectptr - 4, (long) get_next_hole_of(neighbor)); head_hole = objectptr - 4; } else { // adjust our new hole, it becomes the new head hole // new hole points to the old head hole set_size_of_hole(objectptr - 4, get_size_of_allocated_block(objectptr)); set_next_hole_of(objectptr - 4, (long) head_hole ); head_hole = objectptr - 4; } } else { // and every hole is below the space to be freed if( closest_hole == get_next_hole_of(closest_hole) ) { void *neighbor = find_neighbor_hole_of_block_from_down(objectptr); //if the closest hole at the bottom is next to our new hole if( neighbor != NULL && neighbor == closest_hole ) { // just increase the size of head hole // by the amount of our deallocated space set_size_of_hole(neighbor, get_size_of_hole(neighbor) + get_size_of_allocated_block(objectptr)); } else { // set our deallocated space as the next hole of the closest hole set_next_hole_of(closest_hole, (long) objectptr - 4); // adjust our new hole set_size_of_hole(objectptr - 4, get_size_of_allocated_block(objectptr)); set_next_hole_of(objectptr - 4, (long) (objectptr - 4) ); } } // and the space to be freed is between holes else { void *up_neighbor; void *bottom_neighbor; up_neighbor = find_neighbor_hole_of_block_from_up(objectptr); bottom_neighbor = find_neighbor_hole_of_block_from_down(objectptr); //merge with upper and lower holes if( up_neighbor != NULL && bottom_neighbor != NULL) { printf("merging %lx with %lx and %lx\n", (long) objectptr - 4, (long) up_neighbor, (long) bottom_neighbor); int upper_hole_is_last = 0; void *next_of_upper = get_next_hole_of(up_neighbor); if( next_of_upper == up_neighbor) upper_hole_is_last = 1; int new_size = get_size_of_hole(bottom_neighbor) + get_size_of_hole(up_neighbor) + get_size_of_allocated_block(objectptr); set_size_of_hole(bottom_neighbor, new_size); //new merged hole becomes last hole, it will point to itself if(upper_hole_is_last) set_next_hole_of(bottom_neighbor, (long) bottom_neighbor); else set_next_hole_of(bottom_neighbor, (long) next_of_upper); } //merge with upper hole else if( up_neighbor != NULL && bottom_neighbor == NULL) { // adjust our new hole // new hole points to the hole printf("merging %lx with %lx\n", (long) objectptr - 4, (long) up_neighbor); //our upper hole's prev. hole void *prev_hole = find_closest_hole_from_bottom(up_neighbor); //now it points to our merged hole set_next_hole_of(prev_hole, (long) objectptr - 4); set_size_of_hole(objectptr - 4, get_size_of_allocated_block(objectptr) + get_size_of_hole(up_neighbor) ); //if the upper hole was the last hole and pointing to itself if( get_next_hole_of(up_neighbor) == up_neighbor ) set_next_hole_of(objectptr - 4, (long) objectptr - 4 ); // if the upper hole was pointing to another hole else set_next_hole_of(objectptr - 4, (long) get_next_hole_of(up_neighbor)); } //merge with lower hole else if( up_neighbor == NULL && bottom_neighbor != NULL) { printf("merging %lx with %lx\n", (long) objectptr - 4, (long) bottom_neighbor); //just increase the size of the lower hole // by the size of deallocated block set_size_of_hole(bottom_neighbor, get_size_of_allocated_block(objectptr) + get_size_of_hole(bottom_neighbor) ); } //do not merge with any holes else { void *next = get_next_hole_of(closest_hole); // set our deallocated space as the next hole of the closest hole set_next_hole_of(closest_hole, (long) objectptr - 4); // adjust our new hole set_size_of_hole(objectptr - 4, get_size_of_allocated_block(objectptr)); set_next_hole_of(objectptr - 4, (long) next ); } } } } printf("******************************************\n\n"); free_lock(); //synchronization return; }
cache_inode_status_t cache_inode_kill_entry( cache_entry_t * pentry, cache_inode_lock_how_t lock_how, hash_table_t * ht, cache_inode_client_t * pclient, cache_inode_status_t * pstatus ) { fsal_handle_t *pfsal_handle = NULL; cache_inode_fsal_data_t fsaldata; cache_inode_parent_entry_t *parent_iter = NULL; cache_inode_parent_entry_t *parent_iter_next = NULL; hash_buffer_t key, old_key; hash_buffer_t old_value; int rc; fsal_status_t fsal_status; memset( (char *)&fsaldata, 0, sizeof( fsaldata ) ) ; LogInfo(COMPONENT_CACHE_INODE, "Using cache_inode_kill_entry for entry %p", pentry); /* Invalidation is not for junctions or special files */ if( ( pentry->internal_md.type == FS_JUNCTION ) || ( pentry->internal_md.type == SOCKET_FILE ) || ( pentry->internal_md.type == FIFO_FILE ) || ( pentry->internal_md.type == CHARACTER_FILE ) || ( pentry->internal_md.type == BLOCK_FILE ) ) { free_lock( pentry, lock_how ) ; *pstatus = CACHE_INODE_SUCCESS; return *pstatus; } #if 0 /** @todo: BUGAZOMEU : directory invalidation seems quite tricky, temporarily avoid it */ if( pentry->internal_md.type == DIRECTORY ) { free_lock( pentry, lock_how ) ; *pstatus = CACHE_INODE_SUCCESS; return *pstatus; } /** @todo: BUGAZOMEU : file invalidation seems quite tricky, temporarily avoid it */ /* We need to know how to manage how to deal with "files with states" */ if( pentry->internal_md.type == REGULAR_FILE ) { free_lock( pentry, lock_how ) ; *pstatus = CACHE_INODE_SUCCESS; return *pstatus; } #endif if(pstatus == NULL) return CACHE_INODE_INVALID_ARGUMENT; if(pentry == NULL || pclient == NULL || ht == NULL) { free_lock( pentry, lock_how ) ; *pstatus = CACHE_INODE_INVALID_ARGUMENT; return *pstatus; } /* Get the FSAL handle */ if((pfsal_handle = cache_inode_get_fsal_handle(pentry, pstatus)) == NULL) { free_lock( pentry, lock_how ) ; LogCrit(COMPONENT_CACHE_INODE, "cache_inode_kill_entry: unable to retrieve pentry's specific filesystem info"); return *pstatus; } /* Invalidate the related LRU gc entry (no more required) */ if(pentry->gc_lru_entry != NULL) { if(LRU_invalidate(pentry->gc_lru, pentry->gc_lru_entry) != LRU_LIST_SUCCESS) { free_lock( pentry, lock_how ) ; *pstatus = CACHE_INODE_LRU_ERROR; return *pstatus; } } fsaldata.handle = *pfsal_handle; fsaldata.cookie = DIR_START; /* Use the handle to build the key */ if(cache_inode_fsaldata_2_key(&key, &fsaldata, pclient)) { free_lock( pentry, lock_how ) ; LogCrit(COMPONENT_CACHE_INODE, "cache_inode_kill_entry: could not build hashtable key"); cache_inode_release_fsaldata_key(&key, pclient); *pstatus = CACHE_INODE_NOT_FOUND; return *pstatus; } /* use the key to delete the entry */ if((rc = HashTable_Del(ht, &key, &old_key, &old_value)) != HASHTABLE_SUCCESS) { if( rc != HASHTABLE_ERROR_NO_SUCH_KEY) /* rc=3 => Entry was previously removed */ LogCrit( COMPONENT_CACHE_INODE, "cache_inode_kill_entry: entry could not be deleted, status = %d", rc); cache_inode_release_fsaldata_key(&key, pclient); *pstatus = CACHE_INODE_NOT_FOUND; return *pstatus; } /* Release the hash key data */ cache_inode_release_fsaldata_key(&old_key, pclient); /* Clean up the associated ressources in the FSAL */ if(FSAL_IS_ERROR(fsal_status = FSAL_CleanObjectResources(pfsal_handle))) { LogCrit(COMPONENT_CACHE_INODE, "cache_inode_kill_entry: Couldn't free FSAL ressources fsal_status.major=%u", fsal_status.major); } /* Sanity check: old_value.pdata is expected to be equal to pentry, * and is released later in this function */ if((cache_entry_t *) old_value.pdata != pentry) { LogCrit(COMPONENT_CACHE_INODE, "cache_inode_kill_entry: unexpected pdata %p from hash table (pentry=%p)", old_value.pdata, pentry); } /* Release the current key */ cache_inode_release_fsaldata_key(&key, pclient); /* Recover the parent list entries */ parent_iter = pentry->parent_list; while(parent_iter != NULL) { parent_iter_next = parent_iter->next_parent; ReleaseToPool(parent_iter, &pclient->pool_parent); parent_iter = parent_iter_next; } /* If entry is datacached, remove it from the cache */ if(pentry->internal_md.type == REGULAR_FILE) { cache_content_status_t cache_content_status; if(pentry->object.file.pentry_content != NULL) if(cache_content_release_entry ((cache_content_entry_t *) pentry->object.file.pentry_content, (cache_content_client_t *) pclient->pcontent_client, &cache_content_status) != CACHE_CONTENT_SUCCESS) LogCrit(COMPONENT_CACHE_INODE, "Could not removed datacached entry for pentry %p", pentry); } /* If entry is a DIRECTORY, invalidate dirents */ if(pentry->internal_md.type == DIRECTORY) { cache_inode_invalidate_related_dirents(pentry, pclient); } // free_lock( pentry, lock_how ) ; /* Really needed ? The pentry is unaccessible now and will be destroyed */ /* Destroy the mutex associated with the pentry */ cache_inode_mutex_destroy(pentry); /* Put the pentry back to the pool */ ReleaseToPool(pentry, &pclient->pool_entry); *pstatus = CACHE_INODE_SUCCESS; return *pstatus; } /* cache_inode_kill_entry */
static int lock_it(struct file *filp, struct file_lock *caller, unsigned int fd) { struct file_lock *fl; struct file_lock *left = 0; struct file_lock *right = 0; struct file_lock **before; int added = 0; /* * Find the first old lock with the same owner as the new lock. */ before = &filp->f_inode->i_flock; while ((fl = *before) && (caller->fl_owner != fl->fl_owner || caller->fl_fd != fl->fl_fd)) before = &fl->fl_next; /* * Look up all locks of this owner. */ while ( (fl = *before) && caller->fl_owner == fl->fl_owner && caller->fl_fd == fl->fl_fd) { /* * Detect adjacent or overlapping regions (if same lock type) */ if (caller->fl_type == fl->fl_type) { if (fl->fl_end < caller->fl_start - 1) goto next_lock; /* * If the next lock in the list has entirely bigger * addresses than the new one, insert the lock here. */ if (fl->fl_start > caller->fl_end + 1) break; /* * If we come here, the new and old lock are of the * same type and adjacent or overlapping. Make one * lock yielding from the lower start address of both * locks to the higher end address. */ if (fl->fl_start > caller->fl_start) fl->fl_start = caller->fl_start; else caller->fl_start = fl->fl_start; if (fl->fl_end < caller->fl_end) fl->fl_end = caller->fl_end; else caller->fl_end = fl->fl_end; if (added) { free_lock(before); continue; } caller = fl; added = 1; goto next_lock; } /* * Processing for different lock types is a bit more complex. */ if (fl->fl_end < caller->fl_start) goto next_lock; if (fl->fl_start > caller->fl_end) break; if (caller->fl_type == F_UNLCK) added = 1; if (fl->fl_start < caller->fl_start) left = fl; /* * If the next lock in the list has a higher end address than * the new one, insert the new one here. */ if (fl->fl_end > caller->fl_end) { right = fl; break; } if (fl->fl_start >= caller->fl_start) { /* * The new lock completely replaces an old one (This may * happen several times). */ if (added) { free_lock(before); continue; } /* * Replace the old lock with the new one. Wake up * anybody waiting for the old one, as the change in * lock type might satisfy his needs. */ wake_up(&fl->fl_wait); fl->fl_start = caller->fl_start; fl->fl_end = caller->fl_end; fl->fl_type = caller->fl_type; caller = fl; added = 1; } /* * Go on to next lock. */ next_lock: before = &(*before)->fl_next; } if (! added) { if (caller->fl_type == F_UNLCK) { /* * XXX - under iBCS-2, attempting to unlock a not-locked region is * not considered an error condition, although I'm not sure if this * should be a default behavior (it makes porting to native Linux easy) * or a personality option. * * Does Xopen/1170 say anything about this? * - [email protected] */ #if 0 return -EINVAL; #else return 0; #endif } if (! (caller = alloc_lock(before, caller, fd))) return -ENOLCK; } if (right) { if (left == right) { /* * The new lock breaks the old one in two pieces, so we * have to allocate one more lock (in this case, even * F_UNLCK may fail!). */ if (! (left = alloc_lock(before, right, fd))) { if (! added) free_lock(before); return -ENOLCK; } } right->fl_start = caller->fl_end + 1; } if (left) left->fl_end = caller->fl_start - 1; return 0; }
void server(int lpnumber) { struct rlimit resourcelimit; #ifdef USE_GETPROTOBYNAME struct protoent *proto; #endif int netfd = -1, fd, lp, one = 1; int open_sleep = 10; socklen_t clientlen; struct sockaddr_storage client; struct addrinfo hints, *res, *ressave; char pidfilename[sizeof(PIDFILE)]; char service[10]; // 9100 (65535 max) FILE *f; const int bufsiz = 65536; #ifndef TESTING if (!log_to_stdout) { switch (fork()) { case -1: dolog(LOGOPTS, "fork: %m\n"); exit(1); case 0: /* child */ break; default: /* parent */ exit(0); } /* Now in child process */ resourcelimit.rlim_max = 0; if (getrlimit(RLIMIT_NOFILE, &resourcelimit) < 0) { dolog(LOGOPTS, "getrlimit: %m\n"); exit(1); } for (fd = 0; fd < resourcelimit.rlim_max; ++fd) (void)close(fd); if (setsid() < 0) { dolog(LOGOPTS, "setsid: %m\n"); exit(1); } (void)chdir("/"); (void)umask(022); fd = open("/dev/null", O_RDWR); /* stdin */ (void)dup(fd); /* stdout */ (void)dup(fd); /* stderr */ (void)snprintf(pidfilename, sizeof(pidfilename), PIDFILE, lpnumber); if ((f = fopen(pidfilename, "w")) == NULL) { dolog(LOGOPTS, "%s: %m\n", pidfilename); exit(1); } (void)fprintf(f, "%d\n", getpid()); (void)fclose(f); } if (get_lock(lpnumber) == 0) exit(1); #endif memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_flags = AI_PASSIVE; hints.ai_socktype = SOCK_STREAM; (void)snprintf(service, sizeof(service), "%hu", (BASEPORT + lpnumber - '0')); if (getaddrinfo(bindaddr, service, &hints, &res) != 0) { dolog(LOGOPTS, "getaddr: %m\n"); exit(1); } ressave = res; while (res) { #ifdef USE_GETPROTOBYNAME if ((proto = getprotobyname("tcp6")) == NULL) { if ((proto = getprotobyname("tcp")) == NULL) { dolog(LOGOPTS, "Cannot find protocol for TCP!\n"); exit(1); } } if ((netfd = socket(res->ai_family, res->ai_socktype, proto->p_proto)) < 0) #else if ((netfd = socket(res->ai_family, res->ai_socktype, IPPROTO_IP)) < 0) #endif { dolog(LOGOPTS, "socket: %m\n"); close(netfd); res = res->ai_next; continue; } if (setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &bufsiz, sizeof(bufsiz)) < 0) { dolog(LOGOPTS, "setsocketopt: SO_RCVBUF: %m\n"); /* not fatal if it fails */ } if (setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &bufsiz, sizeof(bufsiz)) < 0) { dolog(LOGOPTS, "setsocketopt: SO_SNDBUF: %m\n"); /* not fatal if it fails */ } if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) { dolog(LOGOPTS, "setsocketopt: SO_REUSEADDR: %m\n"); close(netfd); res = res->ai_next; continue; } if (bind(netfd, res->ai_addr, res->ai_addrlen) < 0) { dolog(LOGOPTS, "bind: %m\n"); close(netfd); res = res->ai_next; continue; } if (listen(netfd, 5) < 0) { dolog(LOGOPTS, "listen: %m\n"); close(netfd); res = res->ai_next; continue; } break; } freeaddrinfo(ressave); clientlen = sizeof(client); memset(&client, 0, sizeof(client)); while ((fd = accept(netfd, (struct sockaddr *)&client, &clientlen)) >= 0) { char host[INET6_ADDRSTRLEN]; #ifdef USE_LIBWRAP if (hosts_ctl("p910nd", STRING_UNKNOWN, get_ip_str((struct sockaddr *)&client, host, sizeof(host)), STRING_UNKNOWN) == 0) { dolog(LOGOPTS, "Connection from %s port %hu rejected\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client)); close(fd); continue; } #endif dolog(LOG_NOTICE, "Connection from %s port %hu accepted\n", get_ip_str((struct sockaddr *)&client, host, sizeof(host)), get_port((struct sockaddr *)&client)); /*write(fd, "Printing", 8); */ /* Make sure lp device is open... */ while ((lp = open_printer(lpnumber)) == -1) { sleep(open_sleep); if (open_sleep < 320) /* ~5 min interval to avoid spam in syslog */ open_sleep *= 2; } open_sleep = 10; if (copy_stream(fd, lp) < 0) dolog(LOGOPTS, "copy_stream: %m\n"); (void)close(fd); (void)close(lp); } dolog(LOGOPTS, "accept: %m\n"); free_lock(); exit(1); }
static int lock_it(struct file *filp, struct file_lock *caller, unsigned int fd) { struct file_lock *fl; struct file_lock *left = 0; struct file_lock *right = 0; struct file_lock **before; int added = 0; /* * Find the first old lock with the same owner as the new lock. */ before = &filp->f_inode->i_flock; while ((fl = *before) && (caller->fl_owner != fl->fl_owner || caller->fl_fd != fl->fl_fd)) before = &fl->fl_next; /* * Look up all locks of this owner. */ while ( (fl = *before) && caller->fl_owner == fl->fl_owner && caller->fl_fd == fl->fl_fd) { /* * Detect adjacent or overlapping regions (if same lock type) */ if (caller->fl_type == fl->fl_type) { if (fl->fl_end < caller->fl_start - 1) goto next_lock; /* * If the next lock in the list has entirely bigger * addresses than the new one, insert the lock here. */ if (fl->fl_start > caller->fl_end + 1) break; /* * If we come here, the new and old lock are of the * same type and adjacent or overlapping. Make one * lock yielding from the lower start address of both * locks to the higher end address. */ if (fl->fl_start > caller->fl_start) fl->fl_start = caller->fl_start; else caller->fl_start = fl->fl_start; if (fl->fl_end < caller->fl_end) fl->fl_end = caller->fl_end; else caller->fl_end = fl->fl_end; if (added) { free_lock(before); continue; } caller = fl; added = 1; goto next_lock; } /* * Processing for different lock types is a bit more complex. */ if (fl->fl_end < caller->fl_start) goto next_lock; if (fl->fl_start > caller->fl_end) break; if (caller->fl_type == F_UNLCK) added = 1; if (fl->fl_start < caller->fl_start) left = fl; /* * If the next lock in the list has a higher end address than * the new one, insert the new one here. */ if (fl->fl_end > caller->fl_end) { right = fl; break; } if (fl->fl_start >= caller->fl_start) { /* * The new lock completely replaces an old one (This may * happen several times). */ if (added) { free_lock(before); continue; } /* * Replace the old lock with the new one. Wake up * anybody waiting for the old one, as the change in * lock type migth satisfy his needs. */ wake_up(&fl->fl_wait); fl->fl_start = caller->fl_start; fl->fl_end = caller->fl_end; fl->fl_type = caller->fl_type; caller = fl; added = 1; } /* * Go on to next lock. */ next_lock: before = &(*before)->fl_next; } if (! added) { if (caller->fl_type == F_UNLCK) return -EINVAL; if (! (caller = alloc_lock(before, caller, fd))) return -ENOLCK; } if (right) { if (left == right) { /* * The new lock breaks the old one in two pieces, so we * have to allocate one more lock (in this case, even * F_UNLCK may fail!). */ if (! (left = alloc_lock(before, right, fd))) { if (! added) free_lock(before); return -ENOLCK; } } right->fl_start = caller->fl_end + 1; } if (left) left->fl_end = caller->fl_start - 1; return 0; }
static int rootfs_mount(nspace_id nsid, const char *device, ulong flags, void *parms, size_t len, void **data, vnode_id *vnid) { int err; nspace *ns; vnode *root; vnode_id rvnid; if (device || parms || (len != 0)) { err = EINVAL; goto error1; } ns = (nspace *) malloc(sizeof(nspace)); if (!ns) { err = ENOMEM; goto error1; } root = (vnode *) malloc(sizeof(vnode)); if (!root) { err = ENOMEM; goto error2; } rvnid = 1; ns->nsid = nsid; ns->vnnum = 0; ns->nxvnid = rvnid; ns->root = root; if (new_lock(&ns->lock, "rootfs") < 0) { err = -1; goto error3; } ns->skiplist = NewSL(&compare_vnode, NULL, NO_DUPLICATES); if (!ns->skiplist) { err = -1; goto error4; } root->vnid = rvnid; root->parent = root; root->ns = ns; root->removed = FALSE; root->name = NULL; root->next = root->prev = NULL; root->head = NULL; root->symlink = NULL; /* ### do it for real */ root->uid = 0; root->gid = 0; root->mode = MY_S_IFDIR | 0777; root->mtime = time(NULL); err = new_vnode(nsid, rvnid, root); if (err) goto error5; *data = ns; *vnid = rvnid; return 0; error5: FreeSL(ns->skiplist); error4: free_lock(&ns->lock); error3: free(root); error2: free(ns); error1: return err; }
void dlthread_free_lock( dlthread_lock_t * lock) { free_lock(lock); }
void dlthread_launch( size_t const nthreads, void (*funptr)(void*), void * const ptr) { size_t myid, i; comm_t * comm; comm = my_comms+DLTHREAD_COMM_ROOT; ncomms_lock = malloc(sizeof(dlthread_lock_t)); init_lock(ncomms_lock); __config_comm(comm,nthreads); #ifdef __DOMLIB_USE_PTHREADS size_t i; pthread_t * threads; thread_arg_t * args; threads = malloc(sizeof(pthread_t)*nthreads); args = malloc(sizeof(thread_arg_t)*nthreads); for (i=0;i<nthreads;++i) { args[i].id = i; args[i].ptr = ptr; args[i].funptr = funptr; pthread_create(threads+i,NULL,&__thread_start,args+i); } for (i=0;i<nthreads;++i) { pthread_join(threads[i],NULL); } dl_free(threads); dl_free(args); #else #pragma omp parallel num_threads(nthreads) { thread_arg_t arg; arg.id = omp_get_thread_num(); arg.ptr = ptr; arg.funptr = funptr; __thread_start(&arg); } #endif if (comm->larray) { for (myid=0;myid<nthreads;++myid) { /* destroy locks if they exist */ for (i=0;i<comm->nlarray;++i) { free_lock(comm->larray[myid]+i); } dl_free(comm->larray[myid]); } dl_free(comm->larray); } dl_free(comm->buffer); free_barrier(&(comm->bar)); free_lock(&(comm->loc)); comm->in_use = 0; free_lock(ncomms_lock); dl_free(ncomms_lock); }
int main(int argc, char* argv[]) { char *albumdir = 0, *musicfilename, *file_path = 0; int i, area_idx; sacd_reader_t *sacd_reader; #ifdef PTW32_STATIC_LIB pthread_win32_process_attach_np(); pthread_win32_thread_attach_np(); #endif init(); if (parse_options(argc, argv)) { setlocale(LC_ALL, ""); if (fwide(stdout, 1) < 0) { fprintf(stderr, "ERROR: Output not set to wide.\n"); } // default to 2 channel if (opts.two_channel == 0 && opts.multi_channel == 0) { opts.two_channel = 1; } sacd_reader = sacd_open(opts.input_device); if (sacd_reader) { handle = scarletbook_open(sacd_reader, 0); if (handle) { if (opts.print) { scarletbook_print(handle); } if (opts.output_dsf || opts.output_iso || opts.output_dsdiff || opts.output_dsdiff_em || opts.export_cue_sheet) { output = scarletbook_output_create(handle, handle_status_update_track_callback, handle_status_update_progress_callback, safe_fwprintf); // select the channel area area_idx = ((has_multi_channel(handle) && opts.multi_channel) || !has_two_channel(handle)) ? handle->mulch_area_idx : handle->twoch_area_idx; albumdir = (strlen(opts.output_file) > 0 ? strdup(opts.output_file) : get_album_dir(handle)); if (opts.output_iso) { uint32_t total_sectors = sacd_get_total_sectors(sacd_reader); #ifdef SECTOR_LIMIT #define FAT32_SECTOR_LIMIT 2090000 uint32_t sector_size = FAT32_SECTOR_LIMIT; uint32_t sector_offset = 0; if (total_sectors > FAT32_SECTOR_LIMIT) { musicfilename = (char *) malloc(512); file_path = make_filename(0, 0, albumdir, "iso"); for (i = 1; total_sectors != 0; i++) { sector_size = min(total_sectors, FAT32_SECTOR_LIMIT); snprintf(musicfilename, 512, "%s.%03d", file_path, i); scarletbook_output_enqueue_raw_sectors(output, sector_offset, sector_size, musicfilename, "iso"); sector_offset += sector_size; total_sectors -= sector_size; } free(musicfilename); } else #endif { get_unique_filename(&albumdir, "iso"); file_path = make_filename(0, 0, albumdir, "iso"); scarletbook_output_enqueue_raw_sectors(output, 0, total_sectors, file_path, "iso"); } } else if (opts.output_dsdiff_em) { get_unique_filename(&albumdir, "dff"); file_path = make_filename(0, 0, albumdir, "dff"); scarletbook_output_enqueue_track(output, area_idx, 0, file_path, "dsdiff_edit_master", (opts.convert_dst ? 1 : handle->area[area_idx].area_toc->frame_format != FRAME_FORMAT_DST)); } else if (opts.output_dsf || opts.output_dsdiff) { // create the output folder get_unique_dir(0, &albumdir); recursive_mkdir(albumdir, 0774); // fill the queue with items to rip for (i = 0; i < handle->area[area_idx].area_toc->track_count; i++) { if (opts.select_tracks && opts.selected_tracks[i] == 0) continue; musicfilename = get_music_filename(handle, area_idx, i, opts.output_file); if (opts.output_dsf) { file_path = make_filename(0, albumdir, musicfilename, "dsf"); scarletbook_output_enqueue_track(output, area_idx, i, file_path, "dsf", 1 /* always decode to DSD */); } else if (opts.output_dsdiff) { file_path = make_filename(0, albumdir, musicfilename, "dff"); scarletbook_output_enqueue_track(output, area_idx, i, file_path, "dsdiff", (opts.convert_dst ? 1 : handle->area[area_idx].area_toc->frame_format != FRAME_FORMAT_DST)); } free(musicfilename); free(file_path); file_path = 0; } } if (opts.export_cue_sheet) { char *cue_file_path = make_filename(0, 0, albumdir, "cue"); #ifdef _WIN32 wchar_t *wide_filename = (wchar_t *) charset_convert(cue_file_path, strlen(cue_file_path), "UTF-8", sizeof(wchar_t) == 2 ? "UCS-2-INTERNAL" : "UCS-4-INTERNAL"); #else wchar_t *wide_filename = (wchar_t *) charset_convert(cue_file_path, strlen(cue_file_path), "UTF-8", "WCHAR_T"); #endif fwprintf(stdout, L"Exporting CUE sheet [%ls]\n", wide_filename); if (!file_path) file_path = make_filename(0, 0, albumdir, "dff"); write_cue_sheet(handle, file_path, area_idx, cue_file_path); free(cue_file_path); free(wide_filename); } free(file_path); started_processing = time(0); scarletbook_output_start(output); scarletbook_output_destroy(output); fprintf(stdout, "\rWe are done.. \n"); } scarletbook_close(handle); free(albumdir); } } sacd_close(sacd_reader); #ifndef _WIN32 freopen(0, "w", stdout); #endif if (fwide(stdout, -1) >= 0) { fprintf(stderr, "ERROR: Output not set to byte oriented.\n"); } } free_lock(g_fwprintf_lock); destroy_logging(); #ifdef PTW32_STATIC_LIB pthread_win32_process_detach_np(); pthread_win32_thread_detach_np(); #endif printf("\n"); return 0; }