miscbltin.c revision 212339
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/miscbltin.c 212339 2010-09-08 20:35:43Z jilles $"); 401556Srgrimes 411556Srgrimes/* 4246684Skris * Miscellaneous builtins. 431556Srgrimes */ 441556Srgrimes 4517987Speter#include <sys/types.h> 4617987Speter#include <sys/stat.h> 4717987Speter#include <sys/time.h> 4817987Speter#include <sys/resource.h> 4917987Speter#include <unistd.h> 5017987Speter#include <ctype.h> 5118016Speter#include <errno.h> 52104282Smux#include <stdint.h> 5318018Speter#include <stdio.h> 5438536Scracauer#include <stdlib.h> 5529983Smsmith#include <termios.h> 5617987Speter 571556Srgrimes#include "shell.h" 581556Srgrimes#include "options.h" 591556Srgrimes#include "var.h" 601556Srgrimes#include "output.h" 611556Srgrimes#include "memalloc.h" 621556Srgrimes#include "error.h" 631556Srgrimes#include "mystring.h" 641556Srgrimes 651556Srgrimes#undef eflag 661556Srgrimes 67149018Sstefanfint readcmd(int, char **); 68149018Sstefanfint umaskcmd(int, char **); 69149018Sstefanfint ulimitcmd(int, char **); 70149018Sstefanf 711556Srgrimes/* 7250394Stg * The read builtin. The -r option causes backslashes to be treated like 7350394Stg * ordinary characters. 741556Srgrimes * 751556Srgrimes * This uses unbuffered input, which may be avoidable in some cases. 76190295Sstefanf * 77190295Sstefanf * Note that if IFS=' :' then read x y should work so that: 78190295Sstefanf * 'a b' x='a', y='b' 79190295Sstefanf * ' a b ' x='a', y='b' 80190295Sstefanf * ':b' x='', y='b' 81190295Sstefanf * ':' x='', y='' 82190295Sstefanf * '::' x='', y='' 83190295Sstefanf * ': :' x='', y='' 84190295Sstefanf * ':::' x='', y='::' 85190295Sstefanf * ':b c:' x='', y='b c:' 861556Srgrimes */ 871556Srgrimes 8817987Speterint 8990111Simpreadcmd(int argc __unused, char **argv __unused) 9017987Speter{ 911556Srgrimes char **ap; 921556Srgrimes int backslash; 931556Srgrimes char c; 9450394Stg int rflag; 951556Srgrimes char *prompt; 96201053Sjilles const char *ifs; 971556Srgrimes char *p; 981556Srgrimes int startword; 991556Srgrimes int status; 1001556Srgrimes int i; 101190295Sstefanf int is_ifs; 102190295Sstefanf int saveall = 0; 10329983Smsmith struct timeval tv; 10429983Smsmith char *tvptr; 10529983Smsmith fd_set ifds; 1061556Srgrimes 10750394Stg rflag = 0; 1081556Srgrimes prompt = NULL; 10929983Smsmith tv.tv_sec = -1; 11029983Smsmith tv.tv_usec = 0; 11150394Stg while ((i = nextopt("erp:t:")) != '\0') { 11229983Smsmith switch(i) { 11329983Smsmith case 'p': 11459436Scracauer prompt = shoptarg; 11529983Smsmith break; 11629983Smsmith case 'e': 11729983Smsmith break; 11850394Stg case 'r': 11950394Stg rflag = 1; 12050394Stg break; 12129983Smsmith case 't': 12259436Scracauer tv.tv_sec = strtol(shoptarg, &tvptr, 0); 12359436Scracauer if (tvptr == shoptarg) 12429983Smsmith error("timeout value"); 12529983Smsmith switch(*tvptr) { 12629983Smsmith case 0: 12729983Smsmith case 's': 12829983Smsmith break; 12929983Smsmith case 'h': 13029983Smsmith tv.tv_sec *= 60; 13129983Smsmith /* FALLTHROUGH */ 13229983Smsmith case 'm': 13329983Smsmith tv.tv_sec *= 60; 13429983Smsmith break; 13529983Smsmith default: 13629983Smsmith error("timeout unit"); 13729983Smsmith } 13829983Smsmith break; 13929983Smsmith } 1401556Srgrimes } 1411556Srgrimes if (prompt && isatty(0)) { 1421556Srgrimes out2str(prompt); 1431556Srgrimes flushall(); 1441556Srgrimes } 1451556Srgrimes if (*(ap = argptr) == NULL) 1461556Srgrimes error("arg count"); 1471556Srgrimes if ((ifs = bltinlookup("IFS", 1)) == NULL) 148190298Sstefanf ifs = " \t\n"; 14929983Smsmith 15029983Smsmith if (tv.tv_sec >= 0) { 15129983Smsmith /* 15229983Smsmith * Wait for something to become available. 15329983Smsmith */ 15429983Smsmith FD_ZERO(&ifds); 15529983Smsmith FD_SET(0, &ifds); 15629983Smsmith status = select(1, &ifds, NULL, NULL, &tv); 15729983Smsmith /* 15829983Smsmith * If there's nothing ready, return an error. 15929983Smsmith */ 16029983Smsmith if (status <= 0) 16129983Smsmith return(1); 16229983Smsmith } 16329983Smsmith 1641556Srgrimes status = 0; 165190295Sstefanf startword = 2; 1661556Srgrimes backslash = 0; 1671556Srgrimes STARTSTACKSTR(p); 1681556Srgrimes for (;;) { 16980381Ssheldonh if (read(STDIN_FILENO, &c, 1) != 1) { 1701556Srgrimes status = 1; 1711556Srgrimes break; 1721556Srgrimes } 1731556Srgrimes if (c == '\0') 1741556Srgrimes continue; 1751556Srgrimes if (backslash) { 1761556Srgrimes backslash = 0; 177212339Sjilles startword = 0; 1781556Srgrimes if (c != '\n') 1791556Srgrimes STPUTC(c, p); 1801556Srgrimes continue; 1811556Srgrimes } 18250394Stg if (!rflag && c == '\\') { 1831556Srgrimes backslash++; 1841556Srgrimes continue; 1851556Srgrimes } 1861556Srgrimes if (c == '\n') 1871556Srgrimes break; 188190295Sstefanf if (strchr(ifs, c)) 189190295Sstefanf is_ifs = strchr(" \t\n", c) ? 1 : 2; 190190295Sstefanf else 191190295Sstefanf is_ifs = 0; 192190295Sstefanf 193190295Sstefanf if (startword != 0) { 194190295Sstefanf if (is_ifs == 1) { 195190295Sstefanf /* Ignore leading IFS whitespace */ 196190295Sstefanf if (saveall) 197190295Sstefanf STPUTC(c, p); 198190295Sstefanf continue; 199190295Sstefanf } 200190295Sstefanf if (is_ifs == 2 && startword == 1) { 201190295Sstefanf /* Only one non-whitespace IFS per word */ 202190295Sstefanf startword = 2; 203190295Sstefanf if (saveall) 204190295Sstefanf STPUTC(c, p); 205190295Sstefanf continue; 206190295Sstefanf } 207190295Sstefanf } 208190295Sstefanf 209190295Sstefanf if (is_ifs == 0) { 210190295Sstefanf /* append this character to the current variable */ 211190295Sstefanf startword = 0; 212190295Sstefanf if (saveall) 213190295Sstefanf /* Not just a spare terminator */ 214190295Sstefanf saveall++; 215190295Sstefanf STPUTC(c, p); 2161556Srgrimes continue; 2171556Srgrimes } 218190295Sstefanf 219190295Sstefanf /* end of variable... */ 220190295Sstefanf startword = is_ifs; 221190295Sstefanf 222190295Sstefanf if (ap[1] == NULL) { 223190295Sstefanf /* Last variable needs all IFS chars */ 224190295Sstefanf saveall++; 2251556Srgrimes STPUTC(c, p); 226190295Sstefanf continue; 2271556Srgrimes } 228190295Sstefanf 229190295Sstefanf STACKSTRNUL(p); 230190295Sstefanf setvar(*ap, stackblock(), 0); 231190295Sstefanf ap++; 232190295Sstefanf STARTSTACKSTR(p); 2331556Srgrimes } 2341556Srgrimes STACKSTRNUL(p); 235190295Sstefanf 236190295Sstefanf /* Remove trailing IFS chars */ 237190295Sstefanf for (; stackblock() <= --p; *p = 0) { 238190295Sstefanf if (!strchr(ifs, *p)) 239190295Sstefanf break; 240190295Sstefanf if (strchr(" \t\n", *p)) 241190295Sstefanf /* Always remove whitespace */ 242190295Sstefanf continue; 243190295Sstefanf if (saveall > 1) 244190295Sstefanf /* Don't remove non-whitespace unless it was naked */ 245190295Sstefanf break; 246190295Sstefanf } 2471556Srgrimes setvar(*ap, stackblock(), 0); 248190295Sstefanf 249190295Sstefanf /* Set any remaining args to "" */ 2501556Srgrimes while (*++ap != NULL) 2511556Srgrimes setvar(*ap, nullstr, 0); 2521556Srgrimes return status; 2531556Srgrimes} 2541556Srgrimes 2551556Srgrimes 2561556Srgrimes 25717987Speterint 258201053Sjillesumaskcmd(int argc __unused, char **argv __unused) 25917987Speter{ 26017987Speter char *ap; 2611556Srgrimes int mask; 2621556Srgrimes int i; 26317987Speter int symbolic_mode = 0; 2641556Srgrimes 26517987Speter while ((i = nextopt("S")) != '\0') { 26617987Speter symbolic_mode = 1; 2671556Srgrimes } 26811571Sjoerg 26917987Speter INTOFF; 27017987Speter mask = umask(0); 27117987Speter umask(mask); 27217987Speter INTON; 27311571Sjoerg 27417987Speter if ((ap = *argptr) == NULL) { 27517987Speter if (symbolic_mode) { 27617987Speter char u[4], g[4], o[4]; 27711571Sjoerg 27817987Speter i = 0; 27917987Speter if ((mask & S_IRUSR) == 0) 28017987Speter u[i++] = 'r'; 28117987Speter if ((mask & S_IWUSR) == 0) 28217987Speter u[i++] = 'w'; 28317987Speter if ((mask & S_IXUSR) == 0) 28417987Speter u[i++] = 'x'; 28517987Speter u[i] = '\0'; 28611571Sjoerg 28717987Speter i = 0; 28817987Speter if ((mask & S_IRGRP) == 0) 28917987Speter g[i++] = 'r'; 29017987Speter if ((mask & S_IWGRP) == 0) 29117987Speter g[i++] = 'w'; 29217987Speter if ((mask & S_IXGRP) == 0) 29317987Speter g[i++] = 'x'; 29417987Speter g[i] = '\0'; 29511571Sjoerg 29617987Speter i = 0; 29717987Speter if ((mask & S_IROTH) == 0) 29817987Speter o[i++] = 'r'; 29917987Speter if ((mask & S_IWOTH) == 0) 30017987Speter o[i++] = 'w'; 30117987Speter if ((mask & S_IXOTH) == 0) 30217987Speter o[i++] = 'x'; 30317987Speter o[i] = '\0'; 30411571Sjoerg 30517987Speter out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 30617987Speter } else { 30717987Speter out1fmt("%.4o\n", mask); 30817987Speter } 30917987Speter } else { 31017987Speter if (isdigit(*ap)) { 31117987Speter mask = 0; 31217987Speter do { 31317987Speter if (*ap >= '8' || *ap < '0') 314149918Sstefanf error("Illegal number: %s", *argptr); 31517987Speter mask = (mask << 3) + (*ap - '0'); 31617987Speter } while (*++ap != '\0'); 31717987Speter umask(mask); 31817987Speter } else { 31920425Ssteve void *set; 320151795Sstefanf INTOFF; 32117987Speter if ((set = setmode (ap)) == 0) 32241844Simp error("Illegal number: %s", ap); 32311571Sjoerg 32417987Speter mask = getmode (set, ~mask & 0777); 32517987Speter umask(~mask & 0777); 32641844Simp free(set); 327151795Sstefanf INTON; 32817987Speter } 32917987Speter } 33011571Sjoerg return 0; 33111571Sjoerg} 33211571Sjoerg 33317987Speter/* 33417987Speter * ulimit builtin 33517987Speter * 33617987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 33717987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 33817987Speter * ash by J.T. Conklin. 33917987Speter * 34017987Speter * Public domain. 34117987Speter */ 34211571Sjoerg 34317987Speterstruct limits { 34417987Speter const char *name; 34518016Speter const char *units; 34617987Speter int cmd; 34717987Speter int factor; /* multiply by to get rlim_{cur,max} values */ 34817987Speter char option; 34917987Speter}; 35011571Sjoerg 35117987Speterstatic const struct limits limits[] = { 35217987Speter#ifdef RLIMIT_CPU 35318016Speter { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 35417987Speter#endif 35517987Speter#ifdef RLIMIT_FSIZE 35618016Speter { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 35717987Speter#endif 35817987Speter#ifdef RLIMIT_DATA 35918016Speter { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 36017987Speter#endif 36117987Speter#ifdef RLIMIT_STACK 36218016Speter { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 36317987Speter#endif 36417987Speter#ifdef RLIMIT_CORE 36518016Speter { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 36617987Speter#endif 36717987Speter#ifdef RLIMIT_RSS 36818016Speter { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 36917987Speter#endif 37017987Speter#ifdef RLIMIT_MEMLOCK 37118016Speter { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 37217987Speter#endif 37317987Speter#ifdef RLIMIT_NPROC 37418016Speter { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 37517987Speter#endif 37617987Speter#ifdef RLIMIT_NOFILE 37718016Speter { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 37817987Speter#endif 37917987Speter#ifdef RLIMIT_VMEM 38018016Speter { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 38117987Speter#endif 38217987Speter#ifdef RLIMIT_SWAP 38318016Speter { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 38417987Speter#endif 38552072Sgreen#ifdef RLIMIT_SBSIZE 38652072Sgreen { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' }, 38752072Sgreen#endif 388181905Sed#ifdef RLIMIT_NPTS 389181905Sed { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' }, 390181905Sed#endif 39118016Speter { (char *) 0, (char *)0, 0, 0, '\0' } 39217987Speter}; 39317987Speter 39417987Speterint 39590111Simpulimitcmd(int argc __unused, char **argv __unused) 39617987Speter{ 39725222Ssteve int c; 398104282Smux rlim_t val = 0; 39917987Speter enum { SOFT = 0x1, HARD = 0x2 } 40017987Speter how = SOFT | HARD; 40117987Speter const struct limits *l; 40217987Speter int set, all = 0; 40317987Speter int optc, what; 40417987Speter struct rlimit limit; 40511571Sjoerg 40617987Speter what = 'f'; 407194767Skib while ((optc = nextopt("HSatfdsmcnuvlbpw")) != '\0') 40817987Speter switch (optc) { 40911571Sjoerg case 'H': 41017987Speter how = HARD; 41111571Sjoerg break; 41211571Sjoerg case 'S': 41317987Speter how = SOFT; 41411571Sjoerg break; 41511571Sjoerg case 'a': 41617987Speter all = 1; 41711571Sjoerg break; 41817987Speter default: 41917987Speter what = optc; 42011571Sjoerg } 42111571Sjoerg 42217987Speter for (l = limits; l->name && l->option != what; l++) 42317987Speter ; 42417987Speter if (!l->name) 425104208Stjr error("internal error (%c)", what); 42617987Speter 42717987Speter set = *argptr ? 1 : 0; 42817987Speter if (set) { 42917987Speter char *p = *argptr; 43017987Speter 43117987Speter if (all || argptr[1]) 432104208Stjr error("too many arguments"); 43317987Speter if (strcmp(p, "unlimited") == 0) 43411571Sjoerg val = RLIM_INFINITY; 43511571Sjoerg else { 436104282Smux val = 0; 43717987Speter 43817987Speter while ((c = *p++) >= '0' && c <= '9') 43917987Speter { 44017987Speter val = (val * 10) + (long)(c - '0'); 441104282Smux if (val < 0) 44217987Speter break; 44317987Speter } 44417987Speter if (c) 445104208Stjr error("bad number"); 44617987Speter val *= l->factor; 44711571Sjoerg } 44817987Speter } 44917987Speter if (all) { 450155301Sschweikh for (l = limits; l->name; l++) { 45118016Speter char optbuf[40]; 45218016Speter if (getrlimit(l->cmd, &limit) < 0) 453104208Stjr error("can't get limit: %s", strerror(errno)); 45417987Speter if (how & SOFT) 45517987Speter val = limit.rlim_cur; 45617987Speter else if (how & HARD) 45717987Speter val = limit.rlim_max; 45817987Speter 45918016Speter if (l->units) 46018016Speter snprintf(optbuf, sizeof(optbuf), 46118019Speter "(%s, -%c) ", l->units, l->option); 46218016Speter else 46318016Speter snprintf(optbuf, sizeof(optbuf), 46418019Speter "(-%c) ", l->option); 46518019Speter out1fmt("%-18s %18s ", l->name, optbuf); 46617987Speter if (val == RLIM_INFINITY) 46717987Speter out1fmt("unlimited\n"); 46817987Speter else 46917987Speter { 47017987Speter val /= l->factor; 471104282Smux out1fmt("%jd\n", (intmax_t)val); 47217987Speter } 47311571Sjoerg } 47417987Speter return 0; 47511571Sjoerg } 47617987Speter 47718016Speter if (getrlimit(l->cmd, &limit) < 0) 478104208Stjr error("can't get limit: %s", strerror(errno)); 47917987Speter if (set) { 48017987Speter if (how & SOFT) 48117987Speter limit.rlim_cur = val; 48217987Speter if (how & HARD) 48317987Speter limit.rlim_max = val; 48417987Speter if (setrlimit(l->cmd, &limit) < 0) 485104208Stjr error("bad limit: %s", strerror(errno)); 48617987Speter } else { 48717987Speter if (how & SOFT) 48817987Speter val = limit.rlim_cur; 48917987Speter else if (how & HARD) 49017987Speter val = limit.rlim_max; 49117987Speter 49217987Speter if (val == RLIM_INFINITY) 49317987Speter out1fmt("unlimited\n"); 49417987Speter else 49517987Speter { 49617987Speter val /= l->factor; 497104282Smux out1fmt("%jd\n", (intmax_t)val); 49817987Speter } 49917987Speter } 50011571Sjoerg return 0; 50111571Sjoerg} 502