zsh-workers
 help / color / mirror / code / Atom feed
69174fe8ef373dce12027e9a317372f1158018d8 blob 26794 bytes (raw)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
 
/*
 * rlimits.c - resource limit builtins
 *
 * This file is part of zsh, the Z shell.
 *
 * Copyright (c) 1992-1997 Paul Falstad
 * All rights reserved.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and to distribute modified versions of this software for any
 * purpose, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * In no event shall Paul Falstad or the Zsh Development Group be liable
 * to any party for direct, indirect, special, incidental, or consequential
 * damages arising out of the use of this software and its documentation,
 * even if Paul Falstad and the Zsh Development Group have been advised of
 * the possibility of such damage.
 *
 * Paul Falstad and the Zsh Development Group specifically disclaim any
 * warranties, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose.  The software
 * provided hereunder is on an "as is" basis, and Paul Falstad and the
 * Zsh Development Group have no obligation to provide maintenance,
 * support, updates, enhancements, or modifications.
 *
 */

#include "rlimits.mdh"
#include "rlimits.pro"

#if defined(HAVE_GETRLIMIT) && defined(RLIM_INFINITY)

enum zlimtype {
    ZLIMTYPE_MEMORY,
    ZLIMTYPE_NUMBER,
    ZLIMTYPE_TIME,
    ZLIMTYPE_MICROSECONDS,
    ZLIMTYPE_UNKNOWN
};

typedef struct resinfo_T {
    int	res;		/* RLIMIT_XXX */
    char* name;		/* used by limit builtin */
    enum zlimtype type;
    int unit;		/* 1, 512, or 1024 */
    char opt;		/* option character */
    char* descr;	/* used by ulimit builtin */
} resinfo_T;

/* table of known resources */
/*
 * How to add a new resource:
 * 1. Add zsh_LIMIT_PRESENT(RLIMIT_XXX) in configure.ac.
 * 2. Add an entry for RLIMIT_XXX to known_resources[].
 *    Make sure the option letter (resinto_T.opt) is unique.
 * 3. Add entry in documentation for the limit and ulimit builtins
 * 4. Build zsh and run the test B12rlimit.ztst.
 */
static const resinfo_T known_resources[] = {
    {RLIMIT_CPU, "cputime", ZLIMTYPE_TIME, 1,
		't', "cpu time (seconds)"},
    {RLIMIT_FSIZE, "filesize", ZLIMTYPE_MEMORY, 512,
		'f', "file size (blocks)"},
    {RLIMIT_DATA, "datasize", ZLIMTYPE_MEMORY, 1024,
		'd', "data seg size (kbytes)"},
    {RLIMIT_STACK, "stacksize", ZLIMTYPE_MEMORY, 1024,
		's', "stack size (kbytes)"},
    {RLIMIT_CORE, "coredumpsize", ZLIMTYPE_MEMORY, 512,
		'c', "core file size (blocks)"},
# ifdef HAVE_RLIMIT_NOFILE
    {RLIMIT_NOFILE, "descriptors", ZLIMTYPE_NUMBER, 1,
		'n', "file descriptors"},
# endif
# if defined(HAVE_RLIMIT_AS) && !defined(RLIMIT_VMEM_IS_AS)
    {RLIMIT_AS, "addressspace", ZLIMTYPE_MEMORY, 1024,
		'v', "address space (kbytes)"},
# endif
# if defined(HAVE_RLIMIT_RSS) && !defined(RLIMIT_VMEM_IS_RSS) && !defined(RLIMIT_RSS_IS_AS)
    {RLIMIT_RSS, "resident", ZLIMTYPE_MEMORY, 1024,
		'm', "resident set size (kbytes)"},
# endif
# if defined(HAVE_RLIMIT_VMEM)
    {RLIMIT_VMEM,
#  if defined(RLIMIT_VMEM_IS_RSS)
		 "resident", ZLIMTYPE_MEMORY, 1024,
		 'm', "memory size (kbytes)"
#  else
		 "vmemorysize", ZLIMTYPE_MEMORY, 1024,
		 'v', "virtual memory size (kbytes)"
#  endif
    },
# endif
# ifdef HAVE_RLIMIT_NPROC
    {RLIMIT_NPROC, "maxproc", ZLIMTYPE_NUMBER, 1,
		'u', "processes"},
# endif
# ifdef HAVE_RLIMIT_MEMLOCK
    {RLIMIT_MEMLOCK, "memorylocked", ZLIMTYPE_MEMORY, 1024,
		'l', "locked-in-memory size (kbytes)"},
# endif
    /* Linux */
# ifdef HAVE_RLIMIT_LOCKS
    {RLIMIT_LOCKS, "maxfilelocks", ZLIMTYPE_NUMBER, 1,
		'x', "file locks"},
# endif
# ifdef HAVE_RLIMIT_SIGPENDING
    {RLIMIT_SIGPENDING, "sigpending", ZLIMTYPE_NUMBER, 1,
		'i', "pending signals"},
# endif
# ifdef HAVE_RLIMIT_MSGQUEUE
    {RLIMIT_MSGQUEUE, "msgqueue", ZLIMTYPE_MEMORY, 1,
		'q', "bytes in POSIX msg queues"},
# endif
# ifdef HAVE_RLIMIT_NICE
    /*
     * Not max niceness. On Linux ranges [1..40] for that RLIM translates
     * to [-19..20] meaning that for instance setting it to 20 means
     * processes can lower their niceness as far down as -1 and with the
     * default of 0, unprivileged processes can't lower their niceness.
     *
     * Increasing niceness is always possible.
     *
     * "max nice priority" is the wording used in /proc/self/limits on
     * Linux.
     */
    {RLIMIT_NICE, "nice", ZLIMTYPE_NUMBER, 1,
		'e', "max nice priority"},
# endif
# ifdef HAVE_RLIMIT_RTPRIO
    {RLIMIT_RTPRIO, "rt_priority", ZLIMTYPE_NUMBER, 1,
		'r', "max rt priority"},
# endif
# ifdef HAVE_RLIMIT_RTTIME
    {RLIMIT_RTTIME, "rt_time", ZLIMTYPE_MICROSECONDS, 1,
		'R', "rt cpu time slice (microseconds)"},
# endif
    /* BSD */
# ifdef HAVE_RLIMIT_SBSIZE
    {RLIMIT_SBSIZE, "sockbufsize", ZLIMTYPE_MEMORY, 1,
		'b', "socket buffer size (bytes)"},
# endif
# ifdef HAVE_RLIMIT_KQUEUES /* FreeBSD */
    {RLIMIT_KQUEUES, "kqueues", ZLIMTYPE_NUMBER, 1,
		'k', "kqueues"},
# endif
# ifdef HAVE_RLIMIT_NPTS    /* FreeBSD */
    {RLIMIT_NPTS, "pseudoterminals", ZLIMTYPE_NUMBER, 1,
		'p', "pseudo-terminals"},
# endif
# ifdef HAVE_RLIMIT_SWAP    /* FreeBSD */
    {RLIMIT_SWAP, "swapsize", ZLIMTYPE_MEMORY, 1024,
		'w', "swap size (kbytes)"},
# endif
# ifdef HAVE_RLIMIT_UMTXP   /* FreeBSD */
    {RLIMIT_UMTXP, "umtxp", ZLIMTYPE_NUMBER, 1,
		'o', "umtx shared locks"},
# endif

# ifdef HAVE_RLIMIT_POSIXLOCKS	/* DragonFly */
    {RLIMIT_POSIXLOCKS, "posixlocks", ZLIMTYPE_NUMBER, 1,
		'x', "number of POSIX locks"},
# endif
# if defined(HAVE_RLIMIT_NTHR) && !defined(HAVE_RLIMIT_RTPRIO) /* Net/OpenBSD */
    {RLIMIT_NTHR, "maxpthreads", ZLIMTYPE_NUMBER, 1,
		'r', "threads"},
# endif
    /* others */
# if defined(HAVE_RLIMIT_PTHREAD) && !defined(HAVE_RLIMIT_NTHR)	/* IRIX ? */
    {RLIMIT_PTHREAD, "maxpthreads", ZLIMTYPE_NUMBER, 1,
		'T', "threads per process"},
# endif
# ifdef HAVE_RLIMIT_AIO_MEM /* HP-UX ? */
    {RLIMIT_AIO_MEM, "aiomemorylocked", ZLIMTYPE_MEMORY, 1024,
		'N', "AIO locked-in-memory (kbytes)"},
# endif
# ifdef HAVE_RLIMIT_AIO_OPS /* HP-UX ? */
    {RLIMIT_AIO_OPS, "aiooperations", ZLIMTYPE_NUMBER, 1,
		'N', "AIO operations"},
# endif
# ifdef HAVE_RLIMIT_TCACHE  /* HP-UX ? */
    {RLIMIT_TCACHE, "cachedthreads", ZLIMTYPE_NUMBER, 1,
		'N', "cached threads"},
# endif
};

/* resinfo[RLIMIT_XXX] points to the corresponding entry
 * in known_resources[] */
static const resinfo_T **resinfo;

/**/
static void
set_resinfo(void)
{
    int i;

    resinfo = (const resinfo_T **)zshcalloc(RLIM_NLIMITS*sizeof(resinfo_T *));

    for (i=0; i<sizeof(known_resources)/sizeof(resinfo_T); ++i) {
	resinfo[known_resources[i].res] = &known_resources[i];
    }
    for (i=0; i<RLIM_NLIMITS; ++i) {
	if (!resinfo[i]) {
	    /* unknown resource */
	    resinfo_T *info = (resinfo_T *)zshcalloc(sizeof(resinfo_T));
	    char *buf = (char *)zalloc(8 /* UNKNOWN- */ + 3 /* digits */ + 1 /* '\0' */);
	    snprintf(buf, 12, "UNKNOWN-%d", i);
	    info->res = -1;	/* negative value indicates "unknown" */
	    info->name = buf;
	    info->type = ZLIMTYPE_UNKNOWN;
	    info->unit = 1;
	    info->opt = 'N';
	    info->descr = buf;
	    resinfo[i] = info;
	}
    }
}

/**/
static void
free_resinfo(void)
{
    int i;
    for (i=0; i<RLIM_NLIMITS; ++i) {
	if (resinfo[i]->res < 0) {  /* unknown resource */
	    free(resinfo[i]->name);
	    free((void*)resinfo[i]);
	}
    }
    free(resinfo);
    resinfo = NULL;
}

/* Find resource by its option character */

/**/
static int
find_resource(char c)
{
    int i;
    for (i=0; i<RLIM_NLIMITS; ++i) {
	if (resinfo[i]->opt == c)
	    return i;
    }
    return -1;
}

/* Print a value of type rlim_t */

/**/
static void
printrlim(rlim_t val, const char *unit)
{
# ifdef RLIM_T_IS_QUAD_T
	printf("%qd%s", val, unit);
# else
#  ifdef RLIM_T_IS_LONG_LONG
	printf("%lld%s", val, unit);
#  else
#   ifdef RLIM_T_IS_UNSIGNED
	printf("%lu%s", (unsigned long)val, unit);
#   else
	printf("%ld%s", (long)val, unit);
#   endif /* RLIM_T_IS_UNSIGNED */
#  endif /* RLIM_T_IS_LONG_LONG */
# endif /* RLIM_T_IS_QUAD_T */
}

/*
 * Parse a string into the corresponding value based on the limit type
 *
 *   ZLIMTYPE_UNKNOWN / ZLIMTYPE_NUMBER:
 *     decimal integer without sign only: raw value
 *
 *   ZLIMTYPE_TIME / ZLIMTYPE_MICROSECONDS:
 *     <decimal> only gives seconds or microseconds depending on type
 *     or:
 *     [[hour:]min:]sec[.usec]
 *     or:
 *     <decimal> with h (hour), m (min), s (sec), ms, us suffix.
 *
 *   ZLIMTYPE_MEMORY:
 *     <decimal> without suffix interpreted as KiB by "limit" (except for
 *     RLIMIT_MSGQUEUE, see below) and based on resinfo.unit by "ulimit".
 *
 *     K/M/G/T/P/E suffix and same with iB suffix use 1024 factor
 *     KB/MB/GB... use 1000 factor.
 *
 *     B for bytes (avoids that mess about default units).
 *
 * All suffixes are case insensitive.
 *
 */

/**/
static rlim_t
zstrtorlimt(const char *s, int lim, int ulimit, char **err)
{
    rlim_t ret = 0, tmp;
    const char *orig = s;
    enum zlimtype type = resinfo[lim]->type;
    *err = NULL;

    if (strcmp(s, "unlimited") == 0)
	return RLIM_INFINITY;

    for (; *s >= '0' && *s <= '9'; s++) {
	if ((tmp = ret * 10 + *s - '0') < ret) {
	    *err = "limit out of range";
	    return 0;
	}
	else
	    ret = tmp;
    }

    if (s == orig) {
	*err = "decimal integer expected";
	return 0;
    }

    if (lim >= RLIM_NLIMITS ||
	type == ZLIMTYPE_NUMBER ||
	type == ZLIMTYPE_UNKNOWN) {
	/*
	 * pure numeric resource -- only a straight decimal number is
	 * permitted.
	 */
	if (*s) {
	    *err = "limit must be a decimal integer";
	    return 0;
	}
    }
    else if (type == ZLIMTYPE_TIME ||
	     type == ZLIMTYPE_MICROSECONDS) {
	if (*s) {
	    int divisor = 1;
	    int factor = 1;
	    switch (*s++) {

	    case 'm': /* m for minute or ms for milisecond */
	    case 'M':
		if (*s == 's' || *s == 'S') {
		    s++;
		    divisor = 1000;
		}
		else {
		    factor = 60;
		}
		break;

	    case 'h':
	    case 'H':
		factor = 3600;
		break;

	    case 's':
	    case 'S':
		break;

	    case 'u':
	    case 'U':
		divisor = 1000000;
		if (*s == 's' || *s == 'S')
		    s++;
		break;

	    case ':':
		do {
		    int more = 0;
		    while (*s >= '0' && *s <= '9') {
			more = more * 10 + (*s - '0');
			s++;
		    }
		    ret = ret * 60 + more;
		} while (*s == ':' && ++s);
		if (*s == '.')
		    s++;
		    /* fallthrough */
		else
		    break;
	    case '.':
		if (type == ZLIMTYPE_MICROSECONDS) {
		    int frac;
		    for (frac = 0; *s >= '0' && *s <= '9'; s++) {
			if (divisor < 1000000) {
			    /* ignore digits past the 6th */
			    divisor *= 10;
			    frac = frac * 10 + (*s - '0');
			}
		    }
		    ret = ret * divisor + frac;
		}
		else {
		    /* fractional part ignored */
		    while (*s >= '0' && *s <= '9')
			s++;
		}
		break;
	    default:
		*err = "invalid time specification";
		return 0;
	    }

	    if (*s) {
		*err = "invalid time specification";
		return 0;
	    }

	    ret *= factor;
	    if (type == ZLIMTYPE_MICROSECONDS)
		ret *= 1000000 / divisor;
	    else
		ret /= divisor;
	}
    }
    else {
	/*
	 * memory-type resource
	 */
	rlim_t unit = 1;
	if (*s) {
	    if (*s == 'b' || *s == 'B')
		s++;
	    else {
		const char *suffix = "kKmMgGtTpPeE";
		char *offset;

		if ((offset = strchr(suffix, *s))) {
		    s++;
		    if (*s == 'b' || *s == 'B') {
			/* KB == 1000 */
			const char *p;
			for (p = suffix; p <= offset; p += 2)
			    unit *= 1000;
			s++;
		    }
		    else {
			/* K/KiB == 1024 */
			if ((s[0] == 'i' || s[0] == 'I') &&
			    (s[1] == 'b' || s[1] == 'B'))
			    s += 2;
			unit <<= ((offset - suffix) / 2 + 1) * 10;
		    }
		}
	    }
	    if (*s) {
		*err = "invalid unit";
		return 0;
	    }
	}
	else {
	    if (ulimit)
		unit = resinfo[lim]->unit;
	    else
#ifdef HAVE_RLIMIT_MSGQUEUE
		if (lim != RLIMIT_MSGQUEUE)
		    /*
		     * Historical quirk. In tcsh's limit (and bash's and mksh's
		     * ulimit, but not ksh93), that limit expects bytes instead
		     * of kibibytes and earlier versions of zsh were treating
		     * it as a ZLIMTYPE_NUMBER.
		     *
		     * We still want to treat it as ZLIMTYPE_MEMORY and accept
		     * KMG... suffixes as it is a number of bytes.
		     *
		     * But we carry on taking the value as a number of *bytes*
		     * in the "limit" builtin for backward compatibility and
		     * compatibility with tcsh.
		     */
#endif
		    unit = 1024;
	}
	if ((tmp = ret*unit) < ret) {
	    *err = "limit out of range";
	    return 0;
	}
	else
	    ret = tmp;
    }
    if (ret == RLIM_INFINITY) {
	/* RLIM_INFINITY can be specified only by the string "unlimited" */
	*err = "limit out of range";
	return 0;
    }
    return ret;
}

/**/
static void
showlimitvalue(int lim, rlim_t val)
{
    /* display limit for resource number lim */
    if (lim < RLIM_NLIMITS)
	printf("%-16s", resinfo[lim]->name);
    else
    {
	/* Unknown limit, hence unknown units. */
	printf("%-16d", lim);
    }
    if (val == RLIM_INFINITY)
	printf("unlimited\n");
    else if (lim >= RLIM_NLIMITS)
	printrlim(val, "\n");
    else if (resinfo[lim]->type == ZLIMTYPE_TIME) {
	/* time-type resource -- display as hours, minutes and
	   seconds. */
	printf("%d:%02d:%02d\n", (int)(val / 3600),
	       (int)(val / 60) % 60, (int)(val % 60));
    } else if (resinfo[lim]->type == ZLIMTYPE_MICROSECONDS)
	printrlim(val, "us\n");	/* microseconds */
    else if (resinfo[lim]->type == ZLIMTYPE_NUMBER ||
	       resinfo[lim]->type == ZLIMTYPE_UNKNOWN)
	printrlim(val, "\n");	/* pure numeric resource */
    else {
	/*
	 * memory resource -- display with KiB/MiB... for exact
	 * multiples of those units
	 */
	const char *units = "KMGTPE";
	rlim_t v = val;
	int n = 0;
	while (units[n] && (v & 1023) == 0 && v >> 10) {
	    n++;
	    v >>= 10;
	}
	if (n) {
	    char suffix[] = "XiB\n";
	    *suffix = units[n-1];
	    printrlim(v, suffix);
	}
	else
	    printrlim(val, "B\n");
    }
}

/*
 * Display resource limits.  hard indicates whether `hard' or `soft'
 * limits should be displayed.  lim specifies the limit, or may be -1
 * to show all.  If `shell' is non-zero, the limits in place for the
 * shell process retrieved with getrlimits() are shown.
 */

/**/
static int
showlimits(char *nam, int hard, int lim, int shell)
{
    int rt;
    int ret = 0;

    if (shell || lim >= RLIM_NLIMITS)
    {
	/*
	 * Not configured into the shell.  Ask the OS
	 * explicitly for this limit.
	 */
	struct rlimit vals;
	if (getrlimit(lim, &vals) < 0)
	{
	    zwarnnam(nam, "can't read limit: %e", errno);
	    return 1;
	}
	showlimitvalue(lim, hard ? vals.rlim_max : vals.rlim_cur);
    }
    else if (lim != -1)
    {
	showlimitvalue(lim, hard ? limits[lim].rlim_max :
		       limits[lim].rlim_cur);
    }
    else
    {
	/* main loop over resource types */
	for (rt = 0; rt != RLIM_NLIMITS; rt++) {
	    struct rlimit vals, *plim;
	    if (shell) {
		/*
		 * FIXME: this is dead code as at the moment, there is no API
		 * for the user can request all limits *of the shell* be
		 * displayed.
		 *
		 * Should we add a "limit -s -a" or "limit -s all"?
		 */
		plim = &vals;
		if (getrlimit(rt, plim) < 0)
		{
		    zwarnnam(nam, "can't read \"%s\" limit: %e", resinfo[rt]->name, errno);
		    ret = 1;
		    continue;
		}
	    }
	    else
		plim = &(limits[rt]);

	    showlimitvalue(rt, (hard) ? plim->rlim_max :
			   plim->rlim_cur);
	}
    }

    return ret;
}

/*
 * Display a resource limit, in ulimit style.  lim specifies which
 * limit should be displayed, and hard indicates whether the hard or
 * soft limit should be displayed.
 */

/**/
static int
printulimit(char *nam, int lim, int hard, int head)
{
    rlim_t limit;

    /* get the limit in question */
    if (lim >= RLIM_NLIMITS)
    {
	struct rlimit vals;

	if (getrlimit(lim, &vals) < 0)
	{
	    zwarnnam(nam, "can't read limit: %e", errno);
	    return 1;
	}
	limit = (hard) ? vals.rlim_max : vals.rlim_cur;
    }
    else
	limit = (hard) ? limits[lim].rlim_max : limits[lim].rlim_cur;
    /* display the appropriate heading */
    if (head) {
	if (lim < RLIM_NLIMITS) {
	    const resinfo_T *info = resinfo[lim];
	    if (info->opt == 'N')
		printf("-N %2d: %-32s ", lim, info->descr);
	    else
		printf("-%c: %-35s ", info->opt, info->descr);
	}
	else
	    printf("-N %2d: %-32s ", lim, "");
    }
    /* display the limit */
    if (limit == RLIM_INFINITY)
	printf("unlimited\n");
    else {
	if (lim < RLIM_NLIMITS)
	    printrlim(limit/resinfo[lim]->unit, "\n");
	else
	    printrlim(limit, "\n");
    }

    return 0;
}

/**/
static int
do_limit(char *nam, int lim, rlim_t val, int hard, int soft, int set)
{
    if (lim >= RLIM_NLIMITS) {
	struct rlimit vals;
	if (getrlimit(lim, &vals) < 0)
	{
	    /* best guess about error */
	    zwarnnam(nam, "can't read limit: %e", errno);
	    return 1;
	}
	if (hard)
	{
	    if (val > vals.rlim_max && geteuid()) {
		zwarnnam(nam, "can't raise hard limits");
		return 1;
	    }
	    vals.rlim_max = val;
	    /*
	     * not show if all systems will do this silently, but
	     * best be safe...
	     */
	    if (val < vals.rlim_cur)
		vals.rlim_cur = val;
	}
	if (soft || !hard) {
	    if (val > vals.rlim_max) {
		zwarnnam(nam, "limit exceeds hard limit");
		return 1;
	    }
	    else
		vals.rlim_cur = val;
	}
	if (!set)
	{
	    zwarnnam(nam,
		     "warning: unrecognised limit %d, use -s to set",
		     lim);
	    return 1;
	}
	else if (setrlimit(lim, &vals) < 0)
	{
	    zwarnnam(nam, "setrlimit failed: %e", errno);
	    return 1;
	}
    } else {
	/* new limit is valid and has been interpreted; apply it to the
	specified resource */
	if (hard) {
	    /* can only raise hard limits if running as root */
	    if (val > current_limits[lim].rlim_max && geteuid()) {
		zwarnnam(nam, "can't raise hard limits");
		return 1;
	    } else {
		limits[lim].rlim_max = val;
		if (val < limits[lim].rlim_cur)
		    limits[lim].rlim_cur = val;
	    }
	}
	if (soft || !hard) {
	    if (val > limits[lim].rlim_max) {
		/* no idea about this difference, don't intend to worry */
		if (*nam == 'u')
		{
		    /* ulimit does this */
		    if (val > current_limits[lim].rlim_max && geteuid()) {
			zwarnnam(nam, "value exceeds hard limit");
			return 1;
		    }
		    limits[lim].rlim_max = limits[lim].rlim_cur = val;
		} else {
		    /* but limit does this */
		    zwarnnam(nam, "limit exceeds hard limit");
		    return 1;
		}
	    } else
		limits[lim].rlim_cur = val;
	    if (set && zsetlimit(lim, nam))
		return 1;
	}
    }
    return 0;
}

/* limit: set or show resource limits.  The variable hard indicates *
 * whether `hard' or `soft' resource limits are being set/shown.    */

/**/
static int
bin_limit(char *nam, char **argv, Options ops, UNUSED(int func))
{
    char *s;
    int hard, limnum, lim;
    rlim_t val;
    int ret = 0;

    hard = OPT_ISSET(ops, 'h');
    if (OPT_ISSET(ops, 's') && !*argv)
	return setlimits(NULL);
    /* without arguments, display limits */
    if (!*argv)
	return showlimits(nam, hard, -1, OPT_ISSET(ops, 's'));
    while ((s = *argv++)) {
	/* Search for the appropriate resource name.  When a name matches (i.e. *
	 * starts with) the argument, the lim variable changes from -1 to the   *
	 * number of the resource.  If another match is found, lim goes to -2.  */
	char *err;

	if (idigit(*s))
	{
	    lim = (int)zstrtol(s, NULL, 10);
	}
	else
	    for (lim = -1, limnum = 0; limnum < RLIM_NLIMITS; limnum++)
		if (!strncmp(resinfo[limnum]->name, s, strlen(s))) {
		    if (lim != -1)
			lim = -2;
		    else
			lim = limnum;
		}
	/* lim==-1 indicates that no matches were found.       *
	 * lim==-2 indicates that multiple matches were found. */
	if (lim < 0) {
	    zwarnnam(nam,
		     (lim == -2) ? "ambiguous resource specification: %s"
		     : "no such resource: %s", s);
	    return 1;
	}
	/* without value for limit, display the current limit */
	if (!(s = *argv++))
	    return showlimits(nam, hard, lim, OPT_ISSET(ops, 's'));

	val = zstrtorlimt(s, lim, 0, &err);
	if (err) {
	    zwarnnam(nam, err);
	    return 1;
	}
	if (do_limit(nam, lim, val, hard, !hard, OPT_ISSET(ops, 's')))
	    ret++;
    }
    return ret;
}

/**/
static int
do_unlimit(char *nam, int lim, int hard, int soft, int set, int euid)
{
    /* remove specified limit */
    if (lim >= RLIM_NLIMITS) {
	struct rlimit vals;
	if (getrlimit(lim, &vals) < 0)
	{
	    zwarnnam(nam, "can't read limit: %e", errno);
	    return 1;
	}
	if (hard) {
	    if (euid && vals.rlim_max != RLIM_INFINITY) {
		zwarnnam(nam, "can't remove hard limits");
		return 1;
	    } else
		vals.rlim_max = RLIM_INFINITY;
	}
	if (!hard || soft)
	    vals.rlim_cur = vals.rlim_max;
	if (!set) {
	    zwarnnam(nam,
		     "warning: unrecognised limit %d, use -s to set", lim);
	    return 1;
	} else if (setrlimit(lim, &vals) < 0) {
	    zwarnnam(nam, "setrlimit failed: %e", errno);
	    return 1;
	}
    } else {
	if (hard) {
	    if (euid && current_limits[lim].rlim_max != RLIM_INFINITY) {
		zwarnnam(nam, "can't remove hard limits");
		return 1;
	    } else
		limits[lim].rlim_max = RLIM_INFINITY;
	}
	if (!hard || soft)
	    limits[lim].rlim_cur = limits[lim].rlim_max;
	if (set && zsetlimit(lim, nam))
	    return 1;
    }
    return 0;
}

/* unlimit: remove resource limits.  Much of this code is the same as *
 * that in bin_limit().                                               */

/**/
static int
bin_unlimit(char *nam, char **argv, Options ops, UNUSED(int func))
{
    int hard, limnum, lim;
    int ret = 0;
    uid_t euid = geteuid();

    hard = OPT_ISSET(ops,'h');
    /* Without arguments, remove all limits. */
    if (!*argv) {
	for (limnum = 0; limnum != RLIM_NLIMITS; limnum++) {
	    if (hard) {
		if (euid && current_limits[limnum].rlim_max != RLIM_INFINITY)
		    ret++;
		else
		    limits[limnum].rlim_max = RLIM_INFINITY;
	    } else
		limits[limnum].rlim_cur = limits[limnum].rlim_max;
	}
	if (OPT_ISSET(ops,'s'))
	    ret += setlimits(nam);
	if (ret)
	    zwarnnam(nam, "can't remove hard limits");
    } else {
	for (; *argv; argv++) {
	    /* Search for the appropriate resource name.  When a name     *
	     * matches (i.e. starts with) the argument, the lim variable  *
	     * changes from -1 to the number of the resource.  If another *
	     * match is found, lim goes to -2.                            */
	    if (idigit(**argv)) {
		lim = (int)zstrtol(*argv, NULL, 10);
	    } else {
		for (lim = -1, limnum = 0; limnum < RLIM_NLIMITS; limnum++)
		    if (!strncmp(resinfo[limnum]->name, *argv, strlen(*argv))) {
			if (lim != -1)
			    lim = -2;
			else
			    lim = limnum;
		    }
	    }
	    /* lim==-1 indicates that no matches were found.       *
	     * lim==-2 indicates that multiple matches were found. */
	    if (lim < 0) {
		zwarnnam(nam,
			 (lim == -2) ? "ambiguous resource specification: %s"
			 : "no such resource: %s", *argv);
		return 1;
	    }
	    else if (do_unlimit(nam, lim, hard, !hard, OPT_ISSET(ops, 's'),
				euid))
		ret++;
	}
    }
    return ret;
}

/* ulimit: set or display resource limits */

/**/
static int
bin_ulimit(char *name, char **argv, UNUSED(Options ops), UNUSED(int func))
{
    int res, resmask = 0, hard = 0, soft = 0, nres = 0, all = 0, ret = 0;
    char *options, *eptr, *number;

    do {
	options = *argv;
	if (options && *options == '-' && !options[1]) {
	    zwarnnam(name, "missing option letter");
	    return 1;
	}
	res = -1;
	if (options && *options == '-') {
	    argv++;
	    while (*++options) {
		if(*options == Meta)
		    *++options ^= 32;
		res = -1;
		switch (*options) {
		case 'H':
		    hard = 1;
		    continue;
		case 'S':
		    soft = 1;
		    continue;
		case 'N':
		    if (options[1]) {
			number = options + 1;
		    } else if (*argv) {
			number = *argv++;
		    } else {
			zwarnnam(name, "number required after -N");
			return 1;
		    }
		    res = (int)zstrtol(number, &eptr, 10);
		    if (*eptr) {
			zwarnnam(name, "invalid number: %s", number);
			return 1;
		    }
		    /*
		     * fake it so it looks like we just finished an option...
		     */
		    while (options[1])
			options++;
		    break;
		case 'a':
		    if (resmask) {
			zwarnnam(name, "no limits allowed with -a");
			return 1;
		    }
		    all = 1;
		    resmask = (1 << RLIM_NLIMITS) - 1;
		    nres = RLIM_NLIMITS;
		    continue;
		default:
		    res = find_resource(*options);
		    if (res < 0) {
			/* unrecognised limit */
			zwarnnam(name, "bad option: -%c", *options);
			return 1;
		    }
		    break;
		}
		if (options[1]) {
		    resmask |= 1 << res;
		    nres++;
		}
		if (all && res != -1) {
		    zwarnnam(name, "no limits allowed with -a");
		    return 1;
		}
	    }
	}
	if (!*argv || **argv == '-') {
	    if (res < 0) {
		if (*argv || nres)
		    continue;
		else
		    res = RLIMIT_FSIZE;
	    }
	    resmask |= 1 << res;
	    nres++;
	    continue;
	}
	if (all) {
	    zwarnnam(name, "no arguments allowed after -a");
	    return 1;
	}
	if (res < 0)
	    res = RLIMIT_FSIZE;
	if (strcmp(*argv, "unlimited")) {
	    /* set limit to specified value */
	    rlim_t limit;

	    if (!strcmp(*argv, "hard")) {
		struct rlimit vals;

		if (getrlimit(res, &vals) < 0)
		{
		    zwarnnam(name, "can't read limit: %e", errno);
		    return 1;
		}
		else
		{
		    limit = vals.rlim_max;
		}
	    } else {
		char *err;
		limit = zstrtorlimt(*argv, res, 1, &err);
		if (err) {
		    zwarnnam(name, "%s: %s", *argv, err);
		    return 1;
		}
	    }
	    if (do_limit(name, res, limit, hard, soft, 1))
		ret++;
	} else {
	    if (do_unlimit(name, res, hard, soft, 1, geteuid()))
		ret++;
	}
	argv++;
    } while (*argv);
    for (res = 0; resmask; res++, resmask >>= 1)
	if ((resmask & 1) && printulimit(name, res, hard, nres > 1))
	    ret++;
    return ret;
}

#else /* !HAVE_GETRLIMIT || !RLIM_INFINITY */

# define bin_limit   bin_notavail
# define bin_ulimit  bin_notavail
# define bin_unlimit bin_notavail

#endif /* !HAVE_GETRLIMIT || !RLIM_INFINITY */

static struct builtin bintab[] = {
    BUILTIN("limit",   0, bin_limit,   0, -1, 0, "sh", NULL),
    BUILTIN("ulimit",  0, bin_ulimit,  0, -1, 0, NULL, NULL),
    BUILTIN("unlimit", 0, bin_unlimit, 0, -1, 0, "hs", NULL),
};

static struct features module_features = {
    bintab, sizeof(bintab)/sizeof(*bintab),
    NULL, 0,
    NULL, 0,
    NULL, 0,
    0
};

/**/
int
setup_(UNUSED(Module m))
{
    return 0;
}

/**/
int
features_(Module m, char ***features)
{
    *features = featuresarray(m, &module_features);
    return 0;
}

/**/
int
enables_(Module m, int **enables)
{
    return handlefeatures(m, &module_features, enables);
}

/**/
int
boot_(UNUSED(Module m))
{
    set_resinfo();
    return 0;
}

/**/
int
cleanup_(Module m)
{
    free_resinfo();
    return setfeatureenables(m, &module_features, NULL);
}

/**/
int
finish_(UNUSED(Module m))
{
    return 0;
}
debug log:

solving 69174fe8e ...
found 69174fe8e in https://inbox.vuxu.org/zsh-workers/FF7E5A4B-E00F-4160-AFBD-45783C0BAF7B@kba.biglobe.ne.jp/
found b6568d956 in https://inbox.vuxu.org/zsh-workers/20201126205819.dbncs24ppnw3pdny@chazelas.org/
found 5f9c84b0f in https://git.vuxu.org/mirror/zsh/
preparing index
index prepared:
100644 5f9c84b0f6cf34ebfe39707c82f5206418a3dd8f	Src/Builtins/rlimits.c

applying [1/2] https://inbox.vuxu.org/zsh-workers/20201126205819.dbncs24ppnw3pdny@chazelas.org/
diff --git a/Src/Builtins/rlimits.c b/Src/Builtins/rlimits.c
index 5f9c84b0f..b6568d956 100644


applying [2/2] https://inbox.vuxu.org/zsh-workers/FF7E5A4B-E00F-4160-AFBD-45783C0BAF7B@kba.biglobe.ne.jp/
diff --git a/Src/Builtins/rlimits.c b/Src/Builtins/rlimits.c
index b6568d956..69174fe8e 100644

Checking patch Src/Builtins/rlimits.c...
Applied patch Src/Builtins/rlimits.c cleanly.
Checking patch Src/Builtins/rlimits.c...
Applied patch Src/Builtins/rlimits.c cleanly.

index at:
100644 69174fe8ef373dce12027e9a317372f1158018d8	Src/Builtins/rlimits.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).