| 805 | | # A wrapper over the setuid system call. Particularly useful when opening a network |
|---|
| 806 | | # server on a privileged port because you can use this call to drop privileges |
|---|
| 807 | | # after opening the port. |
|---|
| 808 | | # This method has no effective implementation on Windows or in the pure-Ruby |
|---|
| 809 | | # implementation of EventMachine. |
|---|
| 810 | | # Call #set_effective_user by passing it a string containing the effective name |
|---|
| 811 | | # of the user whose privilege-level your process should attain. |
|---|
| 812 | | # This method is intended for use in enforcing security requirements, consequently |
|---|
| 813 | | # it will throw a fatal error and end your program if it fails. |
|---|
| 814 | | def self::set_effective_user username |
|---|
| 815 | | EventMachine::setuid_string username |
|---|
| 816 | | end |
|---|
| 817 | | |
|---|
| 818 | | #-- |
|---|
| 819 | | # |
|---|
| 820 | | def self::popen cmd, mode="r", handler=nil |
|---|
| 821 | | klass = if (handler and handler.is_a?(Class)) |
|---|
| 822 | | handler |
|---|
| 823 | | else |
|---|
| 824 | | Class.new( Connection ) {handler and include handler} |
|---|
| 825 | | end |
|---|
| 826 | | |
|---|
| 827 | | s = invoke_popen cmd, mode |
|---|
| 828 | | c = klass.new s |
|---|
| 829 | | @conns[s] = c |
|---|
| 830 | | yield(c) if block_given? |
|---|
| 831 | | c |
|---|
| 832 | | end |
|---|
| | 805 | # A wrapper over the setuid system call. Particularly useful when opening a network |
|---|
| | 806 | # server on a privileged port because you can use this call to drop privileges |
|---|
| | 807 | # after opening the port. |
|---|
| | 808 | # This method has no effective implementation on Windows or in the pure-Ruby |
|---|
| | 809 | # implementation of EventMachine. |
|---|
| | 810 | # Call #set_effective_user by passing it a string containing the effective name |
|---|
| | 811 | # of the user whose privilege-level your process should attain. |
|---|
| | 812 | # This method is intended for use in enforcing security requirements, consequently |
|---|
| | 813 | # it will throw a fatal error and end your program if it fails. |
|---|
| | 814 | # |
|---|
| | 815 | def self::set_effective_user username |
|---|
| | 816 | EventMachine::setuid_string username |
|---|
| | 817 | end |
|---|
| | 818 | |
|---|
| | 819 | |
|---|
| | 820 | |
|---|
| | 821 | # Schedules a proc for execution immediately after the next "turn" through the reactor |
|---|
| | 822 | # core. An advanced technique, this can be useful for improving memory management and/or |
|---|
| | 823 | # application responsiveness, especially when scheduling large amounts of data for |
|---|
| | 824 | # writing to a network connection. TODO, we need a FAQ entry on this subject. |
|---|
| | 825 | # |
|---|
| | 826 | # #next_tick takes either a single argument (which must be a Proc) or a block. |
|---|
| | 827 | # And I'm taking suggestions for a better name for this method. |
|---|
| | 828 | # |
|---|
| | 829 | def self::next_tick pr=nil, &block |
|---|
| | 830 | raise "no argument or block given" unless ((pr && pr.respond_to?(:call)) or block) |
|---|
| | 831 | (@next_tick_procs ||= []) << (pr || block) |
|---|
| | 832 | if @next_tick_procs.length == 1 |
|---|
| | 833 | add_timer(0) { |
|---|
| | 834 | @next_tick_procs.each {|t| t.call} |
|---|
| | 835 | @next_tick_procs.clear |
|---|
| | 836 | } |
|---|
| | 837 | end |
|---|
| | 838 | end |
|---|
| | 839 | |
|---|
| | 840 | # TODO, must document popen. At this moment, it's only available on Unix, and it's half-duplex. |
|---|
| | 841 | # Both of these limitations are expected to go away. |
|---|
| | 842 | #-- |
|---|
| | 843 | # |
|---|
| | 844 | def self::popen cmd, mode="r", handler=nil |
|---|
| | 845 | klass = if (handler and handler.is_a?(Class)) |
|---|
| | 846 | handler |
|---|
| | 847 | else |
|---|
| | 848 | Class.new( Connection ) {handler and include handler} |
|---|
| | 849 | end |
|---|
| | 850 | |
|---|
| | 851 | s = invoke_popen cmd, mode |
|---|
| | 852 | c = klass.new s |
|---|
| | 853 | @conns[s] = c |
|---|
| | 854 | yield(c) if block_given? |
|---|
| | 855 | c |
|---|
| | 856 | end |
|---|
| | 857 | |
|---|
| | 858 | |
|---|