Imported Upstream version 2.4.4p3
[debian/amanda] / common-src / util.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1999 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: util.c,v 1.2.2.2.4.3.2.1 2002/03/31 21:01:33 jrjackson Exp $
28  */
29
30 #include "amanda.h"
31 #include "util.h"
32
33 /*
34  * Keep calling read() until we've read buflen's worth of data, or EOF,
35  * or we get an error.
36  *
37  * Returns the number of bytes read, 0 on EOF, or negative on error.
38  */
39 ssize_t
40 fullread(fd, vbuf, buflen)
41     int fd;
42     void *vbuf;
43     size_t buflen;
44 {
45     ssize_t nread, tot = 0;
46     char *buf = vbuf;   /* cast to char so we can ++ it */
47
48     while (buflen > 0) {
49         nread = read(fd, buf, buflen);
50         if (nread < 0)
51             return (nread);
52         if (nread == 0)
53             break;
54         tot += nread;
55         buf += nread;
56         buflen -= nread;
57     }
58     return (tot);
59 }
60
61 /*
62  * Keep calling write() until we've written buflen's worth of data,
63  * or we get an error.
64  *
65  * Returns the number of bytes written, or negative on error.
66  */
67 ssize_t
68 fullwrite(fd, vbuf, buflen)
69     int fd;
70     const void *vbuf;
71     size_t buflen;
72 {
73     ssize_t nwritten, tot = 0;
74     const char *buf = vbuf;     /* cast to char so we can ++ it */
75
76     while (buflen > 0) {
77         nwritten = write(fd, buf, buflen);
78         if (nwritten < 0)
79             return (nwritten);
80         tot += nwritten;
81         buf += nwritten;
82         buflen -= nwritten;
83     }
84     return (tot);
85 }
86
87 /*
88  * Bind to a port in the given range.  Takes a begin,end pair of port numbers.
89  *
90  * Returns negative on error (EGAIN if all ports are in use).  Returns 0
91  * on success.
92  */
93 int
94 bind_portrange(s, addrp, first_port, last_port)
95     int s;
96     struct sockaddr_in *addrp;
97     int first_port, last_port;
98 {
99     int port, cnt;
100     const int num_ports = last_port - first_port + 1;
101     int save_errno;
102
103     assert(first_port > 0 && first_port <= last_port && last_port < 65536);
104
105     /*
106      * We pick a different starting port based on our pid and the current
107      * time to avoid always picking the same reserved port twice.
108      */
109     port = ((getpid() + time(0)) % num_ports) + first_port;
110
111     /*
112      * Scan through the range, trying all available ports.  Wrap around
113      * if we don't happen to start at the beginning.
114      */
115     for (cnt = 0; cnt < num_ports; cnt++) {
116         addrp->sin_port = htons(port);
117         if (bind(s, (struct sockaddr *)addrp, sizeof(*addrp)) >= 0)
118             return 0;
119         /*
120          * If the error was something other then port in use, stop.
121          */
122         if (errno != EADDRINUSE)
123             break;
124         if (++port > last_port)
125             port = first_port;
126     }
127     if (cnt == num_ports) {
128         dbprintf(("%s: bind_portrange: all ports between %d and %d busy\n",
129                   debug_prefix_time(NULL),
130                   first_port,
131                   last_port));
132         errno = EAGAIN;
133     } else if (last_port < IPPORT_RESERVED
134                && getuid() != 0
135                && errno == EACCES) {
136         /*
137          * Do not bother with an error message in this case because it
138          * is expected.
139          */
140     } else {
141         save_errno = errno;
142         dbprintf(("%s: bind_portrange: port %d: %s\n",
143                   debug_prefix_time(NULL),
144                   port,
145                   strerror(errno)));
146         errno = save_errno;
147     }
148     return -1;
149 }
150
151 /*
152  * Construct a datestamp (YYYYMMDD) from a time_t.
153  */
154 char *
155 construct_datestamp(t)
156     time_t *t;
157 {
158     struct tm *tm;
159     char datestamp[3*NUM_STR_SIZE];
160     time_t when;
161
162     if(t == NULL) {
163         when = time((time_t *)NULL);
164     } else {
165         when = *t;
166     }
167     tm = localtime(&when);
168     ap_snprintf(datestamp, sizeof(datestamp),
169                 "%04d%02d%02d", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
170     return stralloc(datestamp);
171 }
172
173 /*
174  * Construct a timestamp (YYYYMMDDHHMMSS) from a time_t.
175  */
176 char *
177 construct_timestamp(t)
178     time_t *t;
179 {
180     struct tm *tm;
181     char timestamp[6*NUM_STR_SIZE];
182     time_t when;
183
184     if(t == NULL) {
185         when = time((time_t *)NULL);
186     } else {
187         when = *t;
188     }
189     tm = localtime(&when);
190     ap_snprintf(timestamp, sizeof(timestamp),
191                 "%04d%02d%02d%02d%02d%02d",
192                 tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
193                 tm->tm_hour, tm->tm_min, tm->tm_sec);
194     return stralloc(timestamp);
195 }