static void proto_handle_mkdir(conn_t* cn, const char* line) { // line is: permission mtime ctime atime directoryname const char* delim = " "; char* saveptr = NULL; char* token; char* ptr = (char*)line; int c = 0; mode_t mode = 0; time_t mtime = 0; time_t ctime = 0; time_t atime = 0; char* name = NULL; while((token = strtok_r(ptr, delim, &saveptr))) { switch(c) { case 0: mode = str2mode(token); break; case 1: atime = atol(token); break; case 2: ctime = atol(token); break; case 3: mtime = atol(token); break; case 4: name = token; break; default: conn_printf(cn, "ERROR Protocol violation (%d)\n", __LINE__); conn_abort(cn); return; break; } c++; ptr = NULL; } if(c != 5) { conn_printf(cn, "ERROR Protocol violation (%d)\n", __LINE__); conn_abort(cn); return ; } if(mkdir(name,mode) == -1 && errno != EEXIST) { conn_perror(cn, "WARNING mkdir()"); conn_printf(cn, "ERROR Can't create directory: %s\n", name); conn_abort(cn); return; } if(chmod(name,mode)==-1) { conn_perror(cn,"WARNING chmod()"); conn_printf(cn, "WARNING Can't change permissions on directory: %s\n", name); } struct utimbuf t; t.actime = atime; t.modtime = mtime; if(utime(name,&t)==-1) { conn_perror(cn, "WARNING utime"); conn_printf(cn, "WARNING Can't timestamp on directory: %s\n", name); } conn_printf(cn, "MKDIR %s\n", name); }
/* * read_mode read next mode from mode file * * return 0 on success, EOF on end of file, 1 on other error */ int read_mode(FILE *file_modes, enum Mode *mode) { char buf[10]; if (fscanf(file_modes, "%9s\n", buf) != 1) { if (feof(file_modes)) return EOF; fprintf(stderr, "\nerror reading mode control file: %s\n", strerror(errno)); return 1; } if (str2mode(buf, mode) != 0 || *mode == MRDTX) { fprintf(stderr, "\ninvalid amr_mode found in mode control file: '%s'\n", buf); return 1; } return 0; }
/* ***************************************************************************** * MAIN PROGRAM ***************************************************************************** */ int main (int argc, char *argv[]) { char *progname = argv[0]; char *modeStr = NULL; char *usedModeStr = NULL; char *fileName = NULL; char *modefileName = NULL; char *serialFileName = NULL; FILE *file_speech = NULL; /* File of speech data */ FILE *file_serial = NULL; /* File of coded bits */ FILE *file_modes = NULL; /* File with mode information */ Word16 new_speech[L_FRAME]; /* Pointer to new speech data */ Word16 serial[SERIAL_FRAMESIZE]; /* Output bitstream buffer */ #ifdef MMS_IO UWord8 packed_bits[MAX_PACKED_SIZE]; Word16 packed_size; #endif Word32 frame; Word16 dtx = 0; /* enable encoder DTX */ /* changed eedodr */ Word16 reset_flag; int i; enum Mode mode; enum Mode used_mode; enum TXFrameType tx_type; int useModeFile = 0; Speech_Encode_FrameState *speech_encoder_state = NULL; sid_syncState *sid_state = NULL; proc_head ("Encoder"); fprintf(stderr, "Code compiled with VAD option: %s\n\n", get_vadname()); /*----------------------------------------------------------------------* * Process command line options * *----------------------------------------------------------------------*/ while (argc > 1) { if (strcmp(argv[1], "-dtx") == 0) { dtx = 1; } else if (strncmp(argv[1], "-modefile=", 10) == 0) { useModeFile = 1; modefileName = argv[1]+10; } else break; argc--; argv++; } /*----------------------------------------------------------------------* * Check number of arguments * *----------------------------------------------------------------------*/ if ( (argc != 4 && !useModeFile) || (argc != 3 && useModeFile)) { fprintf (stderr, " Usage:\n\n" " %s [-dtx] amr_mode speech_file bitstream_file\n\n" " or \n\n" " %s [-dtx] -modefile=mode_file speech_file bitstream_file\n\n" " -dtx enables DTX mode\n" " -modefile=mode_file reads AMR modes from text file (one line per frame)\n\n", progname, progname); exit (1); } /*----------------------------------------------------------------------* * Open mode file or convert mode string * *----------------------------------------------------------------------*/ if (useModeFile) { fileName = argv[1]; serialFileName = argv[2]; /* Open mode control file */ if (strcmp(modefileName, "-") == 0) { file_modes = stdin; } else if ((file_modes = fopen (modefileName, "rt")) == NULL) { fprintf (stderr, "Error opening mode control file %s !!\n", modefileName); exit (1); } fprintf (stderr, " Mode control file: %s\n", modefileName); } else { modeStr = argv[1]; fileName = argv[2]; serialFileName = argv[3]; /* check and convert mode string */ if (str2mode(modeStr, &mode) != 0 && mode != MRDTX) { fprintf(stderr, "Invalid amr_mode specified: '%s'\n", modeStr); exit(1); } } /*----------------------------------------------------------------------* * Open speech file and result file (output serial bit stream) * *----------------------------------------------------------------------*/ if (strcmp(fileName, "-") == 0) { file_speech = stdin; } else if ((file_speech = fopen (fileName, "rb")) == NULL) { fprintf (stderr, "Error opening input file %s !!\n", fileName); exit (1); } fprintf (stderr, " Input speech file: %s\n", fileName); if (strcmp(serialFileName, "-") == 0) { file_serial = stdout; } else if ((file_serial = fopen (serialFileName, "wb")) == NULL) { fprintf (stderr,"Error opening output bitstream file %s !!\n",serialFileName); exit (1); } fprintf (stderr, " Output bitstream file: %s\n", serialFileName); /*-----------------------------------------------------------------------* * Initialisation of the coder. * *-----------------------------------------------------------------------*/ if ( Speech_Encode_Frame_init(&speech_encoder_state, dtx, "encoder") || sid_sync_init (&sid_state)) exit(-1); #ifdef MMS_IO /* write magic number to indicate single channel AMR file storage format */ fwrite(AMR_MAGIC_NUMBER, sizeof(UWord8), strlen(AMR_MAGIC_NUMBER), file_serial); #endif /*-----------------------------------------------------------------------* * Process speech frame by frame * *-----------------------------------------------------------------------*/ frame = 0; while (fread (new_speech, sizeof (Word16), L_FRAME, file_speech) == L_FRAME) { /* read new mode string from file if required */ if (useModeFile) { int res; if ((res = read_mode(file_modes, &mode)) == EOF) { fprintf(stderr, "\nend of mode control file reached"); break; } else if (res == 1) exit(-1); } frame++; /* zero flags and parameter bits */ for (i = 0; i < SERIAL_FRAMESIZE; i++) serial[i] = 0; /* check for homing frame */ reset_flag = encoder_homing_frame_test(new_speech); /* encode speech */ Speech_Encode_Frame(speech_encoder_state, mode, new_speech, &serial[1], &used_mode); /* print frame number and mode information */ mode2str(mode, &modeStr); mode2str(used_mode, &usedModeStr); if ( (frame%50) == 0) { fprintf (stderr, "\rframe=%-8d mode=%-5s used_mode=%-5s", frame, modeStr, usedModeStr); } /* include frame type and mode information in serial bitstream */ sid_sync (sid_state, used_mode, &tx_type); #ifndef MMS_IO serial[0] = tx_type; if (tx_type != TX_NO_DATA) { serial[1+MAX_SERIAL_SIZE] = mode; } else { serial[1+MAX_SERIAL_SIZE] = -1; } /* write bitstream to output file */ if (fwrite (serial, sizeof (Word16), SERIAL_FRAMESIZE, file_serial) != SERIAL_FRAMESIZE) { fprintf(stderr, "\nerror writing output file: %s\n", strerror(errno)); exit(-1); } #else packed_size = PackBits(used_mode, mode, tx_type, &serial[1], packed_bits); /* write file storage format bitstream to output file */ if (fwrite (packed_bits, sizeof (UWord8), packed_size, file_serial) != packed_size) { fprintf(stderr, "\nerror writing output file: %s\n", strerror(errno)); exit(-1); } #endif fflush(file_serial); /* perform homing if homing frame was detected at encoder input */ if (reset_flag != 0) { Speech_Encode_Frame_reset(speech_encoder_state); sid_sync_reset(sid_state); } } fprintf (stderr, "\n%d frame(s) processed\n", frame); /*-----------------------------------------------------------------------* * Close down speech coder * *-----------------------------------------------------------------------*/ Speech_Encode_Frame_exit(&speech_encoder_state); sid_sync_exit (&sid_state); return (0); }
static void proto_handle_get(conn_t* cn, const char* line) { struct stat st; // Reject filenames with just "." and ".." if(memcmp(line, ".\0", 2) == 0 || memcmp(line, "..\0", 3) == 0) { conn_printf(cn, "ERROR Protocol violation (%d)\n", __LINE__); conn_abort(cn); return; } if(lstat(line,&st) == 0) { if(S_ISREG(st.st_mode)) { int fd; md5_state_t md5_state; md5_init(&md5_state); char buffer[PATH_MAX]; ssize_t size; char md5str[33]; char modestr[11]; #ifdef O_NOATIME if((fd = open(line,O_RDONLY|O_NOATIME))==-1) { #else if((fd = open(line,O_RDONLY))==-1) { #endif conn_perror(cn, "WARNING open()"); conn_printf(cn, "WARNING Can't open file: %s\n", line); return; } // Calcuate MD5 { unsigned char md5bin[16]; while((size = read(fd, buffer, sizeof buffer))) md5_append(&md5_state, (unsigned char*)buffer, size); md5_finish(&md5_state, (unsigned char*)md5bin); if(lseek(fd, SEEK_SET, 0)==-1) { conn_perror(cn, "ERROR lseek()"); conn_abort(cn); return; } md5bin2str(md5bin, md5str); } mode2str(st.st_mode, modestr); conn_printf(cn, "PUT %ld %s %s %ld %ld %ld %s\n", (long int)st.st_size, md5str, modestr, st.st_atime, st.st_ctime, st.st_mtime, line); while((size = read(fd, buffer, sizeof buffer))) conn_write(cn, buffer, size); close(fd); } else if(S_ISDIR(st.st_mode)) { char modestr[11]; mode2str(st.st_mode, modestr); conn_printf(cn, "MKDIR %s %ld %ld %ld %s\n", modestr, st.st_atime, st.st_ctime, st.st_mtime, line); } else if(S_ISLNK(st.st_mode)) { char buffer[PATH_MAX]; conn_printf(cn, "SLNK %s\n", line); ssize_t l; if((l=readlink(line, buffer, sizeof buffer))==-1) { conn_perror(cn, "WARNING readlink()"); return; } buffer[l] = '\0'; conn_printf(cn, "%s\n", buffer); } else { conn_printf(cn, "WARNING Ignored %s\n", line); } } } static void proto_handle_put(conn_t* cn, const char* line) { const char* delim = " "; char* saveptr = NULL; char* token; char* ptr = (char*)line; int c = 0; ssize_t size = 0; mode_t mode = 0; time_t mtime = 0; time_t ctime = 0; time_t atime = 0; char* name = NULL; char* md5 = NULL; while((token = strtok_r(ptr, delim, &saveptr))) { switch(c) { case 0: size = atol(token); break; case 1: md5 = token; break; case 2: mode = str2mode(token); break; case 3: atime = atol(token); break; case 4: ctime = atol(token); break; case 5: mtime = atol(token); break; case 6: name = token; break; } c++; ptr = NULL; } if(c != 7) { conn_printf(cn, "ERROR Protocol violation (%d)\n", __LINE__); conn_abort(cn); return ; } int fd = creat(name, O_CREAT|O_TRUNC); if(fd == -1) { conn_perror(cn, "WARNING creat()"); return; } if(chmod(name,mode)==-1) { perror("WARNING chmod()"); } struct utimbuf t; t.actime = atime; t.modtime = mtime; if(utime(name,&t)==-1) { perror("WARNING utime"); } // CONTENT int bytes_left = size; int r; md5_state_t md5_state; unsigned char md5bin[16]; char md5str[33]; md5_init(&md5_state); while(!cn->abort && bytes_left) { if(cn->rbuf_len == 0) (void)conn_read(cn); r = MIN(bytes_left, cn->rbuf_len); if(r) { write(fd, cn->rbuf, r); md5_append(&md5_state, (unsigned char*)cn->rbuf, r); conn_shift(cn,r); bytes_left -= r; } assert(bytes_left >= 0); } close(fd); md5_finish(&md5_state, (unsigned char*)md5bin); md5bin2str(md5bin, md5str); // Check md5 if(strcmp(md5str,md5)!=0) { // Mismatch! conn_printf(cn, "WARNING %s md5-mismatch (%s <-> %s), removing file\n", name, md5str, md5); if(unlink(name)==-1) { perror("WARNING: unlink()"); } } else { struct utimbuf t; t.actime = atime; t.modtime = mtime; if(utime(name,&t)==-1) { perror("utime"); conn_printf(cn, "WARNING Can't timestamp on directory: %s\n", name); } // md5 is fine conn_printf(cn, "GET %s\n", name); } } static void proto_handle_slnk(conn_t* cn, const char* line) { char curdir[PATH_MAX]; // read next line to get target char target[PATH_MAX]; if(!conn_readline(cn, target, sizeof target)) return; getcwd(curdir, sizeof curdir); // Make sure it dosnt exist unlink(line); if(chdir(my_dirname(line))==-1) { conn_perror(cn, "WARNING chdir()"); return; } if(symlink(target, my_basename(line))==-1) { conn_perror(cn, "ERROR symlink()"); conn_abort(cn); return; } chdir(curdir); conn_printf(cn, "GET %s\n", line); }
int main(int argc, char **argv) { int fd; if (argc < 2) usage(); if (!strcmp(argv[1], "mode") && (argc == 3 || argc == 4)) { int disk, mode; char device[64]; if (!(sscanf(argv[2], "ad%d", &disk) == 1 || sscanf(argv[2], "acd%d", &disk) == 1 || sscanf(argv[2], "afd%d", &disk) == 1 || sscanf(argv[2], "ast%d", &disk) == 1)) { fprintf(stderr, "natacontrol: Invalid device %s\n", argv[2]); exit(EX_USAGE); } sprintf(device, "/dev/%s", argv[2]); if ((fd = open(device, O_RDONLY)) < 0) err(1, "device not found"); if (argc == 4) { mode = str2mode(argv[3]); if (ioctl(fd, IOCATASMODE, &mode) < 0) warn("ioctl(IOCATASMODE)"); } if (argc == 3 || argc == 4) { if (ioctl(fd, IOCATAGMODE, &mode) < 0) err(1, "ioctl(IOCATAGMODE)"); printf("current mode = %s\n", mode2str(mode)); } exit(EX_OK); } if (!strcmp(argv[1], "cap") && argc == 3) { int disk; char device[64]; if (!(sscanf(argv[2], "ad%d", &disk) == 1 || sscanf(argv[2], "acd%d", &disk) == 1 || sscanf(argv[2], "afd%d", &disk) == 1 || sscanf(argv[2], "ast%d", &disk) == 1)) { fprintf(stderr, "natacontrol: Invalid device %s\n", argv[2]); exit(EX_USAGE); } sprintf(device, "/dev/%s", argv[2]); if ((fd = open(device, O_RDONLY)) < 0) err(1, "device not found"); ata_cap_print(fd); exit(EX_OK); } if ((fd = open("/dev/ata", O_RDWR)) < 0) err(1, "control device not found"); if (!strcmp(argv[1], "list") && argc == 2) { int maxchannel, channel; if (ioctl(fd, IOCATAGMAXCHANNEL, &maxchannel) < 0) err(1, "ioctl(IOCATAGMAXCHANNEL)"); for (channel = 0; channel < maxchannel; channel++) info_print(fd, channel, 1); exit(EX_OK); } if (!strcmp(argv[1], "info") && argc == 3) { int channel; if (!(sscanf(argv[2], "ata%d", &channel) == 1)) { fprintf(stderr, "natacontrol: Invalid channel %s\n", argv[2]); exit(EX_USAGE); } info_print(fd, channel, 0); exit(EX_OK); } if (!strcmp(argv[1], "detach") && argc == 3) { int channel; if (!(sscanf(argv[2], "ata%d", &channel) == 1)) { fprintf(stderr, "natacontrol: Invalid channel %s\n", argv[2]); exit(EX_USAGE); } if (ioctl(fd, IOCATADETACH, &channel) < 0) err(1, "ioctl(IOCATADETACH)"); exit(EX_OK); } if (!strcmp(argv[1], "attach") && argc == 3) { int channel; if (!(sscanf(argv[2], "ata%d", &channel) == 1)) { fprintf(stderr, "natacontrol: Invalid channel %s\n", argv[2]); exit(EX_USAGE); } if (ioctl(fd, IOCATAATTACH, &channel) < 0) err(1, "ioctl(IOCATAATTACH)"); info_print(fd, channel, 0); exit(EX_OK); } if (!strcmp(argv[1], "reinit") && argc == 3) { int channel; if (!(sscanf(argv[2], "ata%d", &channel) == 1)) { fprintf(stderr, "natacontrol: Invalid channel %s\n", argv[2]); exit(EX_USAGE); } if (ioctl(fd, IOCATAREINIT, &channel) < 0) warn("ioctl(IOCATAREINIT)"); info_print(fd, channel, 0); exit(EX_OK); } if (!strcmp(argv[1], "create")) { int disk, dev, offset; struct ata_ioc_raid_config config; bzero(&config, sizeof(config)); if (argc > 2) { if (!strcasecmp(argv[2], "RAID0") || !strcasecmp(argv[2], "stripe")) config.type = AR_RAID0; if (!strcasecmp(argv[2], "RAID1") || !strcasecmp(argv[2],"mirror")) config.type = AR_RAID1; if (!strcasecmp(argv[2], "RAID0+1") || !strcasecmp(argv[2],"RAID10")) config.type = AR_RAID01; if (!strcasecmp(argv[2], "RAID5")) config.type = AR_RAID5; if (!strcasecmp(argv[2], "SPAN")) config.type = AR_SPAN; if (!strcasecmp(argv[2], "JBOD")) config.type = AR_JBOD; } if (!config.type) { fprintf(stderr, "natacontrol: Invalid RAID type %s\n", argv[2]); fprintf(stderr, "natacontrol: Valid RAID types: \n"); fprintf(stderr, " stripe | mirror | " "RAID0 | RAID1 | RAID0+1 | RAID5 | " "SPAN | JBOD\n"); exit(EX_USAGE); } if (config.type == AR_RAID0 || config.type == AR_RAID01 || config.type == AR_RAID5) { if (argc < 4 || !sscanf(argv[3], "%d", &config.interleave) == 1) { fprintf(stderr, "natacontrol: Invalid interleave %s\n", argv[3]); exit(EX_USAGE); } offset = 4; } else offset = 3; for (disk = 0; disk < 16 && (offset + disk) < argc; disk++) { if (!(sscanf(argv[offset + disk], "ad%d", &dev) == 1)) { fprintf(stderr, "natacontrol: Invalid disk %s\n", argv[offset + disk]); exit(EX_USAGE); } config.disks[disk] = dev; } if ((config.type == AR_RAID1 || config.type == AR_RAID01) && disk < 2) { fprintf(stderr, "natacontrol: At least 2 disks must be " "specified\n"); exit(EX_USAGE); } config.total_disks = disk; if (ioctl(fd, IOCATARAIDCREATE, &config) < 0) err(1, "ioctl(IOCATARAIDCREATE)"); else printf("ar%d created\n", config.lun); exit(EX_OK); } if (!strcmp(argv[1], "delete") && argc == 3) { int array; if (!(sscanf(argv[2], "ar%d", &array) == 1)) { fprintf(stderr, "natacontrol: Invalid array %s\n", argv[2]); exit(EX_USAGE); } if (ioctl(fd, IOCATARAIDDELETE, &array) < 0) warn("ioctl(IOCATARAIDDELETE)"); exit(EX_OK); } if (!strcmp(argv[1], "addspare") && argc == 4) { struct ata_ioc_raid_config config; if (!(sscanf(argv[2], "ar%d", &config.lun) == 1)) { fprintf(stderr, "natacontrol: Invalid array %s\n", argv[2]); usage(); } if (!(sscanf(argv[3], "ad%d", &config.disks[0]) == 1)) { fprintf(stderr, "natacontrol: Invalid disk %s\n", argv[3]); usage(); } if (ioctl(fd, IOCATARAIDADDSPARE, &config) < 0) warn("ioctl(IOCATARAIDADDSPARE)"); exit(EX_OK); } if (!strcmp(argv[1], "rebuild") && argc == 3) { int array; if (!(sscanf(argv[2], "ar%d", &array) == 1)) { fprintf(stderr, "natacontrol: Invalid array %s\n", argv[2]); usage(); } if (ioctl(fd, IOCATARAIDREBUILD, &array) < 0) warn("ioctl(IOCATARAIDREBUILD)"); else { char device[64]; char *buffer; ssize_t len; int arfd; if (daemon(0, 1) == -1) err(1, "daemon"); nice(20); snprintf(device, sizeof(device), "/dev/ar%d", array); if ((arfd = open(device, O_RDONLY)) == -1) err(1, "open %s", device); if ((buffer = malloc(1024 * 1024)) == NULL) err(1, "malloc"); while ((len = read(arfd, buffer, 1024 * 1024)) > 0) ; if (len == -1) err(1, "read"); else fprintf(stderr, "atacontrol: ar%d rebuild completed\n", array); free(buffer); close(arfd); } exit(EX_OK); } if (!strcmp(argv[1], "status") && argc == 3) { struct ata_ioc_raid_config config; int i; if (!(sscanf(argv[2], "ar%d", &config.lun) == 1)) { fprintf(stderr, "natacontrol: Invalid array %s\n", argv[2]); usage(); } if (ioctl(fd, IOCATARAIDSTATUS, &config) < 0) err(1, "ioctl(IOCATARAIDSTATUS)"); printf("ar%d: ATA ", config.lun); switch (config.type) { case AR_RAID0: printf("RAID0 stripesize=%d", config.interleave); break; case AR_RAID1: printf("RAID1"); break; case AR_RAID01: printf("RAID0+1 stripesize=%d", config.interleave); break; case AR_RAID5: printf("RAID5 stripesize=%d", config.interleave); break; case AR_JBOD: printf("JBOD"); case AR_SPAN: printf("SPAN"); break; } printf(" subdisks: "); for (i = 0; i < config.total_disks; i++) { if (config.disks[i] >= 0) printf("ad%d ", config.disks[i]); else printf("DOWN "); } printf("status: "); switch (config.status) { case AR_READY: printf("READY\n"); break; case AR_READY | AR_DEGRADED: printf("DEGRADED\n"); break; case AR_READY | AR_DEGRADED | AR_REBUILDING: printf("REBUILDING %d%% completed\n", config.progress); break; default: printf("BROKEN\n"); } exit(EX_OK); } usage(); exit(EX_OK); }
int devopen(const char *name, const char *mode) { int openfd; char *cp; char *ptr; int flag = 0; int len; int family; int protocol; char *hostname; char *hostnameslastcharp; char *localpname; char *localpnamelastcharp; flag = str2mode(mode); openfd = INVALID_HANDLE; /* /inet/protocol/localport/hostname/remoteport */ len = 6; cp = (char *) name + len; /* which protocol? */ if (strncmp(cp, "tcp/", 4) == 0) protocol = SOCK_STREAM; else if (strncmp(cp, "udp/", 4) == 0) protocol = SOCK_DGRAM; else { protocol = SOCK_STREAM; /* shut up the compiler */ fprintf(stderr, _("no (known) protocol supplied in special filename `%s'"), name); exit(1); } cp += 4; /* which localport? */ localpname = cp; while (*cp != '/' && *cp != '\0') cp++; /* * Require a port, let them explicitly put 0 if * they don't care. */ if (*cp != '/' || cp == localpname) { fprintf(stderr, _("special file name `%s' is incomplete"), name); exit(1); } /* * We change the special file name temporarily because we * need a 0-terminated string here for conversion with atoi(). * By using atoi() the use of decimal numbers is enforced. */ *cp = '\0'; localpnamelastcharp = cp; /* which hostname? */ cp++; hostname = cp; while (*cp != '/' && *cp != '\0') cp++; if (*cp != '/' || cp == hostname) { *localpnamelastcharp = '/'; fprintf(stderr, _("must supply a remote hostname to `/inet'")); exit(1); } *cp = '\0'; hostnameslastcharp = cp; /* which remoteport? */ cp++; /* * The remote port ends the special file name. * This means there already is a '\0' at the end of the string. * Therefore no need to patch any string ending. * * Here too, require a port, let them explicitly put 0 if * they don't care. */ if (*cp == '\0') { *localpnamelastcharp = '/'; *hostnameslastcharp = '/'; fprintf(stderr, _("must supply a remote port to `/inet'")); exit(1); } { #define DEFAULT_RETRIES 20 static unsigned long def_retries = DEFAULT_RETRIES; static bool first_time = true; unsigned long retries = 0; static long msleep = 1000; if (first_time) { char *cp, *end; unsigned long count = 0; char *ms2; first_time = false; if ((cp = getenv("GAWK_SOCK_RETRIES")) != NULL) { count = strtoul(cp, & end, 10); if (end != cp && count > 0) def_retries = count; } /* * Env var is in milliseconds, paramter to usleep() * is microseconds, make the conversion. Default is * 1 millisecond. */ if ((ms2 = getenv("GAWK_MSEC_SLEEP")) != NULL) { msleep = strtol(ms2, & end, 10); if (end == ms2 || msleep < 0) msleep = 1000; else msleep *= 1000; } } retries = def_retries; do { openfd = socketopen(family, protocol, localpname, cp, hostname); retries--; } while (openfd == INVALID_HANDLE && retries > 0 && usleep(msleep) == 0); } *localpnamelastcharp = '/'; *hostnameslastcharp = '/'; return openfd; }
int main(int argc, char **argv) { struct ata_cmd iocmd; int fd; if ((fd = open("/dev/ata", O_RDWR)) < 0) err(1, "control device not found"); if (argc < 2) usage(); bzero(&iocmd, sizeof(struct ata_cmd)); if (argc > 2 && strcmp(argv[1], "create")) { int chan; if (!strcmp(argv[1], "delete") || !strcmp(argv[1], "status") || !strcmp(argv[1], "rebuild")) { if (!(sscanf(argv[2], "%d", &chan) == 1 || sscanf(argv[2], "ar%d", &chan) == 1)) usage(); } else { if (!(sscanf(argv[2], "%d", &chan) == 1 || sscanf(argv[2], "ata%d", &chan) == 1)) usage(); } iocmd.channel = chan; } if (!strcmp(argv[1], "list") && argc == 2) { int unit = 0; while (info_print(fd, unit++, 1) != ENXIO); } else if (!strcmp(argv[1], "info") && argc == 3) { info_print(fd, iocmd.channel, 0); } else if (!strcmp(argv[1], "cap") && argc == 4) { ata_cap_print(fd, iocmd.channel, atoi(argv[3])); } else if (!strcmp(argv[1], "enclosure") && argc == 4) { iocmd.device = atoi(argv[3]); iocmd.cmd = ATAENCSTAT; if (ioctl(fd, IOCATA, &iocmd) < 0) err(1, "ioctl(ATAENCSTAT)"); printf("fan RPM: %d temp: %.1f 5V: %.2f 12V: %.2f\n", iocmd.u.enclosure.fan, (double)iocmd.u.enclosure.temp / 10, (double)iocmd.u.enclosure.v05 / 1000, (double)iocmd.u.enclosure.v12 / 1000); } else if (!strcmp(argv[1], "detach") && argc == 3) { iocmd.cmd = ATADETACH; if (ioctl(fd, IOCATA, &iocmd) < 0) err(1, "ioctl(ATADETACH)"); } else if (!strcmp(argv[1], "attach") && argc == 3) { iocmd.cmd = ATAATTACH; if (ioctl(fd, IOCATA, &iocmd) < 0) err(1, "ioctl(ATAATTACH)"); info_print(fd, iocmd.channel, 0); } else if (!strcmp(argv[1], "reinit") && argc == 3) { iocmd.cmd = ATAREINIT; if (ioctl(fd, IOCATA, &iocmd) < 0) warn("ioctl(ATAREINIT)"); info_print(fd, iocmd.channel, 0); } else if (!strcmp(argv[1], "create")) { int disk, dev, offset; iocmd.cmd = ATARAIDCREATE; if (!strcmp(argv[2], "RAID0") || !strcmp(argv[2], "stripe")) iocmd.u.raid_setup.type = 1; if (!strcmp(argv[2], "RAID1") || !strcmp(argv[2],"mirror")) iocmd.u.raid_setup.type = 2; if (!strcmp(argv[2], "RAID0+1")) iocmd.u.raid_setup.type = 3; if (!strcmp(argv[2], "SPAN") || !strcmp(argv[2], "JBOD")) iocmd.u.raid_setup.type = 4; if (!iocmd.u.raid_setup.type) usage(); if (iocmd.u.raid_setup.type & 1) { if (!sscanf(argv[3], "%d", &iocmd.u.raid_setup.interleave) == 1) usage(); offset = 4; } else offset = 3; for (disk = 0; disk < 16 && (offset + disk) < argc; disk++) { if (!(sscanf(argv[offset + disk], "%d", &dev) == 1 || sscanf(argv[offset + disk], "ad%d", &dev) == 1)) usage(); iocmd.u.raid_setup.disks[disk] = dev; } iocmd.u.raid_setup.total_disks = disk; if (ioctl(fd, IOCATA, &iocmd) < 0) err(1, "ioctl(ATARAIDCREATE)"); else printf("ar%d created\n", iocmd.u.raid_setup.unit); } else if (!strcmp(argv[1], "delete") && argc == 3) { iocmd.cmd = ATARAIDDELETE; if (ioctl(fd, IOCATA, &iocmd) < 0) warn("ioctl(ATARAIDDELETE)"); } else if (!strcmp(argv[1], "rebuild") && argc == 3) { iocmd.cmd = ATARAIDREBUILD; if (ioctl(fd, IOCATA, &iocmd) < 0) warn("ioctl(ATARAIDREBUILD)"); } else if (!strcmp(argv[1], "status") && argc == 3) { int i; iocmd.cmd = ATARAIDSTATUS; if (ioctl(fd, IOCATA, &iocmd) < 0) err(1, "ioctl(ATARAIDSTATUS)"); printf("ar%d: ATA ", iocmd.channel); switch (iocmd.u.raid_status.type) { case AR_RAID0: printf("RAID0"); break; case AR_RAID1: printf("RAID1"); break; case AR_RAID0 | AR_RAID1: printf("RAID0+1"); break; case AR_SPAN: printf("SPAN"); break; } printf(" subdisks: "); for (i = 0; i < iocmd.u.raid_status.total_disks; i++) { if (iocmd.u.raid_status.disks[i] >= 0) printf("ad%d ", iocmd.u.raid_status.disks[i]); else printf("DOWN "); } printf("status: "); switch (iocmd.u.raid_status.status) { case AR_READY: printf("READY\n"); break; case AR_READY | AR_DEGRADED: printf("DEGRADED\n"); break; case AR_READY | AR_DEGRADED | AR_REBUILDING: printf("REBUILDING %d%% completed\n", iocmd.u.raid_status.progress); break; default: printf("BROKEN\n"); } } else if (!strcmp(argv[1], "mode") && (argc == 3 || argc == 5)) { if (argc == 5) { iocmd.cmd = ATASMODE; iocmd.device = -1; iocmd.u.mode.mode[0] = str2mode(argv[3]); iocmd.u.mode.mode[1] = str2mode(argv[4]); if (ioctl(fd, IOCATA, &iocmd) < 0) warn("ioctl(ATASMODE)"); } if (argc == 3 || argc == 5) { iocmd.cmd = ATAGMODE; iocmd.device = -1; if (ioctl(fd, IOCATA, &iocmd) < 0) err(1, "ioctl(ATAGMODE)"); printf("Master = %s \nSlave = %s\n", mode2str(iocmd.u.mode.mode[0]), mode2str(iocmd.u.mode.mode[1])); } } else usage(); exit(0); }