xref: /onnv-gate/usr/src/lib/libc/port/gen/ftok.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56082Smarks  * Common Development and Distribution License (the "License").
66082Smarks  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
236082Smarks  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #pragma weak _ftok = ftok
33*6812Sraf 
34*6812Sraf #include "lint.h"
356082Smarks #include "libc.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/mkdev.h>
396082Smarks #include <sys/nvpair.h>
406082Smarks #include <fcntl.h>
416082Smarks #include <attr.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate key_t
ftok(const char * path,int id)440Sstevel@tonic-gate ftok(const char *path, int id)
450Sstevel@tonic-gate {
460Sstevel@tonic-gate 	struct stat64 st;
476082Smarks 	nvlist_t *nvp;
486082Smarks 	uint32_t devpiece;
496082Smarks 	int error;
500Sstevel@tonic-gate 
516082Smarks 	if (stat64(path, &st) < 0)
526082Smarks 		return ((key_t)-1);
536082Smarks 
546082Smarks 	/*
556082Smarks 	 * if getattrat works then extract the FSID from the nvlist
566082Smarks 	 * otherwise, just fall back and use the value from the stat data
576082Smarks 	 */
586082Smarks 	if ((error = getattrat(AT_FDCWD, XATTR_VIEW_READONLY,
596082Smarks 	    path, &nvp)) == 0) {
606082Smarks 		uint64_t value;
616082Smarks 
626082Smarks 		if ((error = libc_nvlist_lookup_uint64(nvp,
636082Smarks 		    A_FSID, &value)) == 0)
646082Smarks 			devpiece = value & 0xfff;
656082Smarks 		libc_nvlist_free(nvp);
666082Smarks 	}
676082Smarks 
686082Smarks 	if (error)
696082Smarks 		devpiece = st.st_dev & 0xfff;
706082Smarks 
716082Smarks 	return ((key_t)((key_t)id << 24 | devpiece << 12 |
726082Smarks 	    ((uint32_t)st.st_ino & 0x0fff)));
730Sstevel@tonic-gate }
74