/* * Given a file containing a list of pathnames this function * will search for those entries in the alternate root and * delete all matching pathnames from the alternate root that * are symbolic links. * This process is required because of the way the LiveCD env * is constructed. Some of the entries in the microroot are * symbolic links to files mounted off a compressed lofi file. * This is done to drastically reduce space usage by the microroot. */ static int do_clobber_files(char *flist_file) { FILE *fh; char line[PATH_MAX]; struct stat mst; DBG_MSG2("File list for clobber: %s", flist_file); fh = fopen(flist_file, "r"); if (fh == NULL) { Perror(flist_file); return (1); } if (chdir(mntpt) != 0) { Perror("Cannot change dir to alt root"); (void) fclose(fh); return (1); } while (fgets(line, PATH_MAX, fh) != NULL) { CHECK_ABORT2; line[strlen(line) - 1] = '\0'; if (lstat(line, &mst) == 0) { if (S_ISLNK(mst.st_mode)) { DBG_MSG2("Unlink: %s", line); (void) unlink(line); } } } (void) fclose(fh); return (0); }
/* ARGSUSED */ static int add_files(const char *fpath, const struct stat *finfo, int ftw_flag, struct FTW *ftw_prop) { int rstatus; CHECK_ABORT1; if (fpath[0] == '.' && fpath[1] == NULL) { return (0); } if (ftw_flag != FTW_DNR) { if (mre != NULL) { rstatus = regexec(mre, fpath, (size_t)0, NULL, 0); if ((negate && rstatus == 0) || (!negate && rstatus != 0)) { DBG_MSG2("Non pattern match. " "Skipped: %s", fpath); return (0); } } if (finfo->st_size > 0) { (void) fprintf(listfile, "%s\n", fpath); } else { /* * Handle zero-length regular files differently since * they will be hardlinked on hsfs. */ if (S_ISREG(finfo->st_mode)) { (void) fprintf(zerolength, "%d,%d,%d,%s\n", finfo->st_mode & S_IAMB, finfo->st_uid, finfo->st_gid, fpath); } } DBG_MSG2("Added pathname: %s", fpath); nfiles++; percent = (int)(nfiles / MAX_NUMFILES * total_find_percent); if (percent - opercent >= 1) { (*progress)(percent); opercent = percent; } } return (0); }
static void expand_symbols(char *fpath, char *buf, size_t buflen) { char *pos; ptrdiff_t diff1, diff2; int len; buf[0] = '\0'; len = strlen(fpath); pos = strstr(fpath, "%M"); DBG_MSG2("Expanding: %s", fpath); if (pos != NULL) { diff1 = pos - fpath; if (diff1 > 0 && diff1 < buflen) { (void) strncpy(buf, fpath, diff1); } (void) strlcat(buf, mntpt, buflen); diff2 = len - (diff1 + 2); len = strlen(buf); if (diff2 > 0 && diff2 + len < buflen) { (void) strncat(buf, pos+2, diff2); } } else { (void) strlcpy(buf, fpath, buflen); } }
int hub_set_port_feature( struct rt_urb *p_urb, __u16 port_nr, __u8 feature) { int ret = 0; if(!p_urb){ return -ENODEV; } DBG_MSG2(p_urb->p_hcd,p_urb->p_usbdev,"%02d : Set Feature 0x%02x\n", port_nr, feature); ret = nrt_intern_usb_control_msg( p_urb, 0x00, USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, USB_REQ_SET_FEATURE, feature, port_nr, // port 0x0000, // fix NULL); if(ret < 0){ ERR_MSG2(p_urb->p_hcd,p_urb->p_usbdev," %s - Port %02d : Set Feature 0x%02x FAILED\n",__FUNCTION__, port_nr, feature); return -1; } return 0; }
int http_handle_get( char *f, char *out_buf ){ char *token[80]; char *req_token[80]; char i=0; token[0] = strtok(f, "\n"); while(token[i]!= NULL) { i++; token[i] = strtok(NULL, "\n"); } // now split first line (containing GET parameters) i=0; req_token[0] = strtok(token[0], " "); while(req_token[i]!= NULL) { i++; req_token[i] = strtok(NULL, " "); } sprintf(out_buf, "%s", strtok(req_token[1],"/")); if( strstr( out_buf, "favicon.ico" ) ){ DBG_MSG1("http, ignoring favicon request\n" ); return 0; } DBG_MSG2("http GET parameter %s\n", out_buf ); return 1; }
int hub_get_portstat( struct rt_urb *p_urb, __u16 port_nr, portstat_t *p_stat ) { int ret; DBG_MSG2(p_urb->p_hcd,p_urb->p_usbdev,"%02d : Get Port-Status\n",port_nr); ret = nrt_intern_usb_control_msg( p_urb, 0x00, USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER, USB_REQ_GET_STATUS, 0x0000, // fix Value port_nr, // port 0x0004, // fix length p_stat); if(ret < 0x0004){ DBG_MSG2(p_urb->p_hcd,p_urb->p_usbdev," %s - Port %02d : Get Port-Status FAILED\n",__FUNCTION__, port_nr); return -1; } return 0; }
int hub_clear_port_feature( struct rt_urb *p_urb, __u16 port_nr, __u8 feature) { int ret; DBG_MSG2(p_urb->p_hcd,p_urb->p_usbdev,"%02d : Clear Feature 0x%02x\n",port_nr, feature); ret = nrt_intern_usb_control_msg( p_urb, 0x00, USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER, USB_REQ_CLEAR_FEATURE, feature, port_nr, 0x0000, NULL); if(ret < 0){ DBG_MSG2(p_urb->p_hcd,p_urb->p_usbdev," %s - Port %02d : Clear Feature 0x%02x FAILED\n",__FUNCTION__,port_nr, feature); return -1; } return 0; }
int usb_init_hub( struct rt_urb *p_urb ) { int i; hub_descriptor_t *p_hub_desc = NULL; if(!p_urb || ! p_urb->p_usbdev){ return -1; } p_hub_desc = kmalloc( sizeof(hub_descriptor_t),GFP_KERNEL); if(!p_hub_desc){ return -ENOMEM; } alloc_bytes += sizeof(hub_descriptor_t); DBG("RT-USBCORE: %d Byte allocated (%s %d Byte)\n", alloc_bytes, "+", sizeof(hub_descriptor_t) ); memset( p_hub_desc,0,sizeof( hub_descriptor_t ) ); p_urb->p_usbdev->p_hub_desc = p_hub_desc; DBG_MSG2(p_urb->p_hcd,p_urb->p_usbdev," Hub-Descriptor saved @ 0x%p\n",p_hub_desc); if( hub_get_descriptor( p_urb, p_hub_desc ) ){ kfree(p_hub_desc); alloc_bytes -= sizeof(hub_descriptor_t); DBG("RT-USBCORE: %d Byte allocated (%s %d Byte)\n", alloc_bytes, "-", sizeof(hub_descriptor_t) ); return -1; } dump_hub_descriptor( p_urb->p_usbdev ); for(i=1; i<= p_hub_desc->bNbrPorts; i++) { if( hub_set_port_feature( p_urb, i, PORT_POWER) < 0 ){ continue; } } /* Angabe in 2ms -Intervallen */ udelay( p_hub_desc->bPwrOn2PwrGood * 2000); return(0); }
/* * Main function for doing the copying of bits */ int TM_perform_transfer(nvlist_t *targs, void(*prog)(int)) { char *logfile = NULL, *buf = NULL, *cprefix; char *buf1 = NULL, *layout = NULL, *dbg; FILE *cpipe = NULL, *kbd_file; float ipercent, rem_percent, cpfiles; float calc_factor; int kbd = -1, kbd_layout; int rv = 0, i; struct file_list flist, *cflist; clock_t tm; struct stat st; char zerolist[PATH_MAX]; if (pthread_mutex_lock(&tran_mutex) != 0) { Perror("Unable to acquire Transfer lock "); return (1); } if (nvlist_lookup_string(targs, "mountpoint", &mntpt) != 0) { Perror("Alternate root mountpoint not provided. Bailing. "); return (1); } if (prog == NULL) { progress = log_progress; } else { progress = prog; } logfile = malloc(PATH_MAX); if (logfile == NULL) { Perror("Malloc failed "); return (1); } (void) snprintf(logfile, PATH_MAX, "%s/%s", mntpt, TM_LOGFILE_NAME); lof = fopen(logfile, "w+"); if (lof == NULL) { Perror("Unable to open logfile "); goto error_done; } buf = malloc(BUF_SIZE); if (buf == NULL) { Perror("Malloc failed "); goto error_done; } buf1 = malloc(BUF_SIZE); if (buf1 == NULL) { Perror("Malloc failed "); goto error_done; } dbg = getenv("TM_DEBUG"); if (dbg != NULL && strcmp(dbg, "1") == 0) { TM_enable_debug(); } /* * Set TMPDIR to avoid cpio depleting ramdisk space */ if (putenv(tmpenv) != 0) { Perror(tmpenv); goto error_done; } /* * Zero length file list. */ (void) strlcpy(zerolist, mntpt, PATH_MAX); (void) strlcat(zerolist, "/flist.0length", PATH_MAX); if ((zerolength = fopen(zerolist, "w+")) == NULL) { Perror(zerolist); goto error_done; } tm = time(NULL); (void) strftime(buf, PATH_MAX, (char *)0, localtime(&tm)); INFO_MSG2("-- Starting transfer process, %s --", buf); (void) chdir("/"); CHECK_ABORT; (*progress)(0); percent = 0; opercent = 0; total_find_percent = (NUM_PREFIXES - 1) * FIND_PERCENT; /* * Get the optimized libc overlay out of the way. */ if (umount("/lib/libc.so.1") != 0) { if (errno != EINVAL) { Perror("Can't unmount /lib/libc.so.1 "); goto error_done; } } CHECK_ABORT; INFO_MSG1("Building file lists for cpio"); /* * Do a file tree walk of all the mountpoints provided and * build up pathname lists. Pathname lists of all mountpoints * under the same prefix are aggregated in the same file to * reduce the number of cpio invocations. * * This loop builds a linked list where each entry points to * a file containing a pathname list and mentions other info * like the mountpoint from which to copy etc. */ cprefix = ""; flist.next = NULL; cflist = &flist; for (i = 0; cpio_prefixes[i].chdir_prefix != NULL; i++) { char *patt; regex_t re; CHECK_ABORT; DBG_MSG3("Cpio dir: %s, Chdir to: %s", cpio_prefixes[i].cpio_dir, cpio_prefixes[i].chdir_prefix); patt = cpio_prefixes[i].match_pattern; if (strcmp(cprefix, cpio_prefixes[i].chdir_prefix) != 0 || patt != NULL || cpio_prefixes[i].clobber_files == 1 || cpio_prefixes[i].cpio_args != NULL) { cprefix = cpio_prefixes[i].chdir_prefix; cflist->next = (struct file_list *) malloc(sizeof (struct file_list)); cflist = cflist->next; cflist->next = NULL; (void) snprintf(cflist->name, PATH_MAX, "%s/flist%d", mntpt, i); DBG_MSG2(" File list tempfile: %s", cflist->name); cflist->handle = fopen(cflist->name, "w+"); if (cflist->handle == NULL) { Perror("Unable to open file list "); goto error_done; } cflist->chdir_prefix = cpio_prefixes[i].chdir_prefix; if (patt != NULL) { DBG_MSG2(" Compiling regexp: %s", patt); if (patt[0] == '!') { negate = 1; patt++; } else { negate = 0; } if (regcomp(&re, patt, REG_EXTENDED|REG_NOSUB) != 0) { Perror("Regexp error "); goto error_done; } mre = &re; } else { mre = NULL; } listfile = cflist->handle; cflist->clobber_files = cpio_prefixes[i].clobber_files; if (cpio_prefixes[i].cpio_args != NULL) { cflist->cpio_args = cpio_prefixes[i].cpio_args; } else { cflist->cpio_args = DEFAULT_CPIO_ARGS; } } INFO_MSG3("Scanning %s/%s", cflist->chdir_prefix, cpio_prefixes[i].cpio_dir); (void) chdir(cflist->chdir_prefix); if (nftw(cpio_prefixes[i].cpio_dir, add_files, 10, FTW_MOUNT|FTW_PHYS) < 0) { Perror("Nftw failed "); goto error_done; } (void) fflush(cflist->handle); } (void) fflush(zerolength); /* * Now process each entry in the list. cpio is executed with the * -V option where it prints a dot for each pathname processed. * Since we already know the number of files we can show accurate * percentage completion. */ INFO_MSG1("Beginning cpio actions ..."); rem_percent = 95 - percent; ipercent = percent; cflist = flist.next; cpfiles = 0; opercent = 0; percent = 0; calc_factor = rem_percent / nfiles; while (cflist != NULL) { (void) fclose(cflist->handle); cflist->handle = NULL; CHECK_ABORT; if (cflist->clobber_files) { if (do_clobber_files(cflist->name) != 0) { goto error_done; } } (void) chdir(cflist->chdir_prefix); (void) snprintf(buf, PATH_MAX, "%s -%sV %s < %s", CPIO, cflist->cpio_args, mntpt, cflist->name); DBG_MSG3("Executing: %s, CWD: %s", buf, cflist->chdir_prefix); cpipe = popen(buf, "r"); if (cpipe == NULL) { Perror("Unable to cpio files "); goto error_done; } while (!feof(cpipe)) { int ch = fgetc(cpipe); if (ch == '.') { cpfiles++; percent = (int)(cpfiles * calc_factor + ipercent); if (percent - opercent >= 1) { if (progress != NULL) { (*progress)(percent); } opercent = percent; } } CHECK_ABORT; } if (ferror(cpipe)) { Perror(CPIO); goto error_done; } (void) fclose(cpipe); cpipe = NULL; (void) unlink(cflist->name); cflist->name[0] = '\0'; cflist = cflist->next; } (*progress)(percent); cpipe = NULL; /* * Process zero-length files if any. */ INFO_MSG1("Creating zero-length files"); rewind(zerolength); while (fgets(buf, BUF_SIZE, zerolength) != NULL) { int fd; mode_t mod; uid_t st_uid, st_gid; char *token, *lasts; /* Get the newline out of the way */ buf[strlen(buf) - 1] = '\0'; /* Parse out ownership and perms */ GET_TOKEN(token, lasts, buf, ","); mod = atoi(token); GET_TOKEN(token, lasts, NULL, ","); st_uid = atoi(token); GET_TOKEN(token, lasts, NULL, ","); st_gid = atoi(token); GET_TOKEN(token, lasts, NULL, ","); (void) snprintf(buf1, PATH_MAX, "%s/%s", mntpt, token); fd = open(buf1, O_WRONLY | O_CREAT | O_TRUNC, mod); if (fd != -1) { (void) fchown(fd, st_uid, st_gid); (void) close(fd); DBG_MSG2("Created file %s", buf1); } else { INFO_MSG1("Unable to create file:"); Perror(buf1); } } (*progress)(97); CHECK_ABORT; INFO_MSG1("Extracting archive"); (void) chdir(mntpt); (void) snprintf(buf, PATH_MAX, "%s e -so %s | %s -idum", SZIP, ARCHIVE, CPIO); DBG_MSG3("Executing: %s, CWD: %s", buf, mntpt); if (system(buf) != 0) { Perror("Extracting archive failed "); goto error_done; } (*progress)(98); CHECK_ABORT; /* * Check for the presence of skeleton.cpio before extracting it. * This file may not be present in a Distro Constructor image. */ if (lstat(SKELETON, &st) == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) { INFO_MSG1("Extracting skeleton archive"); (void) snprintf(buf, PATH_MAX, "%s -imu < %s", CPIO, SKELETON, mntpt); DBG_MSG3("Executing: %s, CWD: %s", buf, mntpt); if (system(buf) != 0) { Perror("Skeleton cpio failed "); goto error_done; } } (*progress)(99); CHECK_ABORT; INFO_MSG1("Performing file operations"); for (i = 0; i < NUM_FILEOPS_LIST; i++) { int rv; CHECK_ABORT; expand_symbols(fileops_list[i].src, buf, PATH_MAX); switch (fileops_list[i].op) { int op; case FILE_OP_UNLINK: DBG_MSG2("Unlink: %s", buf); (void) unlink(buf); rv = 0; /* unlink errors are non-fatal */ break; case FILE_OP_RMDIR: DBG_MSG2("Rmdir: %s", buf); (void) rmdir(buf); rv = 0; /* Ignore rmdir errors for now */ break; case FILE_OP_MKDIR: DBG_MSG2("Mkdir: %s", buf); rv = 0; if (lstat(buf, &st) == 0) { op = 0; if ((st.st_mode & S_IFMT) != S_IFDIR) { rv = unlink(buf); op = 1; } if (rv == 0 && op) { rv = mkdir(buf, fileops_list[i].perms); } } else { rv = mkdir(buf, fileops_list[i].perms); } break; case FILE_OP_COPY: expand_symbols(fileops_list[i].dst, buf1, PATH_MAX); rv = copyfile(buf, buf1); break; case FILE_OP_CHMOD: expand_symbols(fileops_list[i].dst, buf1, PATH_MAX); rv = chmod(buf, fileops_list[i].perms); break; default: Perror("Unsupported file operation "); rv = 1; break; } if (rv != 0) { Perror("File ops error "); Perror(buf); goto error_done; } } CHECK_ABORT; INFO_MSG1("Fetching and updating keyboard layout"); (void) chdir(mntpt); DBG_MSG2("Opening keyboard device: %s", KBD_DEVICE); kbd = open(KBD_DEVICE, O_RDWR); if (kbd < 0) { Perror("Error opening keyboard"); goto error_done; } if (ioctl(kbd, KIOCLAYOUT, &kbd_layout)) { Perror("ioctl keyboard layout"); goto error_done; } CHECK_ABORT; if ((layout = get_layout_name(kbd_layout)) == NULL) { goto error_done; } kbd_file = fopen(KBD_DEFAULTS_FILE, "a+"); if (kbd_file == NULL) { Perror("Unable to open kbd defaults file "); goto error_done; } (void) fprintf(kbd_file, "LAYOUT=%s\n", layout); (void) fclose(kbd_file); DBG_MSG3("Updated keyboard defaults file: %s/%s", mntpt, KBD_DEFAULTS_FILE); INFO_MSG2("Detected %s keyboard layout", layout); tm = time(NULL); (void) strftime(buf, PATH_MAX, (char *)0, localtime(&tm)); INFO_MSG2("-- Completed transfer process, %s --", buf); (*progress)(100); goto done; error_done: rv = 1; done: if (lof != NULL) (void) fclose(lof); if (cpipe != NULL) (void) fclose(cpipe); free_flist(flist.next); if (logfile != NULL) free(logfile); if (kbd > 0) (void) close(kbd); if (buf != NULL) free(buf); if (buf1 != NULL) free(buf1); if (layout != NULL) free(layout); if (zerolength != NULL) { (void) fclose(zerolength); (void) unlink(zerolist); } do_abort = 0; (void) pthread_mutex_unlock(&tran_mutex); return (rv); }