xref: /titanic_52/usr/src/lib/libc/port/gen/ftok.c (revision 98157a7002f4f2cf7978f3084ca5577f0a1d72b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 
32 #pragma weak ftok = _ftok
33 #include "synonyms.h"
34 #include "libc.h"
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/mkdev.h>
38 #include <sys/nvpair.h>
39 #include <fcntl.h>
40 #include <attr.h>
41 
42 key_t
43 ftok(const char *path, int id)
44 {
45 	struct stat64 st;
46 	nvlist_t *nvp;
47 	uint32_t devpiece;
48 	int error;
49 
50 	if (stat64(path, &st) < 0)
51 		return ((key_t)-1);
52 
53 	/*
54 	 * if getattrat works then extract the FSID from the nvlist
55 	 * otherwise, just fall back and use the value from the stat data
56 	 */
57 	if ((error = getattrat(AT_FDCWD, XATTR_VIEW_READONLY,
58 	    path, &nvp)) == 0) {
59 		uint64_t value;
60 
61 		if ((error = libc_nvlist_lookup_uint64(nvp,
62 		    A_FSID, &value)) == 0)
63 			devpiece = value & 0xfff;
64 		libc_nvlist_free(nvp);
65 	}
66 
67 	if (error)
68 		devpiece = st.st_dev & 0xfff;
69 
70 	return ((key_t)((key_t)id << 24 | devpiece << 12 |
71 	    ((uint32_t)st.st_ino & 0x0fff)));
72 }
73