### Copyright (C) 1996,97 Per Zacho
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

proc limits {} {
    # System<->Bash interface / System limits

    Desc "This configuration page will help you to setup system limits (e.g. how much disk space"\
    "a user file may allocate, how much memory and CPU-time a user process can allocate, etc.).\n"\
    "NOTE: You cannot raise limits above those sysop has set: if in doubt, try \"ulimit -a\"."

    ShortDesc "System limits"

    set brdwdth 4
    set skip 16

    ### Memory management ###

    Header head1 -text "Memory management"\
	    -background gray\
	    -help "Setup of limits concerning memory management."

    #labels
    Label dataSegLabel -text "Data segment"\
	    -help "Data- and stack segments have to do with dynamic memory allocations, i.e.,"\
	    "memory for which a process asks the operating system at runtime. Usually you will"\
	    "not have to limit process data segment."\
	    -borderwidth $brdwdth

    Label stackSegLabel -text "Stack segment"\
	    -help "Data- and stack segments have to do with dynamic memory allocations, i.e.,"\
	    "memory for which a process asks the operating system at runtime. Usually you will"\
	    "not have to limit process stack segment."\
	    -borderwidth $brdwdth

    Label mainMemLabel -text "Main memory"\
	    -help "Virtual- and main memory options are similar to the two above. This option"\
	    "limits the amount of physical memory that a process is allowed to allocate. Usually"\
	    "you will not have to put a limit on this unless your system suffers severe memory"\
	    "constraints or you want to avoid thrashing."\
	    -borderwidth $brdwdth

    Label virtualMemLabel -text "Virtual memory"\
	    -help "Virtual- and main memory options are similar to the two above. This option"\
	    "limits all uses of memory. You normally will not have to put a limit on this unless"\
	    "your system suffers severe memory constraints or you want to avoid thrashing."\
	    -borderwidth $brdwdth

    Label lockMemLabel -text "Locked memory"\
	    -help "Locked memory option restrict the maximum amount of memory a process is"\
	    "allowed to lock. This option is only available from version 2.0 of Bash"\
	    -borderwidth $brdwdth

    Frame memLabels -entries {dataSegLabel stackSegLabel mainMemLabel virtualMemLabel lockMemLabel}\
	    -orient top

    #radios
    foreach elm {dataSeg stackSeg mainMem lockMem} {
	Radio ${elm}Radio -text ""\
		-entries {"unlimited" "limited"}
    }

    Radio virtualMemRadio -text ""\
	    -entries {"maximum" "limited"}

    Frame memRadios -entries {dataSegRadio stackSegRadio mainMemRadio virtualMemRadio lockMemRadio}\
	    -orient top

    #ints
    foreach elm {dataSeg stackSeg virtualMem mainMem lockMem} {
	Int ${elm}Int -text ""\
		-textafter "kilobytes"\
		-labelAfter:width 15
    }

    Frame memInts -entries {dataSegInt stackSegInt mainMemInt virtualMemInt lockMemInt}\
	    -orient top

    #currentvalues
    foreach elm {dataSeg stackSeg mainMem virtualMem lockMem} {
	Label ${elm}Current -text ""\
		-help "This is the current setting--increasing value beyond this may not"\
		"be legal."\
		-borderwidth $brdwdth
    }

    Frame memCurrents -entries {dataSegCurrent stackSegCurrent mainMemCurrent virtualMemCurrent lockMemCurrent}\
	    -orient top

    Frame mem -entries {memLabels memRadios memInts memCurrents}\
	    -orient left\
	    -packFrame:fill x


    ### File system ###
    Header head2 -text "File system"\
	    -background gray\
	    -help "Setup of limits concerning file system."

    #labels
    Label fileSizeLabel -text "File size"\
	    -help "Put a limit on how much diskspace a file can allocate (often huge files are"\
	    "the work of buggy programs). This is useful if you have constraints on disk space."\
	    -borderwidth $brdwdth

    Label coreSizeLabel -text "Core-dump size"\
	    -help "If a program halts abruptly--either by user or due to a program bug, the system"\
	    "makes a core-dump (memory dump). These files are often quite large and, unless you intend"\
	    "to debug the failing program, of no interest."\
	    -borderwidth $brdwdth

    Label fileDescLabel -text "File descriptors"\
	    -help "Put a limit on how many files to have open at the same time per process."\
	    Usually you will not have to limit this number. Do not set the number too low,"\
	    "many programs and commands opens several files at the same time (e.g. performing"\
	    "a \"ls\" require at least five file descriptors). Some older versions of"\
	    "UNIX will not let you alter this number--they have a fixed limit of 20 file"\
	    "descriptors per process."\
	    -borderwidth $brdwdth

    Label pipeSizeLabel -text "Pipe size"\
	    -help "Sets the size for a pipe between two commands/programs. A pipe (|) is to be"\
	    "considered as a fifo (first-in-first-out) queue, where information from"\
	    "sender (command/program) is stored until receiver reads it. You can specify"\
	    "how much memory Bash is to allocate for this buffer, in 512-byte increments"\
	    "(default is 8, i.e. 4 kilobytes).\nTHIS OPTION MAY NOT BE ALTERED"\
	    -borderwidth $brdwdth

    Frame fileLabels -entries {fileSizeLabel coreSizeLabel fileDescLabel pipeSizeLabel}\
	    -orient top

    #radios
    Radio fileSizeRadio -text ""\
	    -entries {"unlimited" "limited"}

    Radio coreSizeRadio -text ""\
	    -entries {"unlimited" "limited"}\
	    -defaultIndex 1

    Frame fileRadios -entries {fileSizeRadio coreSizeRadio}\
	    -orient top\
	    -packFrame:anchor n\
	-packFrame:expand 0\
	-packFrame:fill none

    #ints
    Int fileSizeInt -text ""\
	    -textafter "blocks"\
	    -labelAfter:width 15

    Int coreSizeInt -text ""\
	    -textafter "blocks"\
	    -default 0\
	    -labelAfter:width 15

    Int pipeSizeInt -text ""\
	    -textafter "512-byte blocks"\
	    -default 8\
	    -labelAfter:width 15

    Int fileDescInt -text ""\
	    -textafter "files"\
	    -default 256\
	    -labelAfter:width 15

    Frame fileInts -entries {fileSizeInt coreSizeInt fileDescInt pipeSizeInt}\
	    -orient top

    #currentvalues
    foreach elm {fileSize coreSize pipeSize fileDesc} {
	Label ${elm}Current -text ""\
		-help "This is the current setting--increasing value beyond this may not"\
		"be legal."\
		-borderwidth $brdwdth
    }

    Frame fileCurrents -entries {fileSizeCurrent coreSizeCurrent fileDescCurrent pipeSizeCurrent}\
	    -orient top

    Frame file -entries {fileLabels fileRadios fileInts fileCurrents}\
	    -orient left\
	    -packFrame:fill x

    
    ### Process management ###
    Header head3 -text "Process management"\
	    -background gray\
	    -help "Setup of limits concerning process management."

    #labels
    Label cpuTimeLabel -text "CPU time"\
	    -help "Limit amount of CPU-time a process can use. This is a possible guard"\
	    "against infinite loops, although it also limits how long a non-buggy program"\
	    "is allowed to run. You are probably better off, leaving this option unlimited."\
	    -borderwidth $brdwdth\
	    -padx $skip

    Label maxProcsLabel -text "\# of procs."\
	    -help "Limit number of processes a user can start. Useful if you have"\
	    "system memory constraints."\
	    -borderwidth $brdwdth\
	    -padx $skip

    Frame procLabels -entries {cpuTimeLabel maxProcsLabel}\
	    -orient top

    #radios
    Radio cpuTimeRadio -text ""\
	    -entries {"unlimited" "limited"}

    Radio maxProcsRadio -text ""\
	    -entries {"unlimited" "limited"}\
	    -defaultIndex 1

    Frame procRadios -entries {cpuTimeRadio maxProcsRadio}\
	    -orient top\
	    -packFrame:anchor n

    #ints
    Int cpuTimeInt -text ""\
	    -textafter "seconds"\
	    -labelAfter:width 15

    Int maxProcsInt -text ""\
	    -textafter "procs."\
	    -default 256\
	    -labelAfter:width 15

    Frame procInts -entries {cpuTimeInt maxProcsInt}\
	    -orient top

    #currentvalues
    foreach elm {cpuTime maxProcs} {
	Label ${elm}Current -text ""\
		-help "This is the current setting--increasing value beyond this may not"\
		"be legal."\
		-borderwidth $brdwdth
    }

    Frame procCurrents -entries {cpuTimeCurrent maxProcsCurrent}\
	    -orient top

    Frame proc -entries {procLabels procRadios procInts procCurrents}\
	    -orient left\
	    -packFrame:fill x

    Line linie1

    Command readPath -setvalue {$widget configure -text "Load current system limits"}\
	    -invoke {
	### Reading system limits ###
	set temp ""
	catch {set temp [exec bash -c "ulimit -a"]}
	set sysopLimits {}
	if {$temp != ""} {
	    ### Parse input 
	    ### Parser depend (completely) on how information is listed
	    ### Parser build on output from ulimit -a (10 lines), Bash version 1.14.5(1)
	    ### Parser build on output from ulimit -a (11 lines), Bash version 2.00.0(1)-release
	    set inList [split $temp "\n"]
	    if {[llength $inList] == 11} {
		foreach elm $inList {
		    lappend sysopLimits [string range $elm 28 end]
		}
	    } else {
	      if {[llength $inList] == 10} {
		  foreach elm $inList {
		      lappend sysopLimits [string range $elm 25 end]
		  }
	      }
            }
	
	    if {[llength $inList] == 11} {
		### I guess you use Bash version 2.0+
		set temp {coreSize dataSeg fileSize lockMem mainMem fileDesc pipeSize stackSeg cpuTime maxProcs virtualMem}
	    } else {
		### I guess you use a Bash version prior to 2.0
		set temp {coreSize dataSeg fileSize mainMem stackSeg cpuTime maxProcs pipeSize fileDesc virtualMem}
	    }
	    if {$sysopLimits != {}} {
		set line 0
		foreach elm $temp {
		    set ${elm}Current "([lindex $sysopLimits $line])"
		    incr line
		}
	    } {
		set line 0
		foreach elm $temp {
		    set ${elm}Current "n/a"
		    incr line
		}
	    }
	}
    }

    ShowPage {
	### Memory management ###
	Disable dataSegInt stackSegInt virtualMemInt mainMemInt
	### File system ###
	Disable fileSizeInt
	### Process management ###
	Disable cpuTimeInt

	### Disable pipe-size configuration ###
	Disable pipeSizeLabel pipeSizeInt
    }

    Change {
	### Enable/disable entries ###
	if {[string first "Radio" $changeElm] != -1} {
	    set temp [string trimright $changeElm "Radio"]
	    if {[set ${changeElm}(index)]} {
		Enable ${temp}Int
	    } {
		Disable ${temp}Int
	    }
	}
    }

    PageEnd {
	### Memory management ###
	foreach elm {dataSeg stackSeg virtualMem mainMem} {
	    if {[set ${elm}Radio(index)]} {
		if {[set ${elm}Int] < 1} {
		    error "Illegal value specified in Memory management ($elm)."
		}
	    }
	}

	### File system ###
	foreach elm {fileSize coreSize} {
	    if {[set ${elm}Radio(index)]} {
		if {[set ${elm}Int] < 0} {
		    error "Illegal value specified in File system ($elm)."
		}
	    }
	}
	foreach elm {pipeSizeInt fileDescInt} {
	    if {[set $elm] < 1} {
		error "Illegal value specified in File system ($elm)."
	    }
	}

	### Process management ###
	foreach elm {cpuTime maxProcs} {
	    if {[set ${elm}Radio(index)]} {
		if {[set ${elm}Int] < 1} {
		    error "Illegal value specified in Process management ($elm)."
		}
	    }
	}
    }

    Save {
	### Memory management ###
	if {$dataSegRadio(index) || $generateDefault} {
	    print "ulimit -d [pick $dataSegRadio(index) $dataSegInt unlimited]"
	}
	if {$stackSegRadio(index) || $generateDefault} {
	    print "ulimit -s [pick $stackSegRadio(index) $stackSegInt unlimited]"
	}
	if {$virtualMemRadio(index) || $generateDefault} {
	    print "ulimit -v [pick $virtualMemRadio(index) $virtualMemInt 4194302]"
	}
	if {$mainMemRadio(index) || $generateDefault} {
	    print "ulimit -m [pick $mainMemRadio(index) $mainMemInt unlimited]"
	}

	if {$lockMemRadio(index) || $generateDefault} {
	    print "ulimit -l [pick $lockMemRadio(index) $lockMemInt unlimited]"
	}

	### File system ###
	if {$fileSizeRadio(index) || $generateDefault} {
	    print "ulimit -f [pick $fileSizeRadio(index) $fileSizeInt unlimited]"
	}
	if {$coreSizeRadio(index) != 1 || ($coreSizeRadio(index) && $coreSizeInt != 0) || $generateDefault} {
	    print "ulimit -c [pick $coreSizeRadio(index) $coreSizeInt unlimited]"
	}
	if {$fileDescInt != 256 || $generateDefault} {
	    print "ulimit -n $fileDescInt"
	}
	### This configuration option cannot be altered ###
	### if {$pipeSizeInt != 8 || $generateDefault} {
	###    print "ulimit -p $pipeSizeInt"
	### }

	### Process management ###
	if {$cpuTimeRadio(index) || $generateDefault} {
	    print "ulimit -t [pick $cpuTimeRadio(index) $cpuTimeInt unlimited]"
	}
	if {$maxProcsRadio(index) != 1 || ($maxProcsRadio(index) && $maxProcsInt != 256) || $generateDefault} {
	    print "ulimit -u [pick $maxProcsRadio(index) $maxProcsInt unlimited]"
	}
    }
}


