Subject: lib/305: Programs using SysV IPC often wants to use ftok(3)
To: None <gnats-admin>
From: Thorsten Lockert <tholo@SigmaSoft.COM>
List: netbsd-bugs
Date: 06/24/1994 21:05:02
>Number:         305
>Category:       lib
>Synopsis:       ftok(3) not provided, programs using SysV IPC often wants it
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    gnats-admin (Library Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Fri Jun 24 21:05:02 1994
>Originator:     Thorsten Lockert
>Organization:
SigmaSoft, Th. Lockert
>Release:        current
>Environment:
	
System: NetBSD gandalf.bbb.no 0.9C NetBSD 0.9C (GANDALF) #2: Wed Jun 22 18:00:56 MET DST 1994 tholo@gandalf.bbb.no:/usr/src/sys/arch/i386/compile/GANDALF i386


>Description:
	Programs using the SysV IPC interface often wants to use the ftok(3)
	routine to generate a key.  This is not provided with NetBSD.
>How-To-Repeat:
	Find a run-of-the-mill program using SysV IPC and try to compile it
>Fix:
	The following shar archive contains the missing ftok(3) routine
	with a manpage as well as a patch to <sys/ipc.h> to add a prototype
	for the function.

##!/bin/sh
## This is a shell archive.  Save it in a file, remove anything before
## this line, and then unpack it by entering "sh file".  Note, it may
## create directories; files and directories will be owned by you and
## have default permissions.
##
## This archive contains:
##
##	ftok.3
##	ftok.c
##	ipc.h.diff
##
echo x - ftok.3
sed 's/^X//' >ftok.3 << 'END-of-ftok.3'
X.\" Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
X.\" All rights reserved.
X.\"
X.\" Redistribution and use in source and binary forms, with or without
X.\" modification, are permitted provided that the following conditions
X.\" are met:
X.\" 1. Redistributions of source code must retain the above copyright
X.\"    notice, this list of conditions and the following disclaimer.
X.\" 2. Redistributions in binary form must reproduce the above copyright
X.\"    notice, this list of conditions and the following disclaimer in the
X.\"    documentation and/or other materials provided with the distribution.
X.\" 3. The name of the author may not be used to endorse or promote products
X.\"    derived from this software without specific prior written permission.
X.\"
X.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
X.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
X.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
X.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
X.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
X.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
X.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
X.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
X.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X.\"
X.\"	$Id$
X.Dd June 24, 1994
X.Os
X.Dt ftok 3
X.Sh NAME
X.Nm ftok
X.Nd create IPC indentifier from path name
X.Sh SYNOPSIS
X.Fd #include <sys/types.h>
X.Fd #include <sys/ipc.h>
X.Ft key_t
X.Fn ftok "const char *path" "char id" ;
X.Sh DESCRIPTION
XThe
X.Fn ftok
Xfunction attempts to create a unique key suitable for use with the
X.Xr msgget 2 ,
X.Xr semget 2
Xand
X.Xr shmget 2
Xfunctions given the
X.Fa path
Xof an existing file and a user-selectable
X.Fa id .
X.Pp
XThe specified
X.Fa path
Xmust specify an existing file that is accessible to the calling process
Xor the call will fail.  Also, note that links to files will return the
Xsame key, given the same
X.Fa id .
X.Sh RETURN VALUES
XThe
X.Fn ftok
Xfunction will return -1 if
X.Fa path
Xdoes not exist or if it cannot be accessed by the calling process.
X.Sh SEE ALSO
X.Xr msgget 2 ,
X.Xr semget 2 ,
X.Xr shmget 2
X.Sh HISTORY
XThe
X.Fn ftok
Xfunction originate with System V and is typically used by programs
Xthat use the System V IPC routines.
X.Sh AUTHOR
X.Bl -tag
XThorsten Lockert <tholo@sigmasoft.com>
X.El
X.Sh BUGS
XThe returned key is computed based on the device and inode of the
Xspecified
X.Fa path
Xin combination with the given
X.Fa id .
XThus is is quite possible for the routine to return duplicate keys
Xgiven that those fields are not 8- and 16-bit quantities like they
Xwere on System V based systems where this library routine's ancestor
Xwere originally created.
END-of-ftok.3
echo x - ftok.c
sed 's/^X//' >ftok.c << 'END-of-ftok.c'
X/*
X * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
X * All rights reserved.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. The name of the author may not be used to endorse or promote products
X *    derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
X * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
X * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
X * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
X * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
X * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
X * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
X * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
X * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
X * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
X *
X *	$Id$
X */
X
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <sys/ipc.h>
X
Xkey_t
Xftok(path, id)
X	const char *path;
X	char id;
X{
X	struct stat st;
X
X	if (stat(path, &st) < 0)
X		return (key_t)-1;
X
X	return (key_t) (id << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff));
X}
END-of-ftok.c
echo x - ipc.h.diff
sed 's/^X//' >ipc.h.diff << 'END-of-ipc.h.diff'
X*** /sys/sys/ipc.h.old	Wed May 25 14:38:31 1994
X***************
X*** 85,88 ****
X--- 85,97 ----
X  #define	IXSEQ_TO_IPCID(ix,perm)	(((perm.seq) << 16) | (ix & 0xffff))
X  #endif /* KERNEL */
X  
X+ #ifndef KERNEL
X+ #include <sys/cdefs.h>
X+ 
X+ __BEGIN_DECLS
X+ #ifndef _POSIX_SOURCE
X+ key_t	ftok __P((const char *, char));
X+ #endif
X+ __END_DECLS
X+ #endif
X  #endif /* !_SYS_IPC_H_ */
END-of-ipc.h.diff
exit

>Audit-Trail:
>Unformatted:


------------------------------------------------------------------------------