Class: Plist4r::PlistType::Launchd
- Defined in:
- lib/plist4r/plist_type/launchd.rb
Classes
Instance Methods
- environment_variables
- hard_resource_limits
- inetd_compatibility
- keep_alive
- mach_services
- socket
- soft_resource_limits
- start_calendar_interval
Methods inherited from
#array_dict, #initialize, match_stat, #to_hash, #to_s, #to_sym
Methods included from Plist4r::DataMethods
#_respond_to?, #method_missing, #set_or_return, #set_or_return_of_type, #validate_value
Overview
For help in working with launchd, see www.mactech.com/articles/mactech/Vol.21/21.06/launchd/
Constant Summary
- ValidKeys =
A Hash Array of the supported plist keys for this type. These are plist keys which belong to the PlistType for Launchd plists. Each CamelCased key name has a corresponding set_or_return method call. For example “UserName” => user_name(value). For more information please see PlistKeyNames
{ :string => %w[ Label UserName GroupName LimitLoadToSessionType Program RootDirectory \ WorkingDirectory StandardInPath StandardOutPath StandardErrorPath ], :bool => %w[ Disabled EnableGlobbing EnableTransactions OnDemand RunAtLoad InitGroups \ StartOnMount Debug WaitForDebugger AbandonProcessGroup HopefullyExitsFirst \ HopefullyExitsLast LowPriorityIO LaunchOnlyOnce ], :integer => %w[ Umask TimeOut ExitTimeOut ThrottleInterval StartInterval Nice ], :array_of_strings => %w[ LimitLoadToHosts LimitLoadFromHosts ProgramArguments WatchPaths QueueDirectories ], :method_defined => %w[ inetdCompatibility KeepAlive EnvironmentVariables StartCalendarInterval SoftResourceLimits, HardResourceLimits MachServices Socket ] }
Constants inherited from
Constants included from Plist4r::DataMethods
ClassesForKeyType, ValidKeys, ValidKeysTemplate
Constructor Details
This class inherits a constructor from Plist4r::PlistType
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Plist4r::DataMethods
Instance Method Details
- (Object) environment_variables(value = nil, &blk)
Set or return the plist key EnvironmentVariables
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/plist4r/plist_type/launchd.rb', line 152 def environment_variables value=nil, &blk key = "EnvironmentVariables" case value when Hash value.each do |k,v| unless k.class == String raise "Invalid key: #{method_name}[#{k.inspect}]. Should be of type String" end unless v.class == String raise "Invalid value: #{method_name}[#{k.inspect}] = #{v.inspect}. Should be of type String" end end @hash[key] = value when nil @hash[key] else raise "Invalid value: #{method_name} #{value.inspect}. Should be: #{method_name} { hash_of_strings }" end end |
- (Object) hard_resource_limits(value = nil, &blk)
Set or return the plist key HardResourceLimits
Resource limits to be imposed on the job. These adjust variables set with setrlimit(2). The following keys apply:
369 370 371 372 373 374 375 376 377 |
# File 'lib/plist4r/plist_type/launchd.rb', line 369 def hard_resource_limits value=nil, &blk key = "HardResourceLimits" if blk @hash[key] = ::Plist4r::OrderedHash.new @hash[key] = ResourceLimits.new(@hash[key],&blk).to_hash else @hash[key] end end |
- (Object) inetd_compatibility(value = nil, &blk)
Set or return the plist key inetdCompatibility
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/plist4r/plist_type/launchd.rb', line 52 def inetd_compatibility value=nil, &blk key = "inetdCompatibility" case value when nil if block_given? @hash[key] = ::Plist4r::OrderedHash.new @hash[key] = InetdCompatibility.new(@hash[key],&blk).to_hash else @hash[key] end else raise "Invalid value: #{method_name} #{value.inspect}. Should be: #{method_name} { wait true|false }" end end |
- (Object) keep_alive(value = nil, &blk)
Set or return the plist key KeepAlive
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/plist4r/plist_type/launchd.rb', line 123 def keep_alive value=nil, &blk key = "KeepAlive" case value when TrueClass, FalseClass @hash[key] = value when nil if block_given? @hash[key] = ::Plist4r::OrderedHash.new @hash[key] = KeepAlive.new(@hash[key],&blk).to_hash else @hash[key] end else raise "Invalid value: #{method_name} #{value.inspect}. Should be: #{method_name} true|false, or #{method_name} { block }" end end |
- (Object) mach_services(value = nil, &blk)
Set or return the plist key MachServices
Structure: dictionary of booleans or dictionary of dictionaries
This optional key is used to specify Mach services to be registered with the Mach bootstrap sub-system. Each key in this dictionary should be the name of service to be advertised. The value of the key must be a boolean and set to true. Alternatively, a dictionary can be used instead of a simple true value.
Finally, for the job itself, the values will be replaced with Mach ports at the time of check-in with launchd.
436 437 438 439 440 441 442 443 444 |
# File 'lib/plist4r/plist_type/launchd.rb', line 436 def mach_services value=nil, &blk key = "MachServices" if blk @hash[key] = ::Plist4r::OrderedHash.new @hash[key] = MachServices.new(@hash[key],&blk).to_hash else @hash[key] end end |
- (Object) socket(index_or_key = nil, index = nil, &blk)
Set or return the plist key Sockets
Structure: dictionary of dictionaries or dictionary of array of dictionaries
Please see developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/LaunchOnDemandDaemons.html for more information about how to properly use the Sockets feature.
NOTE:
Sockets will work only if a special Sockets callback method is implemented within the daemon which launchd is attempting to start. (this is not commonly the case for most OSS eg apache, mysql, etc). If writing / modifying a deamon is not an option, it may be more worthwhile to consider if the Launchd WatchPaths feature is applicable instead of Sockets.
Heres 2 example deamons which implement the necessary sockets callbacks.
Examples:
developer.apple.com/mac/library/samplecode/BetterAuthorizationSample/Introduction/Intro.html
bitbucket.org/mikemccracken/py-launchd/wiki/Home
The remainder of this section simply describes how to declare Sockets definitions within a launchd plist file. It does not explain how to implement the sockets feature in a deamon. (For that, please see the above links)
This optional key is used to specify launch on demand sockets that can be used to let launchd know when to run the job. The job must check-in to get a copy of the file descriptors using APIs outlined in launch(3). The keys of the top level Sockets dictionary can be anything. They are meant for the application developer to use to differentiate which descriptors correspond to which application level protocols (e.g. http vs. ftp vs. DNS…). At check-in time, the value of each Sockets dictionary key will be an array of descriptors. Daemon/Agent writers should consider all descriptors of a given key to be to be effectively equivalent, even though each file descriptor likely represents a different networking protocol which conforms to the criteria specified in the job configuration file. The parameters below are used as inputs to call getaddrinfo(3).
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 |
# File 'lib/plist4r/plist_type/launchd.rb', line 669 def socket index_or_key=nil, index=nil, &blk key = "Sockets" if blk @hash[key] = ::Plist4r::OrderedHash.new sockets = Sockets.new(@hash[key]) case index_or_key when nil sockets.add_socket :implicit, "Listeners", &blk when String socket_key = index_or_key unless index.class == Fixnum raise "Invalid sockect index: #{method_name} #{socket_key} #{index.inspect}. Should be: #{method_name} <socket_key> <socket_index> &blk" end socket_index = index sockets.add_socket :explicit, socket_key, socket_index, &blk when Fixnum socket_index = index_or_key sockets.add_socket :implicit, "Listeners", socket_index, &blk else raise "Invalid socket key: #{method_name} #{index_or_key.inspect}. Should be: #{method_name} <socket_key> &blk" end @hash[key] = sockets.to_hash else @hash[key] end end |
- (Object) soft_resource_limits(value = nil, &blk)
Set or return the plist key SoftResourceLimits
Resource limits to be imposed on the job. These adjust variables set with setrlimit(2). The following keys apply:
304 305 306 307 308 309 310 311 312 |
# File 'lib/plist4r/plist_type/launchd.rb', line 304 def soft_resource_limits value=nil, &blk key = "SoftResourceLimits" if blk @hash[key] = ::Plist4r::OrderedHash.new @hash[key] = ResourceLimits.new(@hash[key],&blk).to_hash else @hash[key] end end |
- (Object) start_calendar_interval(index = nil, &blk)
Set or return the plist key StartCalendarInterval
This optional key causes the job to be started every calendar interval as specified. Missing arguments are considered to be wildcard. The semantics are much like crontab(5). Unlike cron which skips job invocations when the computer is asleep, launchd will start the job the next time the computer wakes up. If multiple intervals transpire before the computer is woken, those events will be coalesced into one event upon wake from sleep.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/plist4r/plist_type/launchd.rb', line 226 def start_calendar_interval index=nil, &blk key = "StartCalendarInterval" unless [Fixnum,NilClass].include? index.class raise "Invalid index: #{method_name} #{index.inspect}. Should be: #{method_name} <integer>" end if blk @hash[key] ||= [] h = StartCalendarInterval.new(@hash[key],index,&blk).to_hash if index @hash[key][index] = h else @hash[key] << h end else @hash[key] end end |