miscbltin.c revision 201053
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 201053 2009-12-27 18:04:05Z 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; 1771556Srgrimes if (c != '\n') 1781556Srgrimes STPUTC(c, p); 1791556Srgrimes continue; 1801556Srgrimes } 18150394Stg if (!rflag && c == '\\') { 1821556Srgrimes backslash++; 1831556Srgrimes continue; 1841556Srgrimes } 1851556Srgrimes if (c == '\n') 1861556Srgrimes break; 187190295Sstefanf if (strchr(ifs, c)) 188190295Sstefanf is_ifs = strchr(" \t\n", c) ? 1 : 2; 189190295Sstefanf else 190190295Sstefanf is_ifs = 0; 191190295Sstefanf 192190295Sstefanf if (startword != 0) { 193190295Sstefanf if (is_ifs == 1) { 194190295Sstefanf /* Ignore leading IFS whitespace */ 195190295Sstefanf if (saveall) 196190295Sstefanf STPUTC(c, p); 197190295Sstefanf continue; 198190295Sstefanf } 199190295Sstefanf if (is_ifs == 2 && startword == 1) { 200190295Sstefanf /* Only one non-whitespace IFS per word */ 201190295Sstefanf startword = 2; 202190295Sstefanf if (saveall) 203190295Sstefanf STPUTC(c, p); 204190295Sstefanf continue; 205190295Sstefanf } 206190295Sstefanf } 207190295Sstefanf 208190295Sstefanf if (is_ifs == 0) { 209190295Sstefanf /* append this character to the current variable */ 210190295Sstefanf startword = 0; 211190295Sstefanf if (saveall) 212190295Sstefanf /* Not just a spare terminator */ 213190295Sstefanf saveall++; 214190295Sstefanf STPUTC(c, p); 2151556Srgrimes continue; 2161556Srgrimes } 217190295Sstefanf 218190295Sstefanf /* end of variable... */ 219190295Sstefanf startword = is_ifs; 220190295Sstefanf 221190295Sstefanf if (ap[1] == NULL) { 222190295Sstefanf /* Last variable needs all IFS chars */ 223190295Sstefanf saveall++; 2241556Srgrimes STPUTC(c, p); 225190295Sstefanf continue; 2261556Srgrimes } 227190295Sstefanf 228190295Sstefanf STACKSTRNUL(p); 229190295Sstefanf setvar(*ap, stackblock(), 0); 230190295Sstefanf ap++; 231190295Sstefanf STARTSTACKSTR(p); 2321556Srgrimes } 2331556Srgrimes STACKSTRNUL(p); 234190295Sstefanf 235190295Sstefanf /* Remove trailing IFS chars */ 236190295Sstefanf for (; stackblock() <= --p; *p = 0) { 237190295Sstefanf if (!strchr(ifs, *p)) 238190295Sstefanf break; 239190295Sstefanf if (strchr(" \t\n", *p)) 240190295Sstefanf /* Always remove whitespace */ 241190295Sstefanf continue; 242190295Sstefanf if (saveall > 1) 243190295Sstefanf /* Don't remove non-whitespace unless it was naked */ 244190295Sstefanf break; 245190295Sstefanf } 2461556Srgrimes setvar(*ap, stackblock(), 0); 247190295Sstefanf 248190295Sstefanf /* Set any remaining args to "" */ 2491556Srgrimes while (*++ap != NULL) 2501556Srgrimes setvar(*ap, nullstr, 0); 2511556Srgrimes return status; 2521556Srgrimes} 2531556Srgrimes 2541556Srgrimes 2551556Srgrimes 25617987Speterint 257201053Sjillesumaskcmd(int argc __unused, char **argv __unused) 25817987Speter{ 25917987Speter char *ap; 2601556Srgrimes int mask; 2611556Srgrimes int i; 26217987Speter int symbolic_mode = 0; 2631556Srgrimes 26417987Speter while ((i = nextopt("S")) != '\0') { 26517987Speter symbolic_mode = 1; 2661556Srgrimes } 26711571Sjoerg 26817987Speter INTOFF; 26917987Speter mask = umask(0); 27017987Speter umask(mask); 27117987Speter INTON; 27211571Sjoerg 27317987Speter if ((ap = *argptr) == NULL) { 27417987Speter if (symbolic_mode) { 27517987Speter char u[4], g[4], o[4]; 27611571Sjoerg 27717987Speter i = 0; 27817987Speter if ((mask & S_IRUSR) == 0) 27917987Speter u[i++] = 'r'; 28017987Speter if ((mask & S_IWUSR) == 0) 28117987Speter u[i++] = 'w'; 28217987Speter if ((mask & S_IXUSR) == 0) 28317987Speter u[i++] = 'x'; 28417987Speter u[i] = '\0'; 28511571Sjoerg 28617987Speter i = 0; 28717987Speter if ((mask & S_IRGRP) == 0) 28817987Speter g[i++] = 'r'; 28917987Speter if ((mask & S_IWGRP) == 0) 29017987Speter g[i++] = 'w'; 29117987Speter if ((mask & S_IXGRP) == 0) 29217987Speter g[i++] = 'x'; 29317987Speter g[i] = '\0'; 29411571Sjoerg 29517987Speter i = 0; 29617987Speter if ((mask & S_IROTH) == 0) 29717987Speter o[i++] = 'r'; 29817987Speter if ((mask & S_IWOTH) == 0) 29917987Speter o[i++] = 'w'; 30017987Speter if ((mask & S_IXOTH) == 0) 30117987Speter o[i++] = 'x'; 30217987Speter o[i] = '\0'; 30311571Sjoerg 30417987Speter out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 30517987Speter } else { 30617987Speter out1fmt("%.4o\n", mask); 30717987Speter } 30817987Speter } else { 30917987Speter if (isdigit(*ap)) { 31017987Speter mask = 0; 31117987Speter do { 31217987Speter if (*ap >= '8' || *ap < '0') 313149918Sstefanf error("Illegal number: %s", *argptr); 31417987Speter mask = (mask << 3) + (*ap - '0'); 31517987Speter } while (*++ap != '\0'); 31617987Speter umask(mask); 31717987Speter } else { 31820425Ssteve void *set; 319151795Sstefanf INTOFF; 32017987Speter if ((set = setmode (ap)) == 0) 32141844Simp error("Illegal number: %s", ap); 32211571Sjoerg 32317987Speter mask = getmode (set, ~mask & 0777); 32417987Speter umask(~mask & 0777); 32541844Simp free(set); 326151795Sstefanf INTON; 32717987Speter } 32817987Speter } 32911571Sjoerg return 0; 33011571Sjoerg} 33111571Sjoerg 33217987Speter/* 33317987Speter * ulimit builtin 33417987Speter * 33517987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 33617987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 33717987Speter * ash by J.T. Conklin. 33817987Speter * 33917987Speter * Public domain. 34017987Speter */ 34111571Sjoerg 34217987Speterstruct limits { 34317987Speter const char *name; 34418016Speter const char *units; 34517987Speter int cmd; 34617987Speter int factor; /* multiply by to get rlim_{cur,max} values */ 34717987Speter char option; 34817987Speter}; 34911571Sjoerg 35017987Speterstatic const struct limits limits[] = { 35117987Speter#ifdef RLIMIT_CPU 35218016Speter { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 35317987Speter#endif 35417987Speter#ifdef RLIMIT_FSIZE 35518016Speter { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 35617987Speter#endif 35717987Speter#ifdef RLIMIT_DATA 35818016Speter { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 35917987Speter#endif 36017987Speter#ifdef RLIMIT_STACK 36118016Speter { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 36217987Speter#endif 36317987Speter#ifdef RLIMIT_CORE 36418016Speter { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 36517987Speter#endif 36617987Speter#ifdef RLIMIT_RSS 36718016Speter { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 36817987Speter#endif 36917987Speter#ifdef RLIMIT_MEMLOCK 37018016Speter { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 37117987Speter#endif 37217987Speter#ifdef RLIMIT_NPROC 37318016Speter { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 37417987Speter#endif 37517987Speter#ifdef RLIMIT_NOFILE 37618016Speter { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 37717987Speter#endif 37817987Speter#ifdef RLIMIT_VMEM 37918016Speter { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 38017987Speter#endif 38117987Speter#ifdef RLIMIT_SWAP 38218016Speter { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 38317987Speter#endif 38452072Sgreen#ifdef RLIMIT_SBSIZE 38552072Sgreen { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' }, 38652072Sgreen#endif 387181905Sed#ifdef RLIMIT_NPTS 388181905Sed { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' }, 389181905Sed#endif 39018016Speter { (char *) 0, (char *)0, 0, 0, '\0' } 39117987Speter}; 39217987Speter 39317987Speterint 39490111Simpulimitcmd(int argc __unused, char **argv __unused) 39517987Speter{ 39625222Ssteve int c; 397104282Smux rlim_t val = 0; 39817987Speter enum { SOFT = 0x1, HARD = 0x2 } 39917987Speter how = SOFT | HARD; 40017987Speter const struct limits *l; 40117987Speter int set, all = 0; 40217987Speter int optc, what; 40317987Speter struct rlimit limit; 40411571Sjoerg 40517987Speter what = 'f'; 406194767Skib while ((optc = nextopt("HSatfdsmcnuvlbpw")) != '\0') 40717987Speter switch (optc) { 40811571Sjoerg case 'H': 40917987Speter how = HARD; 41011571Sjoerg break; 41111571Sjoerg case 'S': 41217987Speter how = SOFT; 41311571Sjoerg break; 41411571Sjoerg case 'a': 41517987Speter all = 1; 41611571Sjoerg break; 41717987Speter default: 41817987Speter what = optc; 41911571Sjoerg } 42011571Sjoerg 42117987Speter for (l = limits; l->name && l->option != what; l++) 42217987Speter ; 42317987Speter if (!l->name) 424104208Stjr error("internal error (%c)", what); 42517987Speter 42617987Speter set = *argptr ? 1 : 0; 42717987Speter if (set) { 42817987Speter char *p = *argptr; 42917987Speter 43017987Speter if (all || argptr[1]) 431104208Stjr error("too many arguments"); 43217987Speter if (strcmp(p, "unlimited") == 0) 43311571Sjoerg val = RLIM_INFINITY; 43411571Sjoerg else { 435104282Smux val = 0; 43617987Speter 43717987Speter while ((c = *p++) >= '0' && c <= '9') 43817987Speter { 43917987Speter val = (val * 10) + (long)(c - '0'); 440104282Smux if (val < 0) 44117987Speter break; 44217987Speter } 44317987Speter if (c) 444104208Stjr error("bad number"); 44517987Speter val *= l->factor; 44611571Sjoerg } 44717987Speter } 44817987Speter if (all) { 449155301Sschweikh for (l = limits; l->name; l++) { 45018016Speter char optbuf[40]; 45118016Speter if (getrlimit(l->cmd, &limit) < 0) 452104208Stjr error("can't get limit: %s", strerror(errno)); 45317987Speter if (how & SOFT) 45417987Speter val = limit.rlim_cur; 45517987Speter else if (how & HARD) 45617987Speter val = limit.rlim_max; 45717987Speter 45818016Speter if (l->units) 45918016Speter snprintf(optbuf, sizeof(optbuf), 46018019Speter "(%s, -%c) ", l->units, l->option); 46118016Speter else 46218016Speter snprintf(optbuf, sizeof(optbuf), 46318019Speter "(-%c) ", l->option); 46418019Speter out1fmt("%-18s %18s ", l->name, optbuf); 46517987Speter if (val == RLIM_INFINITY) 46617987Speter out1fmt("unlimited\n"); 46717987Speter else 46817987Speter { 46917987Speter val /= l->factor; 470104282Smux out1fmt("%jd\n", (intmax_t)val); 47117987Speter } 47211571Sjoerg } 47317987Speter return 0; 47411571Sjoerg } 47517987Speter 47618016Speter if (getrlimit(l->cmd, &limit) < 0) 477104208Stjr error("can't get limit: %s", strerror(errno)); 47817987Speter if (set) { 47917987Speter if (how & SOFT) 48017987Speter limit.rlim_cur = val; 48117987Speter if (how & HARD) 48217987Speter limit.rlim_max = val; 48317987Speter if (setrlimit(l->cmd, &limit) < 0) 484104208Stjr error("bad limit: %s", strerror(errno)); 48517987Speter } else { 48617987Speter if (how & SOFT) 48717987Speter val = limit.rlim_cur; 48817987Speter else if (how & HARD) 48917987Speter val = limit.rlim_max; 49017987Speter 49117987Speter if (val == RLIM_INFINITY) 49217987Speter out1fmt("unlimited\n"); 49317987Speter else 49417987Speter { 49517987Speter val /= l->factor; 496104282Smux out1fmt("%jd\n", (intmax_t)val); 49717987Speter } 49817987Speter } 49911571Sjoerg return 0; 50011571Sjoerg} 501