1c1cb2cd8Shaad /*
2c1cb2cd8Shaad * CDDL HEADER START
3c1cb2cd8Shaad *
4c1cb2cd8Shaad * The contents of this file are subject to the terms of the
5c1cb2cd8Shaad * Common Development and Distribution License, Version 1.0 only
6c1cb2cd8Shaad * (the "License"). You may not use this file except in compliance
7c1cb2cd8Shaad * with the License.
8c1cb2cd8Shaad *
9c1cb2cd8Shaad * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10c1cb2cd8Shaad * or http://www.opensolaris.org/os/licensing.
11c1cb2cd8Shaad * See the License for the specific language governing permissions
12c1cb2cd8Shaad * and limitations under the License.
13c1cb2cd8Shaad *
14c1cb2cd8Shaad * When distributing Covered Code, include this CDDL HEADER in each
15c1cb2cd8Shaad * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16c1cb2cd8Shaad * If applicable, add the following below this CDDL HEADER, with the
17c1cb2cd8Shaad * fields enclosed by brackets "[]" replaced with your own identifying
18c1cb2cd8Shaad * information: Portions Copyright [yyyy] [name of copyright owner]
19c1cb2cd8Shaad *
20c1cb2cd8Shaad * CDDL HEADER END
21c1cb2cd8Shaad */
22c1cb2cd8Shaad /*
23c1cb2cd8Shaad * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24c1cb2cd8Shaad * Use is subject to license terms.
25c1cb2cd8Shaad */
26c1cb2cd8Shaad
27c1cb2cd8Shaad #pragma ident "%Z%%M% %I% %E% SMI"
28c1cb2cd8Shaad
29c1cb2cd8Shaad #include "libuutil_common.h"
30c1cb2cd8Shaad
31c1cb2cd8Shaad #include <sys/time.h>
32c1cb2cd8Shaad
33c1cb2cd8Shaad #include <errno.h>
34c1cb2cd8Shaad #include <fcntl.h>
35c1cb2cd8Shaad #include <limits.h>
36c1cb2cd8Shaad #include <stdio.h>
37c1cb2cd8Shaad #include <unistd.h>
38c1cb2cd8Shaad
39c1cb2cd8Shaad #ifdef _LP64
40c1cb2cd8Shaad #define TMPPATHFMT "%s/uu%ld"
41c1cb2cd8Shaad #else /* _LP64 */
42c1cb2cd8Shaad #define TMPPATHFMT "%s/uu%lld"
43c1cb2cd8Shaad #endif /* _LP64 */
44c1cb2cd8Shaad
45c1cb2cd8Shaad /*ARGSUSED*/
46c1cb2cd8Shaad int
uu_open_tmp(const char * dir,uint_t uflags)47c1cb2cd8Shaad uu_open_tmp(const char *dir, uint_t uflags)
48c1cb2cd8Shaad {
49c1cb2cd8Shaad int f;
50c1cb2cd8Shaad char *fname = uu_zalloc(PATH_MAX);
51c1cb2cd8Shaad
52c1cb2cd8Shaad if (fname == NULL)
53c1cb2cd8Shaad return (-1);
54c1cb2cd8Shaad
55c1cb2cd8Shaad for (;;) {
56*0ba9aea4Sriastradh (void) snprintf(fname, PATH_MAX, "%s/uu%lld", dir,
57*0ba9aea4Sriastradh (long long)gethrtime());
58c1cb2cd8Shaad
59c1cb2cd8Shaad f = open(fname, O_CREAT | O_EXCL | O_RDWR, 0600);
60c1cb2cd8Shaad
61c1cb2cd8Shaad if (f >= 0 || errno != EEXIST)
62c1cb2cd8Shaad break;
63c1cb2cd8Shaad }
64c1cb2cd8Shaad
65c1cb2cd8Shaad if (f >= 0)
66c1cb2cd8Shaad (void) unlink(fname);
67c1cb2cd8Shaad
68c1cb2cd8Shaad uu_free(fname);
69c1cb2cd8Shaad
70c1cb2cd8Shaad return (f);
71c1cb2cd8Shaad }
72