1FTOK(P) POSIX Programmer's Manual FTOK(P)
2
3
4
6 ftok - generate an IPC key
7
9 #include <sys/ipc.h>
10
11 key_t ftok(const char *path, int id);
12
13
15 The ftok() function shall return a key based on path and id that is
16 usable in subsequent calls to msgget(), semget(), and shmget(). The
17 application shall ensure that the path argument is the pathname of an
18 existing file that the process is able to stat().
19
20 The ftok() function shall return the same key value for all paths that
21 name the same file, when called with the same id value, and return dif‐
22 ferent key values when called with different id values or with paths
23 that name different files existing on the same file system at the same
24 time. It is unspecified whether ftok() shall return the same key value
25 when called again after the file named by path is removed and recreated
26 with the same name.
27
28 Only the low-order 8-bits of id are significant. The behavior of ftok()
29 is unspecified if these bits are 0.
30
32 Upon successful completion, ftok() shall return a key. Otherwise,
33 ftok() shall return (key_t)-1 and set errno to indicate the error.
34
36 The ftok() function shall fail if:
37
38 EACCES Search permission is denied for a component of the path prefix.
39
40 ELOOP A loop exists in symbolic links encountered during resolution of
41 the path argument.
42
43 ENAMETOOLONG
44 The length of the path argument exceeds {PATH_MAX} or a pathname
45 component is longer than {NAME_MAX}.
46
47 ENOENT A component of path does not name an existing file or path is an
48 empty string.
49
50 ENOTDIR
51 A component of the path prefix is not a directory.
52
53
54 The ftok() function may fail if:
55
56 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
57 resolution of the path argument.
58
59 ENAMETOOLONG
60 Pathname resolution of a symbolic link produced an intermediate
61 result whose length exceeds {PATH_MAX}.
62
63
64 The following sections are informative.
65
67 Getting an IPC Key
68 The following example gets a unique key that can be used by the IPC
69 functions semget(), msgget(), and shmget(). The key returned by ftok()
70 for this example is based on the ID value S and the pathname /tmp.
71
72
73 #include <sys/ipc.h>
74 ...
75 key_t key;
76 char *path = "/tmp";
77 int id = 'S';
78
79
80 key = ftok(path, id);
81
82 Saving an IPC Key
83 The following example gets a unique key based on the pathname /tmp and
84 the ID value a. It also assigns the value of the resulting key to the
85 semkey variable so that it will be available to a later call to
86 semget(), msgget(), or shmget().
87
88
89 #include <sys/ipc.h>
90 ...
91 key_t semkey;
92
93
94 if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) {
95 perror("IPC error: ftok"); exit(1);
96 }
97
99 For maximum portability, id should be a single-byte character.
100
102 None.
103
105 None.
106
108 msgget() , semget() , shmget() , the Base Definitions volume of
109 IEEE Std 1003.1-2001, <sys/ipc.h>
110
112 Portions of this text are reprinted and reproduced in electronic form
113 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
114 -- Portable Operating System Interface (POSIX), The Open Group Base
115 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
116 Electrical and Electronics Engineers, Inc and The Open Group. In the
117 event of any discrepancy between this version and the original IEEE and
118 The Open Group Standard, the original IEEE and The Open Group Standard
119 is the referee document. The original Standard can be obtained online
120 at http://www.opengroup.org/unix/online.html .
121
122
123
124IEEE/The Open Group 2003 FTOK(P)