static carmen_inline void push_state(int x, int y, double new_utility, queue state_queue) { state_ptr new_state = carmen_conventional_create_state(x, y, new_utility); insert_into_queue(new_state, state_queue); *(utility_value(x, y)) = new_utility; if (*(costs+x*y_size+y) > 0.9) carmen_warn("Bad utility\n"); }
int sys_semop (int semid, struct sembuf *tsops, unsigned nsops) { int i, id, size, error; struct semid_ds *sma; struct sembuf sops[SEMOPM], *sop; struct sem_undo *un; int undos = 0, alter = 0; if (nsops < 1 || semid < 0) return -EINVAL; if (nsops > SEMOPM) return -E2BIG; if (!tsops) return -EFAULT; if ((i = verify_area (VERIFY_READ, tsops, nsops * sizeof(*tsops)))) return i; memcpy_fromfs (sops, tsops, nsops * sizeof(*tsops)); id = (unsigned int) semid % SEMMNI; if ((sma = semary[id]) == IPC_UNUSED || sma == IPC_NOID) return -EINVAL; if (sma->sem_perm.seq != (unsigned int) semid / SEMMNI) return -EIDRM; for (i = 0; i < nsops; i++) { sop = &sops[i]; if (sop->sem_num >= sma->sem_nsems) return -EFBIG; if (sop->sem_flg & SEM_UNDO) undos++; if (sop->sem_op) alter++; } if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) return -EACCES; error = try_semop(sma, sops, nsops); if (error < 0) return error; if (undos) { /* Make sure we have an undo structure * for this process and this semaphore set. */ for (un = current->semundo; un; un = un->proc_next) if (un->semid == semid) break; if (!un) { size = sizeof(struct sem_undo) + sizeof(short)*sma->sem_nsems; un = (struct sem_undo *) kmalloc(size, GFP_ATOMIC); if (!un) return -ENOMEM; memset(un, 0, size); un->semadj = (short *) &un[1]; un->semid = semid; un->proc_next = current->semundo; current->semundo = un; un->id_next = sma->undo; sma->undo = un; } } else un = NULL; if (error == 0) { /* the operations go through immediately */ error = do_semop(sma, sops, nsops, un, current->pid); /* maybe some queued-up processes were waiting for this */ update_queue(sma); return error; } else { /* We need to sleep on this operation, so we put the current * task into the pending queue and go to sleep. */ struct sem_queue queue; queue.sma = sma; queue.sops = sops; queue.nsops = nsops; queue.undo = un; queue.pid = current->pid; queue.status = 0; insert_into_queue(sma,&queue); queue.sleeper = NULL; current->semsleeping = &queue; interruptible_sleep_on(&queue.sleeper); current->semsleeping = NULL; /* When we wake up, either the operation is finished, * or some kind of error happened. */ if (!queue.prev) { /* operation is finished, update_queue() removed us */ return queue.status; } else { remove_from_queue(sma,&queue); return -EINTR; } } }
void rcv_file_get (struct htlc_conn *htlc) { u_int16_t fnlen = 0, preview = 0; char path[MAXPATHLEN], dir[MAXPATHLEN], filename[NAME_MAX]; char abuf[HOSTLEN+1], buf[128]; struct stat sb; u_int32_t size = 0, data_size = 0, rsrc_size = 0, ref, data_pos = 0, rsrc_pos = 0; int err, siz, len; struct SOCKADDR_IN lsaddr; struct htxf_conn *htxf; u_int16_t i; #if defined(CONFIG_HTXF_QUEUE) u_int16_t queue_pos; #endif dir[0] = 0; if (htlc->nr_gets >= htlc->get_limit) { for (i = 0; i < HTXF_GET_MAX; i++) { htxf = htlc->htxf_out[i]; if (!htxf) continue; if ((htxf->total_pos == htxf->total_size) || htxf->gone) goto ok; } len = snprintf(buf, sizeof(buf), "%u at a time", htlc->get_limit); hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, len, buf); return; } ok: #if defined(CONFIG_HTXF_QUEUE) if (!htlc->access_extra.ignore_queue && nr_queued >= hxd_cfg.limits.queue_size) { #else if (nr_gets >= hxd_cfg.limits.total_downloads) { #endif #if defined(CONFIG_HTXF_QUEUE) len = snprintf(buf, sizeof(buf), "queue is full (%u >= %d) please try again later", nr_gets, hxd_cfg.limits.queue_size); #else len = snprintf(buf, sizeof(buf), "maximum number of total downloads reached (%u >= %d)", nr_gets, hxd_cfg.limits.total_downloads); #endif hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, len, buf); return; } for (i = 0; i < HTXF_GET_MAX; i++) if (!htlc->htxf_out[i]) break; if (i == HTXF_GET_MAX) { snd_strerror(htlc, EAGAIN); return; } dh_start(htlc) switch (dh_type) { case HTLC_DATA_FILE_NAME: fnlen = dh_len >= NAME_MAX ? NAME_MAX - 1 : dh_len; read_filename(filename, dh_data, fnlen); break; case HTLC_DATA_DIR: if ((err = hldir_to_path(dh, ROOTDIR, dir, dir))) { snd_strerror(htlc, err); return; } break; case HTLC_DATA_RFLT: if (dh_len >= 50) L32NTOH(data_pos, &dh_data[46]); if (dh_len >= 66) L32NTOH(rsrc_pos, &dh_data[62]); break; case HTLC_DATA_FILE_PREVIEW: dh_getint(preview); break; } dh_end() if (!fnlen && !dir[0]) { /* No file name given */ hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, 6, "huh?!?"); return; } if (dir[0]) { if (fnlen) snprintf(path, sizeof(path), "%s/%s", dir, filename); else strcpy(path, dir); } else { snprintf(path, sizeof(path), "%s/%s", ROOTDIR, filename); } #ifdef HAVE_CORESERVICES resolve_alias_path(path, path); #endif if (check_dropbox(htlc, path)) { snd_strerror(htlc, EPERM); return; } #ifdef CONFIG_HTXF_PREVIEW if (preview) { Image *img, *mimg; ImageInfo ii; ExceptionInfo ei; char previewpath[MAXPATHLEN]; static int magick_inited = 0; if (!magick_inited) { InitializeMagick("hxd"); magick_inited = 1; } #if MaxTextExtent < MAXPATHLEN if (strlen(path) >= sizeof(ii.filename)) { hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, 13, "path too long"); return; } #endif memset(&ii, 0, sizeof(ii)); memset(&ei, 0, sizeof(ei)); GetImageInfo(&ii); GetExceptionInfo(&ei); err = preview_path(previewpath, path, &sb); if (!err) { /* Preview file already exists */ strcpy(ii.filename, previewpath); mimg = ReadImage(&ii, &ei); } else { /* Create preview file */ strcpy(ii.filename, path); img = ReadImage(&ii, &ei); if (!img) goto text_preview; mimg = MinifyImage(img, &ei); DestroyImage(img); } if (!mimg) { hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, 18, "MinifyImage failed"); return; } if (err) { err = preview_path(previewpath, path, 0); if (err) { snd_strerror(htlc, err); DestroyImage(mimg); return; } strcpy(mimg->filename, previewpath); data_pos = 0; rsrc_pos = 0; WriteImage(&ii, mimg); DestroyImage(mimg); } else { DestroyImage(mimg); } strcpy(path, previewpath); } text_preview: #endif if (stat(path, &sb)) { snd_strerror(htlc, errno); return; } if (S_ISDIR(sb.st_mode)) { snd_strerror(htlc, EISDIR); return; } data_size = sb.st_size; size = (data_size - data_pos) + (preview ? 0 : 133); #if defined(CONFIG_HFS) if (hxd_cfg.operation.hfs) { rsrc_size = resource_len(path); size += preview ? 0 : (rsrc_size - rsrc_pos); if (!preview) size += ((rsrc_size - rsrc_pos) ? 16 : 0) + comment_len(path); } #endif ref = htxf_ref_new(htlc); ref = htonl(ref); siz = sizeof(struct SOCKADDR_IN); if (getsockname(htlc->fd, (struct sockaddr *)&lsaddr, &siz)) { hxd_log("rcv_file_get: getsockname: %s", strerror(errno)); snd_strerror(htlc, errno); return; } htxf = htxf_new(htlc, 0); htxf->type = HTXF_TYPE_FILE; htxf->data_size = data_size; htxf->rsrc_size = rsrc_size; htxf->data_pos = data_pos; htxf->rsrc_pos = rsrc_pos; htxf->total_size = size; htxf->ref = ref; htxf->limit_out_Bps = htlc->nr_puts > 0 ? (htlc->limit_uploader_out_Bps ? htlc->limit_uploader_out_Bps : htlc->limit_out_Bps) : htlc->limit_out_Bps; hxd_log("conf: %u!%u", htxf->limit_out_Bps, htlc->limit_out_Bps); htxf->preview = preview; htxf->sockaddr = htlc->sockaddr; htxf->listen_sockaddr = lsaddr; htxf->listen_sockaddr.SIN_PORT = htons(ntohs(htxf->listen_sockaddr.SIN_PORT) + 1); strcpy(htxf->path, path); htlc->nr_gets++; nr_gets++; #if defined(CONFIG_HTXF_QUEUE) if (htlc->access_extra.ignore_queue) htxf->queue_pos = queue_pos = 0; else htxf->queue_pos = queue_pos = insert_into_queue(htlc); #endif if (log_download) { inaddr2str(abuf, &htlc->sockaddr); hxd_log("%s@%s:%u - %s:%u:%u:%s - download %s:%08x", htlc->userid, abuf, ntohs(htlc->sockaddr.SIN_PORT), htlc->name, htlc->icon, htlc->uid, htlc->login, htxf->path, htxf->ref); #if defined(CONFIG_SQL) sql_download(htlc->name, abuf, htlc->login, path); #endif } size = htonl(size); #if defined(CONFIG_HTXF_QUEUE) queue_pos = htons(queue_pos); hlwrite(htlc, HTLS_HDR_TASK, 0, 3, HTLS_DATA_HTXF_REF, sizeof(ref), &ref, HTLS_DATA_HTXF_SIZE, sizeof(size), &size, HTLS_DATA_QUEUE_POSITION, sizeof(queue_pos), &queue_pos); #else hlwrite(htlc, HTLS_HDR_TASK, 0, 2, HTLS_DATA_HTXF_REF, sizeof(ref), &ref, HTLS_DATA_HTXF_SIZE, sizeof(size), &size); #endif } void rcv_file_put (struct htlc_conn *htlc) { u_int16_t fnlen = 0, resume = 0; char path[MAXPATHLEN], dir[MAXPATHLEN], filename[NAME_MAX]; char abuf[HOSTLEN+1], buf[128]; struct stat sb; int err, siz, len; u_int32_t ref, data_pos = 0, rsrc_pos = 0, totalsize = 0; u_int8_t rflt[74]; struct SOCKADDR_IN lsaddr; struct htxf_conn *htxf; u_int16_t i; dir[0] = 0; if (htlc->nr_puts >= htlc->put_limit) { len = snprintf(buf, sizeof(buf), "%u at a time", htlc->put_limit); hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, len, buf); return; } if (nr_puts >= hxd_cfg.limits.total_uploads) { len = snprintf(buf, sizeof(buf), "maximum number of total uploads reached (%u >= %d)", nr_gets, hxd_cfg.limits.total_uploads); hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, len, buf); return; } for (i = 0; i < HTXF_PUT_MAX; i++) if (!htlc->htxf_in[i]) break; if (i == HTXF_PUT_MAX) { snd_strerror(htlc, EAGAIN); return; } dh_start(htlc) switch (dh_type) { case HTLC_DATA_FILE_NAME: fnlen = dh_len >= NAME_MAX ? NAME_MAX - 1 : dh_len; read_filename(filename, dh_data, fnlen); break; case HTLC_DATA_DIR: if ((err = hldir_to_path(dh, ROOTDIR, dir, dir))) { snd_strerror(htlc, err); return; } break; case HTLC_DATA_FILE_PREVIEW: dh_getint(resume); break; case HTLC_DATA_HTXF_SIZE: dh_getint(totalsize); break; } dh_end() if (!htlc->access.upload_anywhere && (!dir[0] || (!strcasestr(dir, "UPLOAD") && !strcasestr(dir, "DROP BOX")))) { snd_strerror(htlc, EPERM); return; } if (!fnlen && !dir[0]) { /* No file name given */ hlwrite(htlc, HTLS_HDR_TASK, 1, 1, HTLS_DATA_TASKERROR, 6, "huh?!?"); return; } if (dir[0]) { if (fnlen) snprintf(path, sizeof(path), "%s/%s", dir, filename); else strcpy(path, dir); } else { snprintf(path, sizeof(path), "%s/%s", ROOTDIR, filename); } #ifdef HAVE_CORESERVICES resolve_alias_path(path, path); #endif if (!resume) { if (!stat(path, &sb)) { snd_strerror(htlc, EEXIST); return; } if (errno != ENOENT) { snd_strerror(htlc, errno); return; } } else { if (stat(path, &sb)) { snd_strerror(htlc, errno); return; } data_pos = sb.st_size; #if defined(CONFIG_HFS) if (hxd_cfg.operation.hfs) rsrc_pos = resource_len(path); #endif memcpy(rflt, "RFLT\0\1\ \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\ \0\0\0\2DATA\0\0\0\0\0\0\0\0\0\0\0\0MACR\0\0\0\0\0\0\0\0\0\0\0\0", 74); S32HTON(data_pos, &rflt[46]); S32HTON(rsrc_pos, &rflt[62]); } ref = htxf_ref_new(htlc); ref = htonl(ref); siz = sizeof(struct SOCKADDR_IN); if (getsockname(htlc->fd, (struct sockaddr *)&lsaddr, &siz)) { hxd_log("rcv_file_get: getsockname: %s", strerror(errno)); snd_strerror(htlc, errno); return; } htxf = htxf_new(htlc, 1); htxf->type = HTXF_TYPE_FILE; htxf->data_pos = data_pos; htxf->rsrc_pos = rsrc_pos; htxf->total_size = totalsize; htxf->ref = ref; htxf->sockaddr = htlc->sockaddr; htxf->listen_sockaddr = lsaddr; htxf->listen_sockaddr.SIN_PORT = htons(ntohs(htxf->listen_sockaddr.SIN_PORT) + 1); strcpy(htxf->path, path); htlc->nr_puts++; nr_puts++; if (log_upload) { inaddr2str(abuf, &htlc->sockaddr); hxd_log("%s@%s:%u - %s:%u:%u:%s - upload %s:%08x", htlc->userid, abuf, ntohs(htlc->sockaddr.SIN_PORT), htlc->name, htlc->icon, htlc->uid, htlc->login, htxf->path, htxf->ref); #if defined(CONFIG_SQL) sql_upload(htlc->name, abuf, htlc->login, path); #endif } if (!resume) hlwrite(htlc, HTLS_HDR_TASK, 0, 1, HTLS_DATA_HTXF_REF, sizeof(ref), &ref); else hlwrite(htlc, HTLS_HDR_TASK, 0, 2, HTLS_DATA_RFLT, 74, rflt, HTLS_DATA_HTXF_REF, sizeof(ref), &ref); }
static inline void push_state(int id, int parent_id, double cost, double new_utility, queue state_queue) { state_ptr new_state = create_state(id, parent_id, cost, new_utility); insert_into_queue(new_state, state_queue); }