1 # This file was automatically generated by SWIG (http://www.swig.org).
4 # Don't modify this file, modify the SWIG interface instead.
9 @ISA = qw(Exporter DynaLoader);
10 package Amanda::Utilc;
11 bootstrap Amanda::Util;
15 # ---------- BASE METHODS -------------
20 my ($classname,$obj) = @_;
21 return bless $obj, $classname;
31 my ($self,$field) = @_;
32 my $member_func = "swig_${field}_get";
33 $self->$member_func();
37 my ($self,$field,$newval) = @_;
38 my $member_func = "swig_${field}_set";
39 $self->$member_func($newval);
48 # ------- FUNCTION WRAPPERS --------
52 *set_pname = *Amanda::Utilc::set_pname;
53 *safe_cd = *Amanda::Utilc::safe_cd;
54 *check_running_as = *Amanda::Utilc::check_running_as;
55 *set_erroutput_type = *Amanda::Utilc::set_erroutput_type;
57 # ------- VARIABLE STUBS --------
61 *RUNNING_AS_ROOT = *Amanda::Utilc::RUNNING_AS_ROOT;
62 *RUNNING_AS_DUMPUSER = *Amanda::Utilc::RUNNING_AS_DUMPUSER;
63 *RUNNING_AS_DUMPUSER_PREFERRED = *Amanda::Utilc::RUNNING_AS_DUMPUSER_PREFERRED;
64 *RUNNING_AS_CLIENT_LOGIN = *Amanda::Utilc::RUNNING_AS_CLIENT_LOGIN;
65 *RUNNING_AS_UID_ONLY = *Amanda::Utilc::RUNNING_AS_UID_ONLY;
70 use Amanda::Debug qw(:init);
72 use POSIX qw(:fcntl_h);
76 Amanda::Util - Runtime support for Amanda applications
78 =head1 Application Initialization
80 Application initialization generally looks like this:
82 use Amanda::Config qw( :init );
83 use Amanda::Util qw( :check_running_as_flags );
86 Amanda::Util::setup_application("myapp", "server", "cmdline");
87 # .. command-line processing ..
88 Amanda::Config::config_init(...);
89 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
93 =item C<setup_application($name, $type, $context)>
95 Set up the operating environment for an application, without requiring any
98 C<$name> is the name of the application, used in log messages, etc. C<$type>
99 is usualy one of "server" or "client". It specifies the subdirectory in which
100 debug logfiles will be created. C<$context> indicates the usual manner in
101 which this application is invoked; one of C<"cmdline"> for a user-invoked
102 command-line utility (e.g., C<amadmin>) which should send human-readable error
103 messages to stderr; C<"daemon"> for a program started by C<amandad>, e.g.,
104 C<sendbackup>; or C<"scriptutil"> for a small program used from shell scripts,
107 Based on C<$type> and C<$context>, this function does the following:
113 sets up debug logging;
117 configures internationalization
125 sets the current working directory to the debug or temporary directory;
129 closes any unnecessary file descriptors as a security meaasure;
133 ignores C<SIGPIPE>; and
137 sets the appropriate target for error messages.
143 # private package variables
148 sub setup_application {
149 my ($name, $type, $context) = @_;
152 croak("no name given") unless ($name);
153 croak("no type given") unless ($type);
154 croak("no context given") unless ($context);
156 # store these as perl values
159 $_pcontext = $context;
161 # and let the C side know about the pname
164 safe_cd(); # (also sets umask)
167 # set up debugging for this application type
171 $SIG{'PIPE'} = 'IGNORE';
173 set_erroutput_type($type, $context);
176 =item C<finish_setup($running_as_flags)>
178 Perform final initialization tasks that require a loaded configuration.
179 Specifically, move the debug log into a configuration-specific
180 subdirectory, and check that the current userid is appropriate for
183 The user is specified by one of the following flags, which are
184 available in export tag C<:check_running_as_flags>:
186 $RUNNING_AS_ROOT # root
187 $RUNNING_AS_DUMPUSER # dumpuser, from configuration
188 $RUNNING_AS_DUMPUSER_PREFERRED # dumpuser, but client_login is OK too
189 $RUNNING_AS_CLIENT_LOGIN # client_login (--with-user at build time)
191 If the flag C<$RUNNING_AS_UID_ONLY> is bit-or'd into C<$running_as_flags>, then
192 the euid is ignored; this is used for programs that expect to be setuid-root.
197 my ($running_as) = @_;
199 my $config_name = Amanda::Config::get_config_name();
202 dbrename($config_name, $_ptype);
205 check_running_as($running_as);
210 Return a "safe" environment hash. For non-setuid programs, this means filtering out any
211 localization variables.
218 delete @rv{qw(IFS CDPATH ENV BASH_ENV LANG)};
220 # delete all LC_* variables
221 for my $var (grep /^LC_/, keys %rv) {
229 push @EXPORT_OK, qw(running_as_flags_to_strings);
230 push @{$EXPORT_TAGS{"running_as_flags"}}, qw(running_as_flags_to_strings);
232 my %_running_as_flags_VALUES;
233 #Convert a flag value to a list of names for flags that are set.
234 sub running_as_flags_to_strings {
238 for my $k (keys %_running_as_flags_VALUES) {
239 my $v = $_running_as_flags_VALUES{$k};
241 #is this a matching flag?
242 if (($v == 0 && $flags == 0) || ($v != 0 && ($flags & $v) == $v)) {
247 #by default, just return the number as a 1-element list
255 push @EXPORT_OK, qw($RUNNING_AS_ROOT);
256 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_ROOT);
258 $_running_as_flags_VALUES{"RUNNING_AS_ROOT"} = $RUNNING_AS_ROOT;
260 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER);
261 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER);
263 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER"} = $RUNNING_AS_DUMPUSER;
265 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER_PREFERRED);
266 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER_PREFERRED);
268 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER_PREFERRED"} = $RUNNING_AS_DUMPUSER_PREFERRED;
270 push @EXPORT_OK, qw($RUNNING_AS_CLIENT_LOGIN);
271 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_CLIENT_LOGIN);
273 $_running_as_flags_VALUES{"RUNNING_AS_CLIENT_LOGIN"} = $RUNNING_AS_CLIENT_LOGIN;
275 push @EXPORT_OK, qw($RUNNING_AS_UID_ONLY);
276 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_UID_ONLY);
278 $_running_as_flags_VALUES{"RUNNING_AS_UID_ONLY"} = $RUNNING_AS_UID_ONLY;
281 fcntl(STDIN, F_GETFD, 0) or critical("Standard input is not open");
282 fcntl(STDOUT, F_GETFD, 0) or critical("Standard output is not open");
283 fcntl(STDERR, F_GETFD, 0) or critical("Standard error is not open");