Mkstemp

Last updated

In computing, mkstemp is a POSIX function for creating a temporary file (a computer file which usually ceases to exist when the program, which opened the file, closes it or terminates). [1] It accepts an argument that determines the location of the temporary file, and the prefix of its generated filename. [1] After mkstemp was added to the Single UNIX Specification, the function tmpnam() was deprecated, [1] because the latter carried the risk that a temporary file with the same name could be created by another thread or process within the time from when the caller obtains the temporary filename and attempts to create it. [2] mkstemp does not suffer from this problem. [3]

Contents

Usage

Inclusion

C
#include<stdlib.h> // per IEEE Std 1003.1, 2004#include<unistd.h> // for "legacy" systems
C++
#include<cstdlib>  // per IEEE Std 1003.1, 2004#include<unistd.h> // for "legacy" systems

Declaration

int mkstemp(char* template);

Requirements

Semantics

Example

The following code is an example of the usage of mkstemp; the local variable filename is modified by mkstemp and will contain the path to the new file: [4]

#include<stdlib.h>voidexample(){charfilename[]="/tmp/prefXXXXXX";mkstemp(filename);}

Error conditions

It is unspecified if mkstemp sets errno, and what values of errno are set, in the event of failure. [1]

Mechanism

The mkstemp function generates a filename according to the supplied argument for the template, and attempts to create it. It repeats this process until a file has been successfully created. [5] After this, it opens the file and returns the file descriptor to the caller, [6] with the data buffer that was passed to the function with the template now containing the new filename. [7] The file can be deleted immediately after the mkstemp call returns to prevent other processes from opening it, but the file can still be used because the calling process will still have a valid file descriptor. [5] Older versions of mkstemp created the file with an umask of 0666, resulting in the temporary files being readable and writable to all users, and thus presenting a security vulnerability; this is mitigated by setting the umask manually before calling mkstemp. [6] Newer versions of the function create the file with the umask 600, so that only the owner of the file may read from and write to it. [7]

See also

Related Research Articles

The Single UNIX Specification (SUS) is a standard for computer operating systems, compliance with which is required to qualify for using the "UNIX" trademark. The standard specifies programming interfaces for the C language, a command-line shell, and user commands. The core specifications of the SUS known as Base Specifications are developed and maintained by the Austin Group, which is a joint working group of IEEE, ISO/IEC JTC 1/SC 22/WG 15 and The Open Group. If an operating system is submitted to The Open Group for certification, and passes conformance tests, then it is deemed to be compliant with a UNIX standard such as UNIX 98 or UNIX 03.

Berkeley sockets is an application programming interface (API) for Internet sockets and Unix domain sockets, used for inter-process communication (IPC). It is commonly implemented as a library of linkable modules. It originated with the 4.2BSD Unix operating system, which was released in 1983.

In computer programming, standard streams are preconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console, but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.

The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>. The functionality descends from a "portable I/O package" written by Mike Lesk at Bell Labs in the early 1970s, and officially became part of the Unix operating system in Version 7.

The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. Starting from the original ANSI C standard, it was developed at the same time as the C library POSIX specification, which is a superset of it. Since ANSI C was adopted by the International Organization for Standardization, the C standard library is also called the ISO C library.

errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno.

<span class="mw-page-title-main">Not a typewriter</span> Unix error code

In computing, "Not a typewriter" or ENOTTY is an error code defined in the errno.h found on many Unix systems. This code is now used to indicate that an invalid ioctl number was specified in an ioctl system call.

In Unix and Unix-like computer operating systems, a file descriptor is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket.

In computing, POSIX Threads, commonly known as pthreads, is an execution model that exists independently from a programming language, as well as a parallel execution model. It allows a program to control multiple different flows of work that overlap in time. Each flow of work is referred to as a thread, and creation and control over these flows is achieved by making calls to the POSIX Threads API. POSIX Threads is an API defined by the Institute of Electrical and Electronics Engineers (IEEE) standard POSIX.1c, Threads extensions .

In computing, vectored I/O, also known as scatter/gather I/O, is a method of input and output by which a single procedure call sequentially reads data from multiple buffers and writes it to a single data stream (gather), or reads data from a data stream and writes it to multiple buffers (scatter), as defined in a vector of buffers. Scatter/gather refers to the process of gathering data from, or scattering data into, the given set of buffers. Vectored I/O can operate synchronously or asynchronously. The main reasons for using vectored I/O are efficiency and convenience.

In computer science and software engineering, busy-waiting, busy-looping or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available. Spinning can also be used to generate an arbitrary time delay, a technique that was necessary on systems that lacked a method of waiting a specific length of time. Processor speeds vary greatly from computer to computer, especially as some processors are designed to dynamically adjust speed based on current workload. Consequently, spinning as a time-delay technique can produce unpredictable or even inconsistent results on different systems unless code is included to determine the time a processor takes to execute a "do nothing" loop, or the looping code explicitly checks a real-time clock.

stat (system call) Unix system call

stat is a Unix system call that returns file attributes about an inode. The semantics of stat vary between operating systems. As an example, Unix command ls uses this system call to retrieve information on files that includes:

Signals are standardized messages sent to a running program to trigger specific behavior, such as quitting or error handling. They are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-compliant operating systems.

In computing, umask is a command that determines the settings of a mask that controls how file permissions are set for newly created files. It may also affect how the file permissions are changed explicitly. umask is also a function that sets the mask, or it may refer to the mask itself, which is formally known as the file mode creation mask. The mask is a grouping of bits, each of which restricts how its corresponding permission is set for newly created files. The bits in the mask may be changed by invoking the umask command.

For most file systems, a program initializes access to a file in a file system using the open system call. This allocates resources associated to the file, and returns a handle that the process will use to refer to that file. In some cases the open is performed by the first access.

In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay. It is especially important in Unix-like systems, although it also exists elsewhere. As no new process is created, the process identifier (PID) does not change, but the machine code, data, heap, and stack of the process are replaced by those of the new program.

setcontext is one of a family of C library functions used for context control. The setcontext family allows the implementation in C of advanced control flow patterns such as iterators, fibers, and coroutines. They may be viewed as an advanced version of setjmp/longjmp; whereas the latter allows only a single non-local jump up the stack, setcontext allows the creation of multiple cooperative threads of control, each with its own stack.

select is a system call and application programming interface (API) in Unix-like and POSIX-compliant operating systems for examining the status of file descriptors of open input/output channels. The select system call is similar to the poll facility introduced in UNIX System V and later operating systems. However, with the c10k problem, both select and poll have been superseded by the likes of kqueue, epoll, /dev/poll and I/O completion ports.

Getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems. It is also the name of a Unix program for parsing command line arguments in shell scripts.

In Unix-like operating systems, dup and dup2 system calls create a copy of a given file descriptor. This new descriptor actually does not behave like a copy, but like an alias of the old one.

References

  1. 1 2 3 4 mkstemp by OpenGroup
  2. "tempnam". Open Group Base Specifications (Issue 7 ed.). OpenGroup. 2018.
  3. Stevens, W. Richard; Rago, Stephen A. (2013). "Standard Library Functions". Temporary Files. Addison-Wesley. p. 169. ISBN   9780321638007.{{cite book}}: |work= ignored (help)
  4. Seacord, Robert C. (2014-04-25). "Characters and Strings (STR)". STR30-C. Do not attempt to modify string literals (2 ed.). Addison-Wesley. p. 203. ISBN   9780133805291.{{cite book}}: |work= ignored (help)
  5. 1 2 Viega, John; Messier, Matt (2003). "Access Control". Temporary files on Unix. O'Reilly Media. p. 66. ISBN   9780596003944.{{cite book}}: |work= ignored (help)
  6. 1 2 Chen, Hao; Dean, Drew; Wagner, David A. (2004). "Model Checking One Million Lines of C Code" (PDF). Network and Distributed System Security Symposium . 4. Internet Society. Archived (PDF) from the original on 2015-10-08. Retrieved 2019-05-18.
  7. 1 2 Drepper, Ulrich (2009-04-08). "Defensive Programming for Red Hat Enterprise Linux (and What To Do If Something Goes Wrong)" (PDF). p. 7. S2CID   239879. Archived from the original (PDF) on 2019-03-05. Retrieved 2019-05-18.