xref: /titanic_41/usr/src/lib/libc/port/gen/ftok.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d8ac9b2dSmarks  * Common Development and Distribution License (the "License").
6d8ac9b2dSmarks  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23d8ac9b2dSmarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
32*7257d1b4Sraf #pragma weak _ftok = ftok
33*7257d1b4Sraf 
34*7257d1b4Sraf #include "lint.h"
35d8ac9b2dSmarks #include "libc.h"
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
39d8ac9b2dSmarks #include <sys/nvpair.h>
40d8ac9b2dSmarks #include <fcntl.h>
41d8ac9b2dSmarks #include <attr.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate key_t
ftok(const char * path,int id)447c478bd9Sstevel@tonic-gate ftok(const char *path, int id)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	struct stat64 st;
47d8ac9b2dSmarks 	nvlist_t *nvp;
48d8ac9b2dSmarks 	uint32_t devpiece;
49d8ac9b2dSmarks 	int error;
507c478bd9Sstevel@tonic-gate 
51d8ac9b2dSmarks 	if (stat64(path, &st) < 0)
52d8ac9b2dSmarks 		return ((key_t)-1);
53d8ac9b2dSmarks 
54d8ac9b2dSmarks 	/*
55d8ac9b2dSmarks 	 * if getattrat works then extract the FSID from the nvlist
56d8ac9b2dSmarks 	 * otherwise, just fall back and use the value from the stat data
57d8ac9b2dSmarks 	 */
58d8ac9b2dSmarks 	if ((error = getattrat(AT_FDCWD, XATTR_VIEW_READONLY,
59d8ac9b2dSmarks 	    path, &nvp)) == 0) {
60d8ac9b2dSmarks 		uint64_t value;
61d8ac9b2dSmarks 
62d8ac9b2dSmarks 		if ((error = libc_nvlist_lookup_uint64(nvp,
63d8ac9b2dSmarks 		    A_FSID, &value)) == 0)
64d8ac9b2dSmarks 			devpiece = value & 0xfff;
65d8ac9b2dSmarks 		libc_nvlist_free(nvp);
66d8ac9b2dSmarks 	}
67d8ac9b2dSmarks 
68d8ac9b2dSmarks 	if (error)
69d8ac9b2dSmarks 		devpiece = st.st_dev & 0xfff;
70d8ac9b2dSmarks 
71d8ac9b2dSmarks 	return ((key_t)((key_t)id << 24 | devpiece << 12 |
72d8ac9b2dSmarks 	    ((uint32_t)st.st_ino & 0x0fff)));
737c478bd9Sstevel@tonic-gate }
74