static int lookup_device(struct thread_data *td, char *path, unsigned int maj, unsigned int min) { struct dirent *dir; struct stat st; int found = 0; DIR *D; D = opendir(path); if (!D) return 0; while ((dir = readdir(D)) != NULL) { char full_path[256]; if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) continue; sprintf(full_path, "%s%s%s", path, FIO_OS_PATH_SEPARATOR, dir->d_name); if (lstat(full_path, &st) == -1) { perror("lstat"); break; } if (S_ISDIR(st.st_mode)) { found = lookup_device(td, full_path, maj, min); if (found) { strcpy(path, full_path); break; } } if (!S_ISBLK(st.st_mode)) continue; /* * If replay_redirect is set then always return this device * upon lookup which overrides the device lookup based on * major minor in the actual blktrace */ if (td->o.replay_redirect) { dprint(FD_BLKTRACE, "device lookup: %d/%d\n overridden" " with: %s\n", maj, min, td->o.replay_redirect); strcpy(path, td->o.replay_redirect); found = 1; break; } if (maj == major(st.st_rdev) && min == minor(st.st_rdev)) { dprint(FD_BLKTRACE, "device lookup: %d/%d\n", maj, min); strcpy(path, full_path); found = 1; break; } } closedir(D); return found; }
static int describe_callback(rtspsvr_cbp_t *cbp) { realplay_info_t info; rmd_session_t sess; driver_t *driver = NULL; device_t *device = NULL; channel_t *channel = NULL; #ifndef RMD_DYNAMIC_DEVICES if (lookup_device(cbp->url, &sess, &driver, &device, &channel) != 0) return -1; #else device_t tmp_device; if (lookup_device_dynamic(cbp->url, &sess, &tmp_device, &driver, &device, &channel) != 0) return -1; #endif snprintf(info.ip, sizeof(info.ip), device->ip); info.port = device->port; snprintf(info.user, sizeof(info.user), device->user); snprintf(info.pwd, sizeof(info.pwd), device->pwd); info.channel_id = sess.channel_id; info.hdr_cb = NULL; info.data_cb = NULL; return driver->rmd_di_get_describe(&info, &cbp->sdp); }
int add_wii_device(struct wiimoteglue_state *state, struct udev_device* udev) { char* uniq = udev_device_get_property_value(udev, "HID_UNIQ"); struct wii_device *dev = lookup_device(&state->dev_list,uniq); if (dev == NULL) { struct wii_device_list* node = new_wii_device(state,uniq); node->dev->udev = udev; dev = node->dev; } else { udev_device_unref(dev->udev); dev->udev = udev; printf("\tid: %s\n\taddress %s\n",dev->id, dev->bluetooth_addr); } open_wii_device(state, dev); remove_device_from_slot(dev); if (dev->slot == NULL) { auto_assign_slot(state, dev); } if (dev->slot == NULL) { close_wii_device(state,dev); } return 0; }
static void trace_add_file(struct thread_data *td, __u32 device) { static unsigned int last_maj, last_min; unsigned int maj = FMAJOR(device); unsigned int min = FMINOR(device); struct fio_file *f; char dev[256]; unsigned int i; if (last_maj == maj && last_min == min) return; last_maj = maj; last_min = min; /* * check for this file in our list */ for_each_file(td, f, i) if (f->major == maj && f->minor == min) return; strcpy(dev, "/dev"); if (lookup_device(td, dev, maj, min)) { int fileno; dprint(FD_BLKTRACE, "add devices %s\n", dev); fileno = add_file_exclusive(td, dev); trace_add_open_event(td, fileno); } }
/**ltl * 功能: 将目标设备加入到列表中 * 参数: t ->映射表 * ti ->映射目标 * path ->映射目标<major:minor> * start-> 映射目标相对低层设备的起始偏移量(类似磁盘分区的起始地址) * len -> 此目标设备在dm设备的长度 * mode -> rw * result-> 底层设备对象 * 返回值: * 说明: */ static int __table_get_device(struct dm_table *t, struct dm_target *ti, const char *path, sector_t start, sector_t len, int mode, struct dm_dev **result) { int r; dev_t dev; struct dm_dev *dd; unsigned int major, minor; BUG_ON(!t); /* 获取主设备号和次设备号 */ if (sscanf(path, "%u:%u", &major, &minor) == 2) { /* Extract the major/minor numbers */ dev = MKDEV(major, minor); if (MAJOR(dev) != major || MINOR(dev) != minor) return -EOVERFLOW; } else { /* convert the path to a device */ if ((r = lookup_device(path, &dev))) /* 根据设备路径/dev/sdb获取<major:minor> */ return r; } /* 在列表中查找映射目标 */ dd = find_device(&t->devices, dev); if (!dd) { dd = kmalloc(sizeof(*dd), GFP_KERNEL); if (!dd) return -ENOMEM; dd->mode = mode; dd->bdev = NULL; /* 打开设备 */ if ((r = open_dev(dd, dev, t->md))) { kfree(dd); return r; } format_dev_t(dd->name, dev); /* 主设备号次设备号 */ atomic_set(&dd->count, 0); /* 将目标设备插入到映射表中 */ list_add(&dd->list, &t->devices); } else if (dd->mode != (mode | dd->mode)) { r = upgrade_mode(dd, mode, t->md); if (r) return r; } atomic_inc(&dd->count); /* 检查区域是否超过设备 */ if (!check_device_area(dd, start, len)) { DMWARN("device %s too small for target", path); dm_put_device(ti, dd); return -EINVAL; } *result = dd; return 0; }
/* * Add a device to the list, or just increment the usage count if * it's already present. */ static int __table_get_device(struct dm_table *t, struct dm_target *ti, const char *path, sector_t start, sector_t len, int mode, struct dm_dev **result) { int r; dev_t dev; struct dm_dev *dd; unsigned int major, minor; if (!t) BUG(); if (sscanf(path, "%u:%u", &major, &minor) == 2) { /* Extract the major/minor numbers */ dev = MKDEV(major, minor); if (MAJOR(dev) != major || MINOR(dev) != minor) return -EOVERFLOW; } else { /* convert the path to a device */ if ((r = lookup_device(path, &dev))) return r; } dd = find_device(&t->devices, dev); if (!dd) { dd = kmalloc(sizeof(*dd), GFP_KERNEL); if (!dd) return -ENOMEM; dd->mode = mode; dd->bdev = NULL; if ((r = open_dev(dd, dev))) { kfree(dd); return r; } atomic_set(&dd->count, 0); list_add(&dd->list, &t->devices); } else if (dd->mode != (mode | dd->mode)) { r = upgrade_mode(dd, mode); if (r) return r; } atomic_inc(&dd->count); if (!check_device_area(dd, start, len)) { DMWARN("device %s too small for target", path); dm_put_device(ti, dd); return -EINVAL; } *result = dd; return 0; }
int main(int argc, char **argv) { int first = 0x03, last = 0x77; int flags = 0; int version = 0; while (1 + flags < argc && argv[1 + flags][0] == '-') { switch (argv[1 + flags][1]) { case 'V': version = 1; break; case 'a': first = 0x00; last = 0x7F; break; default: fprintf(stderr, "Warning: Unsupported flag \"-%c\"!\n", argv[1 + flags][1]); //help(); // TODO exit(1); } flags++; } if (version) { fprintf(stderr, "i2cdetect version %s\n", VERSION); exit(0); } if (bcm2835_init() != 1) { fprintf(stderr, "bcm2835_init() failed\n"); exit(1); } bcm2835_i2c_begin(); bcm2835_i2c_setClockDivider(BCM2835_I2C_CLOCK_DIVIDER_2500); // 100kHz int address; for (address = 0x00; address <= 0x7F; address++) { if (address < first || address > last) { continue; } bcm2835_i2c_setSlaveAddress(address); if (bcm2835_i2c_write(NULL, 0) == 0) { printf("0x%.2X : 0x%.2X : %s\n", address, address << 1, lookup_device(address)); } } bcm2835_i2c_end(); bcm2835_close(); return 0; }
static int lookup_device_dynamic(const char *url, rmd_session_t *sess, device_t *tmp_device, driver_t **driver, device_t **device, channel_t **channel) { if (parse_url_dynamic(url, sess, tmp_device, device) == 0) { *driver = rmd_devmgr_get_driver(sess->driver_type); if (*driver == NULL) return -1; return 0; } return lookup_device(url, sess, driver, device, channel); }
int main(int argc, char* argv[]) { char* filesystem = NULL; char* device_name = NULL; char** uid_strs = NULL; int* uids = NULL; int num_uids = 0; int ii = 0; if (argc < 3) { printf("Usage: report_quota [filesystem] [uid]+\n"); printf("Reports quota information for the supplied uids on the given filesystem\n"); printf("Format is: <uid> <bytes used> <soft> <hard> <grace> <inodes used> <soft> <hard> <grace>\n"); exit(1); } filesystem = argv[1]; num_uids = argc - 2; uid_strs = argv + 2; if (lookup_device(filesystem, &device_name) < 0) { printf("Couldn't find device for %s\n", argv[1]); exit(1); } uids = malloc(sizeof(*uids) * num_uids); assert(NULL != uids); memset(uids, 0, sizeof(*uids) * num_uids); for (ii = 0; ii < num_uids; ii++) { uids[ii] = atoi(uid_strs[ii]); } for (ii = 0; ii < num_uids; ii++) { if (print_quota_usage(device_name, uids[ii]) < 0) { exit(1); } } /* Pedantry! */ free(device_name); free(uids); return 0; }
static int trace_add_file(struct thread_data *td, __u32 device) { static unsigned int last_maj, last_min, last_fileno; unsigned int maj = FMAJOR(device); unsigned int min = FMINOR(device); struct fio_file *f; char dev[256]; unsigned int i; if (last_maj == maj && last_min == min) return last_fileno; last_maj = maj; last_min = min; /* * check for this file in our list */ for_each_file(td, f, i) if (f->major == maj && f->minor == min) { last_fileno = f->fileno; return last_fileno; } strcpy(dev, "/dev"); if (lookup_device(td, dev, maj, min)) { int fileno; dprint(FD_BLKTRACE, "add devices %s\n", dev); fileno = add_file_exclusive(td, dev); td->o.open_files++; td->files[fileno]->major = maj; td->files[fileno]->minor = min; trace_add_open_close_event(td, fileno, FIO_LOG_OPEN_FILE); last_fileno = fileno; } return last_fileno; }
static int setup_callback(rtspsvr_cbp_t *cbp) { rmd_session_t sess; device_t *new_device = NULL; if (rmd_sessmgr_is_valid_sid(cbp->sid) != 0) return -1; memset(&sess, 0, sizeof(sess)); #ifndef RMD_DYNAMIC_DEVICES if (parse_url(cbp->url, &sess) != 0) return -1; #else device_t tmp_device; driver_t *driver = NULL; device_t *device = NULL; channel_t *channel = NULL; if (parse_url_dynamic(cbp->url, &sess, &tmp_device, &new_device) != 0) { if (lookup_device(cbp->url, &sess, &driver, &device, &channel) != 0) return -1; new_device = device; } #endif if (rtsp_istcp(cbp->trans)) sess.trans_method = TRANS_VIA_TCP; else sess.trans_method = TRANS_VIA_UDP; snprintf(sess.sid, sizeof(sess.sid), cbp->sid); return rmd_sessmgr_add(&sess, new_device, cbp->client_fd, cbp->cliip, cbp->cli_rtp_port, cbp->cli_rtcp_port, &cbp->svr_rtp_port); }
int parse_mounts(const glightui *gui,const char *fn){ char *mnt,*dev,*ops,*fs; off_t len,idx; char *map; int fd; if((map = map_virt_file(fn,&fd,&len)) == MAP_FAILED){ return -1; } idx = 0; dev = mnt = fs = ops = NULL; while(idx < len){ char buf[PATH_MAX + 1]; struct statvfs vfs; struct stat st; device *d; char *rp; int r; free(dev); free(mnt); free(fs); free(ops); if((r = parse_mount(map + idx,len - idx,&dev,&mnt,&fs,&ops)) < 0){ goto err; } idx += r; if(statvfs(mnt,&vfs)){ int skip = 0; // We might have mounted a new target atop or above an // already existing one, in which case we'll need // possibly recreate the directory structure on the // newly-mounted filesystem. if(growlight_target){ if(strncmp(mnt,growlight_target,strlen(growlight_target)) == 0){ if(make_parent_directories(mnt) == 0){ skip = 1; } // FIXME else remount? otherwise writes // go to new filesystem rather than old...? } } if(!skip){ diag("Couldn't stat fs %s (%s?)\n",mnt,strerror(errno)); r = -1; continue; } } if(*dev != '/'){ // have to get zfs's etc if(fstype_virt_p(fs)){ continue; } if((d = lookup_device(dev)) == NULL){ verbf("virtfs %s at %s\n",fs,mnt); continue; } }else{ rp = dev; if(lstat(rp,&st) == 0){ if(S_ISLNK(st.st_mode)){ if((r = readlink(dev,buf,sizeof(buf))) < 0){ diag("Couldn't deref %s (%s?)\n",dev,strerror(errno)); continue; } if((size_t)r >= sizeof(buf)){ diag("Name too long for %s (%d?)\n",dev,r); continue; } buf[r] = '\0'; rp = buf; } } if((d = lookup_device(rp)) == NULL){ continue; } } free(dev); dev = NULL; if(d->mnttype && strcmp(d->mnttype,fs)){ diag("Already had mounttype for %s: %s (got %s)\n", d->name,d->mnttype,fs); free(d->mnttype); d->mnttype = NULL; free_stringlist(&d->mntops); free_stringlist(&d->mnt); d->mnttype = fs; }else{ free(fs); } fs = NULL; if(add_string(&d->mnt,mnt)){ goto err; } if(add_string(&d->mntops,ops)){ goto err; } d->mntsize = (uintmax_t)vfs.f_bsize * vfs.f_blocks; if(d->layout == LAYOUT_PARTITION){ d = d->partdev.parent; } d->uistate = gui->block_event(d,d->uistate); if(growlight_target){ if(strcmp(mnt,growlight_target) == 0){ mount_target(); } } } free(mnt); free(fs); free(ops); mnt = fs = ops = NULL; munmap_virt(map,len); close(fd); return 0; err: free(dev); free(mnt); free(fs); free(ops); munmap_virt(map,len); close(fd); return -1; }