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 same id value.
  • it should return different key values when called with different id values, 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 by path has 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 the path argument.
  • ENAMETOOLONG β€” the length of a component of a pathname is longer than {NAME_MAX}.
  • ENOENT β€” a component of path does not name an existing file, or path is 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 the path argument 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 the path argument.
  • 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>


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. πŸ’…πŸŽ€πŸ¦‹πŸŒΈ