/* * This is the original version of the program in the man page. * Copy-and-paste whatever you need from it. */ int main(int argc, char **argv) { char *cinput = "1.5K", buf[FMT_SCALED_STRSIZE]; long long ninput = 10483892, result; if (scan_scaled(cinput, &result) == 0) printf("\"%s\" -> %lld\n", cinput, result); else perror(cinput); if (fmt_scaled(ninput, buf) == 0) printf("%lld -> \"%s\"\n", ninput, buf); else fprintf(stderr, "%lld invalid (%s)\n", ninput, strerror(errno)); return 0; }
int parse_size(struct parse_result *res, char *word, long long val) { if (word != NULL) { if (scan_scaled(word, &val) != 0) { warn("invalid size: %s", word); return (-1); } } if (val < (1024 * 1024)) { warnx("size must be at least one megabyte"); return (-1); } else res->size = val / 1024 / 1024; if ((res->size * 1024 * 1024) != val) warnx("size rounded to %lld megabytes", res->size); return (0); }
int process_config_line(Options *options, const char *host, char *line, const char *filename, int linenum, int *activep, int userconfig) { char *s, **charptr, *endofnumber, *keyword, *arg, *arg2; char **cpptr, fwdarg[256]; u_int i, *uintptr, max_entries = 0; int negated, opcode, *intptr, value, value2; LogLevel *log_level_ptr; long long val64; size_t len; Forward fwd; /* Strip trailing whitespace */ for (len = strlen(line) - 1; len > 0; len--) { if (strchr(WHITESPACE, line[len]) == NULL) break; line[len] = '\0'; } s = line; /* Get the keyword. (Each line is supposed to begin with a keyword). */ if ((keyword = strdelim(&s)) == NULL) return 0; /* Ignore leading whitespace. */ if (*keyword == '\0') keyword = strdelim(&s); if (keyword == NULL || !*keyword || *keyword == '\n' || *keyword == '#') return 0; /* Match lowercase keyword */ for (i = 0; i < strlen(keyword); i++) keyword[i] = tolower(keyword[i]); opcode = parse_token(keyword, filename, linenum, options->ignored_unknown); switch (opcode) { case oBadOption: /* don't panic, but count bad options */ return -1; /* NOTREACHED */ case oIgnoredUnknownOption: debug("%s line %d: Ignored unknown option \"%s\"", filename, linenum, keyword); return 0; case oConnectTimeout: intptr = &options->connection_timeout; parse_time: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%s line %d: missing time value.", filename, linenum); if ((value = convtime(arg)) == -1) fatal("%s line %d: invalid time value.", filename, linenum); if (*activep && *intptr == -1) *intptr = value; break; case oForwardAgent: intptr = &options->forward_agent; parse_flag: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing yes/no argument.", filename, linenum); value = 0; /* To avoid compiler warning... */ if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0) value = 1; else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0) value = 0; else fatal("%.200s line %d: Bad yes/no argument.", filename, linenum); if (*activep && *intptr == -1) *intptr = value; break; case oForwardX11: intptr = &options->forward_x11; goto parse_flag; case oForwardX11Trusted: intptr = &options->forward_x11_trusted; goto parse_flag; case oForwardX11Timeout: intptr = &options->forward_x11_timeout; goto parse_time; case oGatewayPorts: intptr = &options->gateway_ports; goto parse_flag; case oExitOnForwardFailure: intptr = &options->exit_on_forward_failure; goto parse_flag; case oUsePrivilegedPort: intptr = &options->use_privileged_port; goto parse_flag; case oPasswordAuthentication: intptr = &options->password_authentication; goto parse_flag; case oZeroKnowledgePasswordAuthentication: intptr = &options->zero_knowledge_password_authentication; goto parse_flag; case oKbdInteractiveAuthentication: intptr = &options->kbd_interactive_authentication; goto parse_flag; case oKbdInteractiveDevices: charptr = &options->kbd_interactive_devices; goto parse_string; case oPubkeyAuthentication: intptr = &options->pubkey_authentication; goto parse_flag; case oRSAAuthentication: intptr = &options->rsa_authentication; goto parse_flag; case oRhostsRSAAuthentication: intptr = &options->rhosts_rsa_authentication; goto parse_flag; case oHostbasedAuthentication: intptr = &options->hostbased_authentication; goto parse_flag; case oChallengeResponseAuthentication: intptr = &options->challenge_response_authentication; goto parse_flag; case oGssAuthentication: intptr = &options->gss_authentication; goto parse_flag; case oGssDelegateCreds: intptr = &options->gss_deleg_creds; goto parse_flag; case oBatchMode: intptr = &options->batch_mode; goto parse_flag; case oCheckHostIP: intptr = &options->check_host_ip; goto parse_flag; case oVerifyHostKeyDNS: intptr = &options->verify_host_key_dns; goto parse_yesnoask; case oStrictHostKeyChecking: intptr = &options->strict_host_key_checking; parse_yesnoask: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing yes/no/ask argument.", filename, linenum); value = 0; /* To avoid compiler warning... */ if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0) value = 1; else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0) value = 0; else if (strcmp(arg, "ask") == 0) value = 2; else fatal("%.200s line %d: Bad yes/no/ask argument.", filename, linenum); if (*activep && *intptr == -1) *intptr = value; break; case oCompression: intptr = &options->compression; goto parse_flag; case oTCPKeepAlive: intptr = &options->tcp_keep_alive; goto parse_flag; case oNoHostAuthenticationForLocalhost: intptr = &options->no_host_authentication_for_localhost; goto parse_flag; case oNumberOfPasswordPrompts: intptr = &options->number_of_password_prompts; goto parse_int; case oCompressionLevel: intptr = &options->compression_level; goto parse_int; case oRekeyLimit: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (strcmp(arg, "default") == 0) { val64 = 0; } else { if (scan_scaled(arg, &val64) == -1) fatal("%.200s line %d: Bad number '%s': %s", filename, linenum, arg, strerror(errno)); /* check for too-large or too-small limits */ if (val64 > UINT_MAX) fatal("%.200s line %d: RekeyLimit too large", filename, linenum); if (val64 != 0 && val64 < 16) fatal("%.200s line %d: RekeyLimit too small", filename, linenum); } if (*activep && options->rekey_limit == -1) options->rekey_limit = (u_int32_t)val64; if (s != NULL) { /* optional rekey interval present */ if (strcmp(s, "none") == 0) { (void)strdelim(&s); /* discard */ break; } intptr = &options->rekey_interval; goto parse_time; } break; case oIdentityFile: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (*activep) { intptr = &options->num_identity_files; if (*intptr >= SSH_MAX_IDENTITY_FILES) fatal("%.200s line %d: Too many identity files specified (max %d).", filename, linenum, SSH_MAX_IDENTITY_FILES); add_identity_file(options, NULL, arg, userconfig); } break; case oXAuthLocation: charptr=&options->xauth_location; goto parse_string; case oUser: charptr = &options->user; parse_string: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (*activep && *charptr == NULL) *charptr = xstrdup(arg); break; case oGlobalKnownHostsFile: cpptr = (char **)&options->system_hostfiles; uintptr = &options->num_system_hostfiles; max_entries = SSH_MAX_HOSTS_FILES; parse_char_array: if (*activep && *uintptr == 0) { while ((arg = strdelim(&s)) != NULL && *arg != '\0') { if ((*uintptr) >= max_entries) fatal("%s line %d: " "too many authorized keys files.", filename, linenum); cpptr[(*uintptr)++] = xstrdup(arg); } } return 0; case oUserKnownHostsFile: cpptr = (char **)&options->user_hostfiles; uintptr = &options->num_user_hostfiles; max_entries = SSH_MAX_HOSTS_FILES; goto parse_char_array; case oHostName: charptr = &options->hostname; goto parse_string; case oHostKeyAlias: charptr = &options->host_key_alias; goto parse_string; case oPreferredAuthentications: charptr = &options->preferred_authentications; goto parse_string; case oBindAddress: charptr = &options->bind_address; goto parse_string; case oPKCS11Provider: charptr = &options->pkcs11_provider; goto parse_string; case oProxyCommand: charptr = &options->proxy_command; parse_command: if (s == NULL) fatal("%.200s line %d: Missing argument.", filename, linenum); len = strspn(s, WHITESPACE "="); if (*activep && *charptr == NULL) *charptr = xstrdup(s + len); return 0; case oPort: intptr = &options->port; parse_int: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (arg[0] < '0' || arg[0] > '9') fatal("%.200s line %d: Bad number.", filename, linenum); /* Octal, decimal, or hex format? */ value = strtol(arg, &endofnumber, 0); if (arg == endofnumber) fatal("%.200s line %d: Bad number.", filename, linenum); if (*activep && *intptr == -1) *intptr = value; break; case oConnectionAttempts: intptr = &options->connection_attempts; goto parse_int; case oCipher: intptr = &options->cipher; arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); value = cipher_number(arg); if (value == -1) fatal("%.200s line %d: Bad cipher '%s'.", filename, linenum, arg ? arg : "<NONE>"); if (*activep && *intptr == -1) *intptr = value; break; case oCiphers: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (!ciphers_valid(arg)) fatal("%.200s line %d: Bad SSH2 cipher spec '%s'.", filename, linenum, arg ? arg : "<NONE>"); if (*activep && options->ciphers == NULL) options->ciphers = xstrdup(arg); break; case oMacs: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (!mac_valid(arg)) fatal("%.200s line %d: Bad SSH2 Mac spec '%s'.", filename, linenum, arg ? arg : "<NONE>"); if (*activep && options->macs == NULL) options->macs = xstrdup(arg); break; case oKexAlgorithms: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (!kex_names_valid(arg)) fatal("%.200s line %d: Bad SSH2 KexAlgorithms '%s'.", filename, linenum, arg ? arg : "<NONE>"); if (*activep && options->kex_algorithms == NULL) options->kex_algorithms = xstrdup(arg); break; case oHostKeyAlgorithms: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (!key_names_valid2(arg)) fatal("%.200s line %d: Bad protocol 2 host key algorithms '%s'.", filename, linenum, arg ? arg : "<NONE>"); if (*activep && options->hostkeyalgorithms == NULL) options->hostkeyalgorithms = xstrdup(arg); break; case oProtocol: intptr = &options->protocol; arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); value = proto_spec(arg); if (value == SSH_PROTO_UNKNOWN) fatal("%.200s line %d: Bad protocol spec '%s'.", filename, linenum, arg ? arg : "<NONE>"); if (*activep && *intptr == SSH_PROTO_UNKNOWN) *intptr = value; break; case oLogLevel: log_level_ptr = &options->log_level; arg = strdelim(&s); value = log_level_number(arg); if (value == SYSLOG_LEVEL_NOT_SET) fatal("%.200s line %d: unsupported log level '%s'", filename, linenum, arg ? arg : "<NONE>"); if (*activep && *log_level_ptr == SYSLOG_LEVEL_NOT_SET) *log_level_ptr = (LogLevel) value; break; case oLocalForward: case oRemoteForward: case oDynamicForward: arg = strdelim(&s); if (arg == NULL || *arg == '\0') fatal("%.200s line %d: Missing port argument.", filename, linenum); if (opcode == oLocalForward || opcode == oRemoteForward) { arg2 = strdelim(&s); if (arg2 == NULL || *arg2 == '\0') fatal("%.200s line %d: Missing target argument.", filename, linenum); /* construct a string for parse_forward */ snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2); } else if (opcode == oDynamicForward) { strlcpy(fwdarg, arg, sizeof(fwdarg)); } if (parse_forward(&fwd, fwdarg, opcode == oDynamicForward ? 1 : 0, opcode == oRemoteForward ? 1 : 0) == 0) fatal("%.200s line %d: Bad forwarding specification.", filename, linenum); if (*activep) { if (opcode == oLocalForward || opcode == oDynamicForward) add_local_forward(options, &fwd); else if (opcode == oRemoteForward) add_remote_forward(options, &fwd); } break; case oClearAllForwardings: intptr = &options->clear_forwardings; goto parse_flag; case oHost: *activep = 0; arg2 = NULL; while ((arg = strdelim(&s)) != NULL && *arg != '\0') { negated = *arg == '!'; if (negated) arg++; if (match_pattern(host, arg)) { if (negated) { debug("%.200s line %d: Skipping Host " "block because of negated match " "for %.100s", filename, linenum, arg); *activep = 0; break; } if (!*activep) arg2 = arg; /* logged below */ *activep = 1; } } if (*activep) debug("%.200s line %d: Applying options for %.100s", filename, linenum, arg2); /* Avoid garbage check below, as strdelim is done. */ return 0; case oEscapeChar: intptr = &options->escape_char; arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); if (arg[0] == '^' && arg[2] == 0 && (u_char) arg[1] >= 64 && (u_char) arg[1] < 128) value = (u_char) arg[1] & 31; else if (strlen(arg) == 1) value = (u_char) arg[0]; else if (strcmp(arg, "none") == 0) value = SSH_ESCAPECHAR_NONE; else { fatal("%.200s line %d: Bad escape character.", filename, linenum); /* NOTREACHED */ value = 0; /* Avoid compiler warning. */ } if (*activep && *intptr == -1) *intptr = value; break; case oAddressFamily: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%s line %d: missing address family.", filename, linenum); intptr = &options->address_family; if (strcasecmp(arg, "inet") == 0) value = AF_INET; else if (strcasecmp(arg, "inet6") == 0) value = AF_INET6; else if (strcasecmp(arg, "any") == 0) value = AF_UNSPEC; else fatal("Unsupported AddressFamily \"%s\"", arg); if (*activep && *intptr == -1) *intptr = value; break; case oEnableSSHKeysign: intptr = &options->enable_ssh_keysign; goto parse_flag; case oIdentitiesOnly: intptr = &options->identities_only; goto parse_flag; case oServerAliveInterval: intptr = &options->server_alive_interval; goto parse_time; case oServerAliveCountMax: intptr = &options->server_alive_count_max; goto parse_int; case oSendEnv: while ((arg = strdelim(&s)) != NULL && *arg != '\0') { if (strchr(arg, '=') != NULL) fatal("%s line %d: Invalid environment name.", filename, linenum); if (!*activep) continue; if (options->num_send_env >= MAX_SEND_ENV) fatal("%s line %d: too many send env.", filename, linenum); options->send_env[options->num_send_env++] = xstrdup(arg); } break; case oControlPath: charptr = &options->control_path; goto parse_string; case oControlMaster: intptr = &options->control_master; arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing ControlMaster argument.", filename, linenum); value = 0; /* To avoid compiler warning... */ if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0) value = SSHCTL_MASTER_YES; else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0) value = SSHCTL_MASTER_NO; else if (strcmp(arg, "auto") == 0) value = SSHCTL_MASTER_AUTO; else if (strcmp(arg, "ask") == 0) value = SSHCTL_MASTER_ASK; else if (strcmp(arg, "autoask") == 0) value = SSHCTL_MASTER_AUTO_ASK; else fatal("%.200s line %d: Bad ControlMaster argument.", filename, linenum); if (*activep && *intptr == -1) *intptr = value; break; case oControlPersist: /* no/false/yes/true, or a time spec */ intptr = &options->control_persist; arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing ControlPersist" " argument.", filename, linenum); value = 0; value2 = 0; /* timeout */ if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0) value = 0; else if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0) value = 1; else if ((value2 = convtime(arg)) >= 0) value = 1; else fatal("%.200s line %d: Bad ControlPersist argument.", filename, linenum); if (*activep && *intptr == -1) { *intptr = value; options->control_persist_timeout = value2; } break; case oHashKnownHosts: intptr = &options->hash_known_hosts; goto parse_flag; case oTunnel: intptr = &options->tun_open; arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%s line %d: Missing yes/point-to-point/" "ethernet/no argument.", filename, linenum); value = 0; /* silence compiler */ if (strcasecmp(arg, "ethernet") == 0) value = SSH_TUNMODE_ETHERNET; else if (strcasecmp(arg, "point-to-point") == 0) value = SSH_TUNMODE_POINTOPOINT; else if (strcasecmp(arg, "yes") == 0) value = SSH_TUNMODE_DEFAULT; else if (strcasecmp(arg, "no") == 0) value = SSH_TUNMODE_NO; else fatal("%s line %d: Bad yes/point-to-point/ethernet/" "no argument: %s", filename, linenum, arg); if (*activep) *intptr = value; break; case oTunnelDevice: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%.200s line %d: Missing argument.", filename, linenum); value = a2tun(arg, &value2); if (value == SSH_TUNID_ERR) fatal("%.200s line %d: Bad tun device.", filename, linenum); if (*activep) { options->tun_local = value; options->tun_remote = value2; } break; case oLocalCommand: charptr = &options->local_command; goto parse_command; case oPermitLocalCommand: intptr = &options->permit_local_command; goto parse_flag; case oVisualHostKey: intptr = &options->visual_host_key; goto parse_flag; case oIPQoS: arg = strdelim(&s); if ((value = parse_ipqos(arg)) == -1) fatal("%s line %d: Bad IPQoS value: %s", filename, linenum, arg); arg = strdelim(&s); if (arg == NULL) value2 = value; else if ((value2 = parse_ipqos(arg)) == -1) fatal("%s line %d: Bad IPQoS value: %s", filename, linenum, arg); if (*activep) { options->ip_qos_interactive = value; options->ip_qos_bulk = value2; } break; case oUseRoaming: intptr = &options->use_roaming; goto parse_flag; case oRequestTTY: arg = strdelim(&s); if (!arg || *arg == '\0') fatal("%s line %d: missing argument.", filename, linenum); intptr = &options->request_tty; if (strcasecmp(arg, "yes") == 0) value = REQUEST_TTY_YES; else if (strcasecmp(arg, "no") == 0) value = REQUEST_TTY_NO; else if (strcasecmp(arg, "force") == 0) value = REQUEST_TTY_FORCE; else if (strcasecmp(arg, "auto") == 0) value = REQUEST_TTY_AUTO; else fatal("Unsupported RequestTTY \"%s\"", arg); if (*activep && *intptr == -1) *intptr = value; break; case oIgnoreUnknown: charptr = &options->ignored_unknown; goto parse_string; case oDeprecated: debug("%s line %d: Deprecated option \"%s\"", filename, linenum, keyword); return 0; case oUnsupported: error("%s line %d: Unsupported option \"%s\"", filename, linenum, keyword); return 0; default: fatal("process_config_line: Unimplemented opcode %d", opcode); } /* Check that there is no garbage at end of line. */ if ((arg = strdelim(&s)) != NULL && *arg != '\0') { fatal("%.200s line %d: garbage at end of line; \"%.200s\".", filename, linenum, arg); } return 0; }
int ct_settings_add(struct ct_settings *settings, char *var, char *val) { int rv = 1, *p; double *f; char **s; struct ct_settings *cs; if (settings == NULL) CFATALX("invalid parameters"); for (cs = settings; cs->cs_name != NULL; cs++) { if (strcmp(var, cs->cs_name)) continue; if (cs->cs_secure == 0) CNDBG(CTUTIL_LOG_CONFIG, "settings_add: %s=%s\n", var, val); if (cs->cs_s) { if (cs->cs_s->csp_set(cs, val)) CFATALX("invalid value for %s: %s", var, val); rv = 0; break; } else switch (cs->cs_type) { case CT_S_INT: p = cs->cs_ival; *p = atoi(val); rv = 0; break; case CT_S_DIR: case CT_S_STR: s = cs->cs_sval; if (s == NULL) CFATALX("invalid sval for %s", cs->cs_name); if (*s) free(*s); if (cs->cs_type == CT_S_DIR) ct_expand_tilde(s, val); else *s = strdup(val); if (*s == NULL) CFATAL("no memory for %s", cs->cs_name); rv = 0; break; case CT_S_FLOAT: f = cs->cs_fval; *f = atof(val); rv = 0; break; case CT_S_SIZE: if (scan_scaled(val, cs->cs_szval) != 0) CFATAL("can't parse size for %s", cs->cs_name); rv = 0; break; case CT_S_INVALID: default: CFATALX("invalid type for %s", var); } break; } return (rv); }
int main(int argc, char *argv[]) { int ch; struct partition *pp; struct disklabel *lp; struct partition oldpartition; struct stat st; struct statfs *mp; struct rlimit rl; int fsi = -1, fso, len, n, maxpartitions; char *cp = NULL, *s1, *s2, *special, *opstring, *realdev; char *fstype = NULL; char **saveargv = argv; int ffsflag = 1; const char *errstr; long long fssize_input = 0; int fssize_usebytes = 0; u_int64_t nsecs; getphysmem(); maxpartitions = getmaxpartitions(); if (maxpartitions > 26) fatal("insane maxpartitions value %d", maxpartitions); opstring = "NO:S:T:b:c:e:f:g:h:i:m:o:qs:t:"; while ((ch = getopt(argc, argv, opstring)) != -1) { switch (ch) { case 'N': Nflag = 1; break; case 'O': Oflag = strtonum(optarg, 0, 2, &errstr); if (errstr) fatal("%s: invalid ffs version", optarg); break; case 'S': if (scan_scaled(optarg, §orsize) == -1 || sectorsize <= 0 || (sectorsize % DEV_BSIZE)) fatal("sector size invalid: %s", optarg); break; case 'T': disktype = optarg; break; case 'b': bsize = strtonum(optarg, MINBSIZE, MAXBSIZE, &errstr); if (errstr) fatal("block size is %s: %s", errstr, optarg); break; case 'c': maxfrgspercg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("fragments per cylinder group is %s: %s", errstr, optarg); break; case 'e': maxbpg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("blocks per file in a cylinder group is" " %s: %s", errstr, optarg); break; case 'f': fsize = strtonum(optarg, MINBSIZE / MAXFRAG, MAXBSIZE, &errstr); if (errstr) fatal("fragment size is %s: %s", errstr, optarg); break; case 'g': avgfilesize = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average file size is %s: %s", errstr, optarg); break; case 'h': avgfilesperdir = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average files per dir is %s: %s", errstr, optarg); break; case 'i': density = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("bytes per inode is %s: %s", errstr, optarg); break; case 'm': minfree = strtonum(optarg, 0, 99, &errstr); if (errstr) fatal("free space %% is %s: %s", errstr, optarg); break; case 'o': if (strcmp(optarg, "space") == 0) reqopt = opt = FS_OPTSPACE; else if (strcmp(optarg, "time") == 0) reqopt = opt = FS_OPTTIME; else fatal("%s: unknown optimization preference: " "use `space' or `time'.", optarg); break; case 'q': quiet = 1; break; case 's': if (scan_scaled(optarg, &fssize_input) == -1 || fssize_input == 0) fatal("file system size invalid: %s", optarg); fssize_usebytes = 0; /* in case of multiple -s */ for (s1 = optarg; *s1 != '\0'; s1++) if (isalpha((unsigned char)*s1)) { fssize_usebytes = 1; break; } break; case 't': fstype = optarg; if (strcmp(fstype, "ffs")) ffsflag = 0; break; case '?': default: usage(); } if (!ffsflag) break; } argc -= optind; argv += optind; if (ffsflag && argc != 1) usage(); special = argv[0]; char execname[MAXPATHLEN], name[MAXPATHLEN]; if (fstype == NULL) fstype = readlabelfs(special, 0); if (fstype != NULL && strcmp(fstype, "ffs")) { snprintf(name, sizeof name, "newfs_%s", fstype); saveargv[0] = name; snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_SBIN, fstype); (void)execv(execname, saveargv); snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_USRSBIN, fstype); (void)execv(execname, saveargv); err(1, "%s not found", name); } if (Nflag) { fso = -1; } else { fso = opendev(special, O_WRONLY, 0, &realdev); if (fso < 0) fatal("%s: %s", special, strerror(errno)); special = realdev; /* Bail if target special is mounted */ n = getmntinfo(&mp, MNT_NOWAIT); if (n == 0) fatal("%s: getmntinfo: %s", special, strerror(errno)); len = sizeof(_PATH_DEV) - 1; s1 = special; if (strncmp(_PATH_DEV, s1, len) == 0) s1 += len; while (--n >= 0) { s2 = mp->f_mntfromname; if (strncmp(_PATH_DEV, s2, len) == 0) { s2 += len - 1; *s2 = 'r'; } if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) fatal("%s is mounted on %s", special, mp->f_mntonname); ++mp; } } fsi = opendev(special, O_RDONLY, 0, NULL); if (fsi < 0) fatal("%s: %s", special, strerror(errno)); if (fstat(fsi, &st) < 0) fatal("%s: %s", special, strerror(errno)); if (S_ISBLK(st.st_mode)) fatal("%s: block device", special); if (!S_ISCHR(st.st_mode)) warnx("%s: not a character-special device", special); if (*argv[0] == '\0') fatal("empty partition name supplied"); cp = argv[0] + strlen(argv[0]) - 1; if ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) && !isdigit((unsigned char)*cp)) fatal("%s: can't figure out file system partition", argv[0]); lp = getdisklabel(special, fsi); if (pledge("stdio disklabel tty", NULL) == -1) err(1, "pledge"); if (isdigit((unsigned char)*cp)) pp = &lp->d_partitions[0]; else pp = &lp->d_partitions[*cp - 'a']; if (DL_GETPSIZE(pp) == 0) fatal("%s: `%c' partition is unavailable", argv[0], *cp); if (pp->p_fstype == FS_BOOT) fatal("%s: `%c' partition overlaps boot program", argv[0], *cp); havelabel: if (sectorsize == 0) { sectorsize = lp->d_secsize; if (sectorsize <= 0) fatal("%s: no default sector size", argv[0]); } if (fssize_input < 0) { #if 1 fatal("-s < 0 not yet implemented"); #else long long gap; /* leave gap at the end of partition */ fssize_input = -fssize_input; if (fssize_usebytes) { gap = (daddr_t)fssize_input / (daddr_t)sectorsize; if ((daddr_t)fssize_input % (daddr_t)sectorsize != 0) gap++; } else gap = fssize_input; if (gap >= DL_GETPSIZE(pp)) fatal("%s: requested gap of %lld sectors on partition " "'%c' is too big", argv[0], gap, *cp); nsecs = DL_GETPSIZE(pp) - gap; #endif } else { if (fssize_usebytes) { nsecs = fssize_input / sectorsize; if (fssize_input % sectorsize != 0) nsecs++; } else if (fssize_input == 0) nsecs = DL_GETPSIZE(pp); else nsecs = fssize_input; } if (nsecs > DL_GETPSIZE(pp)) fatal("%s: maximum file system size on the `%c' partition is " "%llu sectors", argv[0], *cp, DL_GETPSIZE(pp)); /* Can't use DL_SECTOBLK() because sectorsize may not be from label! */ fssize = nsecs * (sectorsize / DEV_BSIZE); if (fsize == 0) { fsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); if (fsize <= 0) fsize = MAX(DFL_FRAGSIZE, lp->d_secsize); } if (bsize == 0) { bsize = DISKLABELV1_FFS_BSIZE(pp->p_fragblock); if (bsize <= 0) bsize = MIN(DFL_BLKSIZE, 8 * fsize); } if (density == 0) density = NFPI * fsize; if (minfree < MINFREE && opt != FS_OPTSPACE && reqopt == -1) { warnx("warning: changing optimization to space " "because minfree is less than %d%%\n", MINFREE); opt = FS_OPTSPACE; } if (maxbpg == 0) { if (Oflag <= 1) maxbpg = MAXBLKPG_FFS1(bsize); else maxbpg = MAXBLKPG_FFS2(bsize); } oldpartition = *pp; mkfs(pp, special, fsi, fso); if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition))) rewritelabel(special, fso, lp); if (!Nflag) close(fso); close(fsi); exit(0); }
int main(int argc, char *argv[]) { int ch; struct partition *pp; struct disklabel *lp; struct disklabel mfsfakelabel; struct partition oldpartition; struct stat st; struct statfs *mp; struct rlimit rl; int fsi = -1, oflagset = 0, fso, len, n, maxpartitions; char *cp = NULL, *s1, *s2, *special, *opstring, *realdev; #ifdef MFS char mountfromname[BUFSIZ]; char *pop = NULL, node[PATH_MAX]; pid_t pid, res; struct statfs sf; struct stat mountpoint; int status; #endif uid_t mfsuid = 0; gid_t mfsgid = 0; mode_t mfsmode = 0; char *fstype = NULL; char **saveargv = argv; int ffsflag = 1; const char *errstr; long long fssize_input = 0; int fssize_usebytes = 0; u_int64_t nsecs; if (strstr(__progname, "mfs")) mfs = Nflag = quiet = 1; getphysmem(); maxpartitions = getmaxpartitions(); if (maxpartitions > 26) fatal("insane maxpartitions value %d", maxpartitions); opstring = mfs ? "P:T:b:c:e:f:i:m:o:s:" : "NO:S:T:b:c:e:f:g:h:i:m:o:qs:t:"; while ((ch = getopt(argc, argv, opstring)) != -1) { switch (ch) { case 'N': Nflag = 1; break; case 'O': Oflag = strtonum(optarg, 0, 2, &errstr); if (errstr) fatal("%s: invalid ffs version", optarg); oflagset = 1; break; case 'S': if (scan_scaled(optarg, §orsize) == -1 || sectorsize <= 0 || (sectorsize % DEV_BSIZE)) fatal("sector size invalid: %s", optarg); break; case 'T': disktype = optarg; break; case 'b': bsize = strtonum(optarg, MINBSIZE, MAXBSIZE, &errstr); if (errstr) fatal("block size is %s: %s", errstr, optarg); break; case 'c': maxfrgspercg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("fragments per cylinder group is %s: %s", errstr, optarg); break; case 'e': maxbpg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("blocks per file in a cylinder group is" " %s: %s", errstr, optarg); break; case 'f': fsize = strtonum(optarg, MINBSIZE / MAXFRAG, MAXBSIZE, &errstr); if (errstr) fatal("fragment size is %s: %s", errstr, optarg); break; case 'g': avgfilesize = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average file size is %s: %s", errstr, optarg); break; case 'h': avgfilesperdir = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average files per dir is %s: %s", errstr, optarg); break; case 'i': density = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("bytes per inode is %s: %s", errstr, optarg); break; case 'm': minfree = strtonum(optarg, 0, 99, &errstr); if (errstr) fatal("free space %% is %s: %s", errstr, optarg); break; case 'o': if (mfs) getmntopts(optarg, mopts, &mntflags); else { if (strcmp(optarg, "space") == 0) reqopt = opt = FS_OPTSPACE; else if (strcmp(optarg, "time") == 0) reqopt = opt = FS_OPTTIME; else fatal("%s: unknown optimization " "preference: use `space' or `time'.", optarg); } break; case 'q': quiet = 1; break; case 's': if (scan_scaled(optarg, &fssize_input) == -1 || fssize_input <= 0) fatal("file system size invalid: %s", optarg); fssize_usebytes = 0; /* in case of multiple -s */ for (s1 = optarg; *s1 != '\0'; s1++) if (isalpha((unsigned char)*s1)) { fssize_usebytes = 1; break; } break; case 't': fstype = optarg; if (strcmp(fstype, "ffs")) ffsflag = 0; break; #ifdef MFS case 'P': pop = optarg; break; #endif case '?': default: usage(); } if (!ffsflag) break; } argc -= optind; argv += optind; if (ffsflag && argc - mfs != 1) usage(); if (mfs) { /* Increase our data size to the max */ if (getrlimit(RLIMIT_DATA, &rl) == 0) { rl.rlim_cur = rl.rlim_max; (void)setrlimit(RLIMIT_DATA, &rl); } } special = argv[0]; if (!mfs) { char execname[PATH_MAX], name[PATH_MAX]; if (fstype == NULL) fstype = readlabelfs(special, 0); if (fstype != NULL && strcmp(fstype, "ffs")) { snprintf(name, sizeof name, "newfs_%s", fstype); saveargv[0] = name; snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_SBIN, fstype); (void)execv(execname, saveargv); snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_USRSBIN, fstype); (void)execv(execname, saveargv); err(1, "%s not found", name); } } if (mfs && !strcmp(special, "swap")) { /* * it's an MFS, mounted on "swap." fake up a label. * XXX XXX XXX */ fso = -1; /* XXX; normally done below. */ memset(&mfsfakelabel, 0, sizeof(mfsfakelabel)); mfsfakelabel.d_secsize = 512; mfsfakelabel.d_nsectors = 64; mfsfakelabel.d_ntracks = 16; mfsfakelabel.d_ncylinders = 16; mfsfakelabel.d_secpercyl = 1024; DL_SETDSIZE(&mfsfakelabel, 16384); mfsfakelabel.d_npartitions = 1; mfsfakelabel.d_version = 1; DL_SETPSIZE(&mfsfakelabel.d_partitions[0], 16384); mfsfakelabel.d_partitions[0].p_fragblock = DISKLABELV1_FFS_FRAGBLOCK(1024, 8); mfsfakelabel.d_partitions[0].p_cpg = 16; lp = &mfsfakelabel; pp = &mfsfakelabel.d_partitions[0]; goto havelabel; } if (Nflag) { fso = -1; } else { fso = opendev(special, O_WRONLY, 0, &realdev); if (fso < 0) fatal("%s: %s", special, strerror(errno)); special = realdev; /* Bail if target special is mounted */ n = getmntinfo(&mp, MNT_NOWAIT); if (n == 0) fatal("%s: getmntinfo: %s", special, strerror(errno)); len = sizeof(_PATH_DEV) - 1; s1 = special; if (strncmp(_PATH_DEV, s1, len) == 0) s1 += len; while (--n >= 0) { s2 = mp->f_mntfromname; if (strncmp(_PATH_DEV, s2, len) == 0) { s2 += len - 1; *s2 = 'r'; } if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) fatal("%s is mounted on %s", special, mp->f_mntonname); ++mp; } } if (mfs && disktype != NULL) { lp = (struct disklabel *)getdiskbyname(disktype); if (lp == NULL) fatal("%s: unknown disk type", disktype); pp = &lp->d_partitions[1]; } else { fsi = opendev(special, O_RDONLY, 0, NULL); if (fsi < 0) fatal("%s: %s", special, strerror(errno)); if (fstat(fsi, &st) < 0) fatal("%s: %s", special, strerror(errno)); if (!mfs) { if (S_ISBLK(st.st_mode)) fatal("%s: block device", special); if (!S_ISCHR(st.st_mode)) warnx("%s: not a character-special device", special); } if (*argv[0] == '\0') fatal("empty partition name supplied"); cp = argv[0] + strlen(argv[0]) - 1; if ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) && !isdigit((unsigned char)*cp)) fatal("%s: can't figure out file system partition", argv[0]); lp = getdisklabel(special, fsi); if (!mfs) { if (pledge("stdio disklabel tty", NULL) == -1) err(1, "pledge"); } if (isdigit((unsigned char)*cp)) pp = &lp->d_partitions[0]; else pp = &lp->d_partitions[*cp - 'a']; if (DL_GETPSIZE(pp) == 0) fatal("%s: `%c' partition is unavailable", argv[0], *cp); if (pp->p_fstype == FS_BOOT) fatal("%s: `%c' partition overlaps boot program", argv[0], *cp); } havelabel: if (sectorsize == 0) { sectorsize = lp->d_secsize; if (sectorsize <= 0) fatal("%s: no default sector size", argv[0]); } if (fssize_usebytes) { nsecs = fssize_input / sectorsize; if (fssize_input % sectorsize != 0) nsecs++; } else if (fssize_input == 0) nsecs = DL_GETPSIZE(pp); else nsecs = fssize_input; if (nsecs > DL_GETPSIZE(pp) && !mfs) fatal("%s: maximum file system size on the `%c' partition is " "%llu sectors", argv[0], *cp, DL_GETPSIZE(pp)); /* Can't use DL_SECTOBLK() because sectorsize may not be from label! */ fssize = nsecs * (sectorsize / DEV_BSIZE); if (oflagset == 0 && fssize >= INT_MAX) Oflag = 2; /* FFS2 */ if (fsize == 0) { fsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); if (fsize <= 0) fsize = MAXIMUM(DFL_FRAGSIZE, lp->d_secsize); } if (bsize == 0) { bsize = DISKLABELV1_FFS_BSIZE(pp->p_fragblock); if (bsize <= 0) bsize = MINIMUM(DFL_BLKSIZE, 8 * fsize); } if (density == 0) density = NFPI * fsize; if (minfree < MINFREE && opt != FS_OPTSPACE && reqopt == -1) { warnx("warning: changing optimization to space " "because minfree is less than %d%%\n", MINFREE); opt = FS_OPTSPACE; } if (maxbpg == 0) { if (Oflag <= 1) maxbpg = MAXBLKPG_FFS1(bsize); else maxbpg = MAXBLKPG_FFS2(bsize); } oldpartition = *pp; #ifdef MFS if (mfs) { if (realpath(argv[1], node) == NULL) err(1, "realpath %s", argv[1]); if (stat(node, &mountpoint) < 0) err(ECANCELED, "stat %s", node); mfsuid = mountpoint.st_uid; mfsgid = mountpoint.st_gid; mfsmode = mountpoint.st_mode & ALLPERMS; } #endif mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid); if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition))) rewritelabel(special, fso, lp); if (!Nflag) close(fso); close(fsi); #ifdef MFS if (mfs) { struct mfs_args args; memset(&args, 0, sizeof(args)); args.base = membase; args.size = fssize * DEV_BSIZE; args.export_info.ex_root = -2; if (mntflags & MNT_RDONLY) args.export_info.ex_flags = MNT_EXRDONLY; switch (pid = fork()) { case -1: err(10, "mfs"); case 0: snprintf(mountfromname, sizeof(mountfromname), "mfs:%d", getpid()); break; default: snprintf(mountfromname, sizeof(mountfromname), "mfs:%d", pid); for (;;) { /* * spin until the mount succeeds * or the child exits */ usleep(1); /* * XXX Here is a race condition: another process * can mount a filesystem which hides our * ramdisk before we see the success. */ if (statfs(node, &sf) < 0) err(ECANCELED, "statfs %s", node); if (!strcmp(sf.f_mntfromname, mountfromname) && !strncmp(sf.f_mntonname, node, MNAMELEN) && !strcmp(sf.f_fstypename, "mfs")) { if (pop != NULL) copy(pop, node, &args); exit(0); } res = waitpid(pid, &status, WNOHANG); if (res == -1) err(EDEADLK, "waitpid"); if (res != pid) continue; if (WIFEXITED(status)) { if (WEXITSTATUS(status) == 0) exit(0); errx(1, "%s: mount: %s", node, strerror(WEXITSTATUS(status))); } else errx(EDEADLK, "abnormal termination"); } /* NOTREACHED */ } (void) setsid(); (void) close(0); (void) close(1); (void) close(2); (void) chdir("/"); args.fspec = mountfromname; if (mntflags & MNT_RDONLY && pop != NULL) mntflags &= ~MNT_RDONLY; if (mount(MOUNT_MFS, node, mntflags, &args) < 0) exit(errno); /* parent prints message */ } #endif exit(0); }