'''
rlimit.py - An "service" to set resource limits in sagator.

(c) 2004 Jan ONDREJ (SAL) <ondrejj(at)salstar.sk>

 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.

'''

from aglib import *

__all__=['rlimit']

class rlimit(service):
  '''
  rlimit service for sagator.
  
  This service can be used to set resource limits for sagator.
  
  Usage: rlimit(PARAM1=value1, PARAM2=value1, ...)
  
  Where: PARAM1,... are resource parameter names
         value1,... are resource values

  Example: rlimit(AS=30000000)
  '''
  name='rlimit()'
  def __init__(self,**args):
      self.ARGS=args
  def main(self):
      import resource
      debug.echo(1,"rlimit(): LIMITS:",str(self.ARGS))
      for res,limit in self.ARGS.items():
        try:
          eres=eval('resource.RLIMIT_'+res)
          resource.setrlimit(eres,(limit,limit))
          debug.echo(7,"rlimit(): Limit %s set to %s"
            % (res,str(resource.getrlimit(eres))))
        except:
          debug.echo(1,"rlimit(): ERROR: Can't set limit for resource: "+res)
          raise
      return []
