Imported Upstream version 3.1.0
[debian/amanda] / perl / Amanda / Util.pod
1 /*
2  * Copyright (c) 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 %perlcode %{
22
23 =head1 NAME
24
25 Amanda::Util - Runtime support for Amanda applications
26
27 =head1 Application Initialization
28
29 Application initialization generally looks like this:
30
31   use Amanda::Config qw( :init );
32   use Amanda::Util qw( :constants );
33   use Amanda::Debug;
34
35   Amanda::Util::setup_application("myapp", "server", $CONTEXT_CMDLINE);
36   # .. command-line processing ..
37   Amanda::Config::config_init(...);
38   Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
39   # ..
40   Amanda::Util::finish_application();
41
42 =over
43
44 =item setup_application($name, $type, $context)
45
46 Set up the operating environment for an application, without requiring
47 any configuration.
48
49 C<$name> is the name of the application, used in log messages, etc.
50 C<$type> is usualy one of "server" or "client".  It specifies the
51 subdirectory in which debug logfiles will be created.  C<$context>
52 indicates the usual manner in which this application is invoked; one
53 of C<$CONTEXT_CMDLINE> for a user-invoked command-line utility (e.g.,
54 C<amadmin>) which should send human-readable error messages to stderr;
55 C<$CONTEXT_DAEMON> for a program started by C<amandad>, e.g.,
56 C<sendbackup>; or C<$CONTEXT_SCRIPTUTIL> for a small program used from
57 shell scripts, e.g., C<amgetconf>
58
59 Based on C<$type> and C<$context>, this function does the following:
60
61 =over
62
63 =item *
64
65 sets up debug logging;
66
67 =item *
68
69 configures internationalization
70
71 =item *
72
73 sets the umask;
74
75 =item *
76
77 sets the current working directory to the debug or temporary
78 directory;
79
80 =item *
81
82 closes any unnecessary file descriptors as a security meaasure;
83
84 =item *
85
86 ignores C<SIGPIPE>; and
87
88 =item *
89
90 sets the appropriate target for error messages.
91
92 =back
93
94 =item finish_setup($running_as_flags)
95
96 Perform final initialization tasks that require a loaded
97 configuration.  Specifically, move the debug log into a
98 configuration-specific subdirectory, and check that the current userid
99 is appropriate for this applciation.
100
101 The user is specified by one of the following flags, which are
102 available in export tag C<:check_running_as_flags>:
103
104   $RUNNING_AS_ANY                 # any user is OK
105   $RUNNING_AS_ROOT                # root
106   $RUNNING_AS_DUMPUSER            # dumpuser, from configuration
107   $RUNNING_AS_DUMPUSER_PREFERRED  # dumpuser, but client_login is OK too
108   $RUNNING_AS_CLIENT_LOGIN        # client_login (--with-user at build time)
109
110 If the flag C<$RUNNING_AS_UID_ONLY> is bit-or'd into
111 C<$running_as_flags>, then the euid is ignored; this is used for
112 programs that expect to be setuid-root.
113
114 =item finish_application()
115
116 Remove old debug files.
117 All applications should call this before exiting.
118
119 =item get_original_cwd()
120
121 Return the original current directory with C<get_original_cwd>.
122
123 =item version_opt()
124
125 Print the version and exit.  This is intended to be used in C<GetOptions> invocations, e.g.,
126
127   GetOptions(
128     # ...
129     'version' => \&Amanda::Util::version_opt,
130   );
131
132 =back
133
134 =head1 File Handling
135
136 These functions read and write the entire requested size to a file
137 descriptor, even if the underlying syscall returns early.  Note that
138 they do not operate on Perl file handles.
139
140 If fewer than C<$size> bytes are written, C<full_write> returns the
141 number of bytes actually written and sets C<$!> appropriately.  When
142 reading, if fewer than C<$size> bytes are read due to a normal EOF,
143 then C<$!> is zero; otherwise, it contains the appropriate error
144 message.
145
146 Unlike C<POSIX::read>, C<full_read> returns a scalar containing the
147 bytes it read from the file descriptor.
148
149 =over
150
151 =item full_read($fd, $size)
152
153 =item full_write($fd, $buf, $size)
154
155 =back
156
157 =head1 Miscellaneous Utilities
158
159 =over
160
161 =item safe_env()
162
163 Return a "safe" environment hash.  For non-setuid programs, this means
164 filtering out any localization variables.
165
166 =item get_fs_usage(file, disk)
167
168 This is a wrapper around the Gnulib function of the same name.  On success, it returns
169 a hash with keys:
170
171   blocksize           Size of a block
172   blocks              Total blocks on disk
173   bfree               Free blocks available to superuser
174   bavail              Free blocks available to non-superuser
175   bavail_top_bit_set  1 if fsu_bavail represents a value < 0
176   files               Total file nodes
177   ffree               Free file nodes
178
179 On failure, it returns nothing, and C<$!> should be set.  If C<$!> is 0, then
180 this is a system which cannot measure usage without a C<disk> argument, which
181 this wrapper does not support.
182
183 =item is_pid_alive(pid)
184
185 Return 1 is the process with that pid is still alive.
186
187 =item weaken_ref($ref)
188
189 This is exactly the same as C<Scalar::Util::weaken>, but available in all
190 supported versions of perl.
191
192 =item gettimeofday()
193
194 Return the number of microseconds since the UNIX epoch.
195
196 =item fsync($fd)
197
198 Invoke the C<fsync> syscall.
199
200 =item set_blocking($fd, $blocking)
201
202 Set or clear the C<O_NONBLOCK> fd flag on $fd; returns a negative value on
203 failure, or 0 on success.
204
205 =item openbsd_fd_inform()
206
207 Due to a particularly poor user-space implementation of threading on OpenBSD,
208 executables that are run with nonstandard file descriptors open (fd > 2) find
209 those descriptors to be in a nonblocking state.  This particularly affects
210 amandad services, which begin with several file descriptors in the 50's open.
211
212 This function "informs" the C library about these descriptors by making an
213 C<fcntl(fd, F_GETFL)> call.  This is otherwise harmless, and is only perfomed
214 on OpenBSD.
215
216 =item built_with_component($comp)
217
218 Returns true if Amanda was built with the given component.  Component names are
219 in C<config/amanda/components.m4>.
220
221 =back
222
223 =head1 TCP Utilities
224
225 These are thin wrappers over functions in C<common-src/stream.h> and other related
226 functions.
227
228 =over
229
230 =item stream_server
231
232     my $family = $Amanda::Util::AF_INET;
233     my $bufsize = $Amanda::Util::STREAM_BUFSIZE;
234     my ($listensock, $port) = Amanda::Util::stream_server(
235             $family, $bufsize, $bufsize, $priv);
236
237 This function creates a new socket and binds it to a port, returning both the
238 socket and port.  If the socket is -1, then an error occurred and is available
239 in C<$!>.  The constants C<$AF_INET> and C<$STREAM_BUFSIZE> are universally
240 used when calling this function.  If the final argument, C<$priv>, is true,
241 then a the function opens a privileged port (below 1024).
242
243 =item stream_accept
244
245     my $sock = Amanda::Util::stream_accept(
246             $listen_sock, $timeout, $bufsize, $bufsize);
247
248 This function accepts a connection on a listening socket.  If the connection is
249 not made within C<$timeout> seconds, or some other error occurs, then the
250 function returns -1.  The bufsize arguments are applied to the new socket.
251
252 =item check_security
253
254     my $ok = Amanda::Util::check_security($socket, $userstr);
255
256 This function takes a socket descriptor and a string of the form C<"USER foo">
257 and performs BSD-style checks on that descriptor.  These include verifying
258 round-trip DNS sanity; check that the user is in C<.rhosts> or C<.amandahosts>,
259 and checking that the remote port is reserved.  Returns an error string on
260 error, or C<undef> on success.
261
262 =back
263
264 =head1 String Utilities
265
266 =over
267
268 =item quote_string($str)
269
270 Quote a string using Amanda's quoting algorithm.  Strings with no
271 whitespace, control, or quote characters are returned unchanged.  An
272 empty string is represented as the two-character string C<"">.
273 Otherwise, tab, newline, carriage return, form-feed, backslash, and
274 double-quote (C<">) characters are escaped with a backslash and the
275 string is surrounded by double quotes.
276
277 =item unquote_string($str)
278
279 Unquote a string as quoted with C<quote_string>.
280
281 =item skip_quoted_string($str)
282
283 my($q, $remaider) = skip_quoted_string($str)
284
285 Return the first quoted string and the remainder of the string.
286
287 =item C<split_quoted_strings($str)>
288
289 Split string on unquoted whitespace.  Multiple consecutive spaces are not
290 collapsed into a single space: C<"x  y"> (with two spaces) parses as C<( "x",
291 "", "y")>.  The strings are unquoted before they are returned.  An empty string
292 is split into C<( "" )>.
293
294 All of these quoting-related functions are available under the export
295 tag C<:quoting>.
296
297 =item hexencode($str)
298
299 Encode a string using URI-style hexadecimal encoding.
300 Non-alphanumeric characters will be replaced with "%xx"
301 where "xx" is the two-digit hexadecimal representation of the character.
302
303 =item hexdecode($str)
304
305 Decode a string using URI-style hexadecimal encoding.
306
307 Both C<hexencode> and C<hexdecode> are available under the export tag C<:encoding>
308
309 =item expand_braced_alternates($str)
310 =item collapse_braced_alternates(\@list)
311
312 These two functions handle "braced alternates", which is a syntax
313 borrowed, partially, from shells.  Comma-separated strings enclosed in
314 curly braces expand into multiple alternatives for the entire string.
315 For example:
316
317   "{foo,bar,bat}"   [ "foo", "bar", "bat" ]
318   "foo{1,2}bar"     [ "foo1bar", "foo2bar" ]
319   "foo{1\,2,3}bar"  [ "foo1,2bar", "foo3bar" ]
320   "{a,b}-{1,2}"     [ "a-1", "a-2", "b-1", "b-2" ]
321
322 Note that nested braces are not processed.  Braces, commas, and
323 backslashes may be escaped with backslashes.  On error,
324 C<expand_braced_altnerates> returns undef.  These two functions are
325 available in the export tag C<:alternates>.
326
327 =item generate_timestamp()
328
329 Generate a timestamp from the current time, obeying the
330 'USETIMESTAMPS' config parameter.  The Amanda configuration must
331 already be loaded.
332
333 =item sanitise_filename($fn)
334
335 "Santitises" a filename by replacing any characters that might have special
336 meaning to a filesystem with underscores.  This operation is I<not> reversible,
337 and distinct input filenames I<may> produce identical output filenames.
338
339 =item unmarshal_tapespec($tapespec)
340 =item marshal_tapespec($filelist)
341
342 These functions convert between a tapespec -- formerly, and confusingly, called
343 a "tapelist" -- and a perl data structure like
344
345     [   $label1 => [ $filenum1, $filenum2, .. ],
346         $label2 => [ $filenum1, $filenum2, .. ],
347     ]
348
349 Note that a non-tapespec C<$string> will be unmarshalled as C<[ $string, [] ]>.
350
351 =back
352
353 =head1 Locking Files
354
355 Amanda provides a basic mechanism to lock a file and read its contents.  This
356 uses operating-system facilities to acquire an advisory lock, so non-Amanda
357 applications are not prevented from modifying the file while it is locked.
358
359 To create a lock object, call the C<file_lock> constructor, passing the
360 filename to lock:
361
362   my $fl = Amanda::Util::file_lock->new($filename)
363
364 then, lock the file:
365
366   $fl->lock();
367
368 which also reads the contents of the file into memory, accessible via
369
370   my $state = $fl->data();
371
372 to change the file contents, call C<write>:
373
374   $fl->write($new_contents);
375
376 and unlock the lock with
377
378   $fl->unlock();
379
380 Note that the file will be automatically unlocked if the C<file_lock> object is
381 garbage-collected.
382
383 =head1 Simple File Reading & Writing
384
385 For reading small files directly into memory with little code
386 overhead, we can use C<slurp>.
387
388   my $data = slurp $filename;
389
390 After processing the data, we can write it back to file with C<burp>.  This
391 function always completely overwrites the file.
392
393   burp $filename, $header;
394
395 These functions can (and should) be exported to the main namespace
396   
397 =cut
398
399 %}