void tst_release_device(const char *dev) { if (getenv("LTP_DEV")) return; /* * Loop device was created -> we need to deatch it. * * The file image is deleted in tst_rmdir(); */ detach_device(dev); device_acquired = 0; }
int main(int argc, char *argv[]) { // Pid Code,erlaubt nur eine Instanz int pid_file = open("/var/run/ctlmissile.pid", O_CREAT | O_RDWR, 0666); int rc = flock(pid_file, LOCK_EX | LOCK_NB); if(rc) { if(EWOULDBLOCK == errno) fprintf(stderr, "Another Instance is running!"); exit(0); } libusb_device **devlist; libusb_device *dev; ssize_t numdev, i; int ret = 0; do_init(); numdev = libusb_get_device_list(ctx, &devlist); for(ret = 1, i = 0; i < numdev; i++) { dev = devlist[i]; struct libusb_device_descriptor d; libusb_get_device_descriptor(dev, &d); int ret=0; if (d.idVendor == 0x0a81 && d.idProduct == 0x0701) { //fprintf(stderr, "Found Device:\n"); // Try to detach: ret = detach_device(dev); //fprintf(stderr, "%d:\n",ret); libusb_device_handle *handle; // get Handle: ret = libusb_open(dev, &handle); if(ret!=0){ fprintf(stderr, "Couldnt get Handle: %s\n",libusb_error_name(ret)); } // Claim Device: ret = libusb_claim_interface(handle,0); if(ret!=0){ fprintf(stderr, "Error with Claiming: %s\n",libusb_error_name(ret)); } int delay; sscanf (argv[2],"%d",&delay); // Try Command makeCommand(handle,argv[1],delay); // Try to reset //libusb_reset_device(handle); break; } } if (!dev) { fprintf(stderr, "Unable to find device.\n"); exit(EXIT_FAILURE); } return 0; }
main (int argc, char **argv) { char *params[10], *lodevice; char *prefix, *fname, *mountpoint; pid_t pid, cluster_size=0; int status,opt=0,q=0; long size=0; #ifdef PATH if (strlen(PATH) > MAX_PATH_LENGTH) { fprintf(stderr, "too long PATH\n"); exit(1); } prefix = malloc(sizeof(char)*strlen(PATH)+1); if (prefix == NULL) { perror(PNAME); exit(1); } strcpy (prefix,PATH); #else #error "must define PATH" #endif #ifdef MOUNTPOINT if (strlen(MOUNTPOINT) > MAX_PATH_LENGTH) { fprintf(stderr, "too long MOUNTPOINT\n"); exit(1); } mountpoint = malloc(sizeof(char)*strlen(MOUNTPOINT)+1); if (mountpoint == NULL) { perror(PNAME); exit(1); } strcpy (mountpoint,MOUNTPOINT); sanitize_path(mountpoint); #else #error "must define MOUNTPOINT" #endif if (argc < 2) { fprintf(stderr,"Usage: %s [create fstype size cluster_size name [clean | random] filename | attach fstype filename | detach]\n", PNAME); exit(1); } if (strcmp(argv[1], "create") == 0) { if (argc < 2) { fprintf(stderr,"Usage: %s create ntfs|FAT16 ...\n", PNAME); exit(1); } if (strcmp(argv[2], "ntfs") == 0) { if (argc != 8) { fprintf(stderr,"Usage: %s create fstype size cluster_size name [clean | random] filename\n", PNAME); exit(1); } /* freopen("/dev/null","w",stderr);*/ if (strlen(argv[3]) > 12) { fprintf(stderr,"Too long size parameter\n"); exit(1); } size = calculate_size(argv[3]); if (strlen(argv[7]) > MAX_PATH_LENGTH) { fprintf(stderr, "Too long parameter %s\n", argv[7]); exit(1); } fname = malloc(sizeof(char)*strlen(argv[7])+1); if (fname == NULL) { perror(PNAME); exit(1); } strcpy(fname, argv[7]); if ((atoi(argv[4]) < 1 || atoi (argv[4]) > 32) || atoi(argv[4]) & 1) { fprintf(stderr,"Cluster size must be a multiple of 2 and max 32\n"); exit(1); } cluster_size = atoi(argv[4])*512; /* Generally we could be more allowing here but use the same routine for reasons of pure laziness. Technically only white space, ?, *, " and ' should be really frowned upon here */ sanitize_path(argv[5]); if (strlen(argv[5]) > 20) { fprintf(stderr, "too long parameter %s", argv[5]); exit(1); } if (strcmp(argv[6], "random") == 0) { create_file(prefix, fname, size, C_RANDOM); } else { create_file(prefix, fname, size, C_ZERO); } create_ntfs_filesystem(prefix, fname, argv[5], cluster_size); exit(0); } if ((strcmp(argv[2], "FAT16") == 0) || (strcmp(argv[2],"FAT32") == 0) || (strcmp(argv[2], "FAT12") == 0) || (strcmp(argv[2], "GENERICFAT") == 0)) { if (argc != 9) { fprintf(stderr,"Usage: %s create FAT16|FAT12|FAT32 size cluster_size sector_size name [clean | random] filename\n", PNAME); exit(1); } /* freopen("/dev/null","w",stderr);*/ if (strlen(argv[3]) > 12) { fprintf(stderr,"Too long size parameter\n"); exit(1); } size = calculate_size(argv[3]); if (strlen(argv[8]) > MAX_PATH_LENGTH) { fprintf(stderr, "Too long parameter %s\n", argv[8]); exit(1); } fname = malloc(sizeof(char)*strlen(argv[7])+1); if (fname == NULL) { perror(PNAME); exit(1); } strcpy(fname, argv[8]); if ((atoi(argv[4]) < 1 || atoi (argv[4]) > 32) || atoi(argv[4]) & 1) { fprintf(stderr,"Cluster size must be a multiple of 2 and max 32\n"); exit(1); } cluster_size = atoi(argv[4])*512; if ((atoi(argv[5]) < 512 || atoi (argv[5]) > 32768) || (atoi(argv[5]) & (atoi(argv[5])-1))) { fprintf(stderr,"Sector size must be a multiple of 512, max 32768 and a power of two\n"); exit(1); } /* Generally we could be more allowing here but use the same routine for reasons of pure laziness. Technically only white space, ?, *, " and ' should be really frowned upon here */ sanitize_path(argv[6]); if (strlen(argv[6]) > 11) { fprintf(stderr, "too long parameter %s (max 11 chars)\n", argv[6]); exit(1); } if (strcmp(argv[7], "random") == 0) { create_file(prefix, fname, size, C_RANDOM); } else { create_file(prefix, fname, size, C_ZERO); } create_fat_filesystem(prefix, argv[2], fname, argv[6], atoi(argv[5]),argv[4]); exit(0); } else { fprintf(stderr, "Unknown file system type %s\n", argv[2]); exit(1); } } /* Attach */ if (strcmp(argv[1], "attach") == 0) { if (argc != 4) { fprintf(stderr,"Usage: %s attach fstype filename\n", PNAME); exit(1); } if (strlen(argv[3]) > MAX_PATH_LENGTH) { fprintf(stderr, "Too long parameter %s\n", argv[3]); exit(1); } fname = malloc(sizeof(char)*strlen(argv[3])+1); if (fname == NULL) { perror(PNAME); exit(1); } strcpy(fname, argv[3]); lodevice = attach_file(prefix,fname); if (lodevice) { if (strcmp(argv[2],"ntfs") == 0) { q = mount_ntfs_filesystem(lodevice); if (q != 0) detach_device(lodevice); exit(q); } if (strncmp(argv[2], "FAT",3 ) == 0) { q = mount_fat_filesystem(lodevice); if (q != 0) detach_device(lodevice); exit(q); } else { fprintf(stderr, "unknown file system type %s\n", argv[2]); detach_device(lodevice); exit(1); } } else fprintf (stderr, "Cannot find loopback device\n"); exit(0); } /* detach */ if (strcmp(argv[1], "detach") == 0) { freopen("/dev/null","w",stderr); exit(detach_image()); } fprintf(stderr,"Usage: %s [create fstype size cluster_size name [clean | random] filename | attach fstype filename | detach]\n", PNAME); exit(1); }