FTOK(3POSIX)
π FTOK(3POSIX) π
okay so bestie, welcome to the POSIX Programmer's Manual page for ftok, from the POSIX Programmer's Manual. quick heads up before we dive in: this whole page is part of the POSIX Programmer's Manual, and the Linux implementation of this interface may be different from what's written here (so pls go check the corresponding Linux manual page for the actual Linux behavior), or honestly the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
ftok β generate an IPC key ποΈ
πΈ SYNOPSIS πΈ
#include <sys/ipc.h>
key_t ftok(const char *path, int id);
π¦ DESCRIPTION π¦
so the ftok() function shall return a key based on path and id, and that key is usable in subsequent calls to msgget(), semget(), and shmget(). no cap, that's literally the whole job.
your application shall ensure that the path argument is the pathname of an existing file that the process is able to stat(). the one exception, and it's a cute one: if stat() would fail with [EOVERFLOW] because of the file size, ftok() shall still succeed. iconic behavior honestly. π
here's the tea on how keys come out:
ftok()shall return the same key value for all paths that name the same file, when it's called with the sameidvalue.- it should return different key values when called with different
idvalues, or with paths that name different files that exist on the same file system at the same time. - it is unspecified whether
ftok()shall return the same key value when you call it again after the file named bypathhas been removed and recreated with the same name. so likeβ¦ don't count on it, bestie. π
and the spicy bit: only the low-order 8 bits of id are significant. the behavior of ftok() is unspecified if those bits are 0. so if you zero those out, you're in undefined-vibes territory, and not in the fun way. π
π«Ά RETURN VALUE π«Ά
upon successful completion, ftok() shall return a key. slay. β¨
otherwise, ftok() shall return (key_t)-1 and set errno to indicate the error. not slay. π
π₯ ERRORS π₯
the ftok() function shall fail if:
EACCESβ search permission is denied for a component of the path prefix.EIOβ an error occurred while reading from the file system.ELOOPβ a loop exists in symbolic links encountered during resolution of thepathargument.ENAMETOOLONGβ the length of a component of a pathname is longer than{NAME_MAX}.ENOENTβ a component ofpathdoes not name an existing file, orpathis an empty string.ENOTDIRβ a component of the path prefix names an existing file that is neither a directory nor a symbolic link to a directory, or thepathargument contains at least one non-<slash>character and ends with one or more trailing<slash>characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
the ftok() function may fail if:
ELOOPβ more than{SYMLOOP_MAX}symbolic links were encountered during resolution of thepathargument.ENAMETOOLONGβ the length of a pathname exceeds{PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds{PATH_MAX}.
the following sections are informative. π (yes that means they're the bonus content, not the legally-binding stuff)
π EXAMPLES π
π Getting an IPC Key π
the following example gets a key based on the pathname /tmp and the ID value a. it also assigns the value of the resulting key to the semkey variable so that it'll be available to a later call to semget(), msgget(), or shmget(). bestie, look at that error handling β we love to see it. π«Ά
#include <sys/ipc.h>
...
key_t semkey;
if ((semkey = ftok("/tmp", 'a')) == (key_t) -1) {
perror("IPC error: ftok"); exit(1);
}
π APPLICATION USAGE π
for maximum portability, id should be a single-byte character. fr fr, keep it simple.
applications should not assume that the resulting key value is unique. ngl, this one bites people. π
π§ RATIONALE π§
None.
literally none. the rationale section said "no thoughts, head empty." πβ¨
π FUTURE DIRECTIONS π
future versions of this standard may add new interfaces to provide unique keys. so likeβ¦ watch this space, iykyk. β
π SEE ALSO π
msgget()semget()shmget()
The Base Definitions volume of POSIX.1β2017, <sys_ipc.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! this diva of a manual page was brought to you by IEEE/The Open Group, 2017, and it lives at FTOK(3POSIX). periodt. π ππ¦πΈ
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms