]>
git.ipfire.org Git - people/ms/dma.git/blob - util.c
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
53 static char name
[MAXHOSTNAMELEN
+1];
54 static int initialized
= 0;
62 if (config
.mailname
!= NULL
&& config
.mailname
[0] != '\0') {
63 snprintf(name
, sizeof(name
), "%s", config
.mailname
);
67 if (config
.mailnamefile
!= NULL
&& config
.mailnamefile
[0] != '\0') {
68 fp
= fopen(config
.mailnamefile
, "r");
70 res
= fgets(name
, sizeof(name
), fp
);
75 (name
[len
- 1] == '\r' ||
76 name
[len
- 1] == '\n'))
78 if (name
[0] != '\0') {
85 if (gethostname(name
, sizeof(name
)) != 0)
86 strcpy(name
, "(unknown hostname)");
92 setlogident(const char *fmt
, ...)
101 if (vasprintf(&sufx
, fmt
, ap
) != -1 && sufx
!= NULL
) {
102 if (asprintf(&tag
, "%s[%s]", logident_base
, sufx
) == -1)
109 openlog(tag
!= NULL
? tag
: logident_base
, 0, LOG_MAIL
);
113 errlog(int exitcode
, const char *fmt
, ...)
121 if (vasprintf(&outs
, fmt
, ap
) == -1)
127 syslog(LOG_ERR
, "%s: %m", outs
);
128 fprintf(stderr
, "%s: %s: %s\n", getprogname(), outs
, strerror(oerrno
));
130 syslog(LOG_ERR
, "%m");
131 fprintf(stderr
, "%s: %s\n", getprogname(), strerror(oerrno
));
138 errlogx(int exitcode
, const char *fmt
, ...)
145 if (vasprintf(&outs
, fmt
, ap
) == -1)
151 syslog(LOG_ERR
, "%s", outs
);
152 fprintf(stderr
, "%s: %s\n", getprogname(), outs
);
154 syslog(LOG_ERR
, "Unknown error");
155 fprintf(stderr
, "%s: Unknown error\n", getprogname());
162 check_username(const char *name
, uid_t ckuid
)
168 pwd
= getpwnam(name
);
169 if (pwd
== NULL
|| pwd
->pw_uid
!= ckuid
)
182 username
= check_username(getlogin(), uid
);
183 if (username
!= NULL
)
185 username
= check_username(getenv("LOGNAME"), uid
);
186 if (username
!= NULL
)
188 username
= check_username(getenv("USER"), uid
);
189 if (username
!= NULL
)
192 if (pwd
!= NULL
&& pwd
->pw_name
!= NULL
&& pwd
->pw_name
[0] != '\0' &&
193 (u
= strdup(pwd
->pw_name
)) != NULL
) {
194 username
= check_username(u
, uid
);
195 if (username
!= NULL
)
200 if (asprintf(__DECONST(void *, &username
), "%ld", (long)uid
) != -1 &&
203 username
= "unknown-or-invalid-username";
211 SLIST_FOREACH(t
, &tmpfs
, next
) {
216 static sigjmp_buf sigbuf
;
217 static int sigbuf_valid
;
220 sigalrm_handler(int signo
)
222 (void)signo
; /* so that gcc doesn't complain */
224 siglongjmp(sigbuf
, 1);
228 do_timeout(int timeout
, int dojmp
)
230 struct sigaction act
;
233 sigemptyset(&act
.sa_mask
);
237 act
.sa_handler
= sigalrm_handler
;
238 if (sigaction(SIGALRM
, &act
, NULL
) != 0)
239 syslog(LOG_WARNING
, "can not set signal handler: %m");
241 ret
= sigsetjmp(sigbuf
, 1);
244 /* else just programmed */
253 act
.sa_handler
= SIG_IGN
;
254 if (sigaction(SIGALRM
, &act
, NULL
) != 0)
255 syslog(LOG_WARNING
, "can not remove signal handler: %m");
263 open_locked(const char *fname
, int flags
, ...)
267 if (flags
& O_CREAT
) {
270 mode
= va_arg(ap
, int);
277 fd
= open(fname
, flags
, mode
);
280 if (flock(fd
, LOCK_EX
|((flags
& O_NONBLOCK
)? LOCK_NB
: 0)) < 0) {
288 return(open(fname
, flags
|O_EXLOCK
, mode
));
300 error
= strftime(str
, sizeof(str
), "%a, %d %b %Y %T %z",
303 strcpy(str
, "(date fail)");
308 strprefixcmp(const char *str
, const char *prefix
)
310 return (strncasecmp(str
, prefix
, strlen(prefix
)));
319 rf
= open("/dev/urandom", O_RDONLY
);
321 rf
= open("/dev/random", O_RDONLY
);
323 if (!(rf
!= -1 && read(rf
, &seed
, sizeof(seed
)) == sizeof(seed
)))
324 seed
= (time(NULL
) ^ getpid()) + (uintptr_t)&seed
;