System (C standard library)

System (C standard library)

In the C standard library, system is a function used to execute subprocesses and commands, residing in stdlib.h. It differs from the exec/spawn family of functions in that instead of passing arguments to an executed object, a single string is passed to the system shell, typically the POSIX shell, /bin/sh -c.

Prototype

int system(const char *command);

Behaviour

system is blocking; that is, the call will wait until the child process terminates and return its exit value. During this time, SIGCHLD will be blocked as system waits for the child to die; also, SIGINT and SIGQUIT are ignored, so to ensure responsiveness the programmer should check the return value to see if the user is trying to terminate the process. On error, system will return -1 on failure prior to or at fork (e.g. process count limit reached) but will return 127 on failure after fork (e.g. unable to execute sh); this is indistinguishable from the command exiting with status 127.

Under POSIX, system forks and execs /bin/sh with two arguments: "-c" and command. While the behaviour of sh is specified elsewhere, it is instructive to consider that command need not be a single command; it can in fact be a pipeline or even a series of pipelines. For example, consider a program that wishes to display a screenshot:

system ("pngtopnm "My Screenshot.png" | pnmtoxwd )> out.xwd && xwud out.xwd");

This line exhibits an import consideration: since command will be parsed as a shell command line, quotes around e.g. file names must be escaped. This however raises security considerations, since if command is constructed from user-supplied data, an attacker may be able to break out of any quoting and execute an arbitrary command in the context of the parent; indeed, this is almost the canonical code injection exploit. It is thus considered sensible to only employ system on predetermined command strings, using other functions (spawn et al) to pass user-supplied data in argv or passing such data through pipes or temporary files.

The child spawned by system inherits its parent's standard streams; thus the child can receive keyboard input and write to the terminal. Note that this means that the parent will not receive the child's output, except through the use of a redirection or tee.

External links

*


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Standard library — A standard library for a programming language is the library that is conventionally made available in every implementation of that language. In some cases, the library is described directly in the programming language specification; in other… …   Wikipedia

  • Apache C++ Standard Library — Infobox Software name = Apache C++ Standard Library caption = developer = Apache Software Foundation latest release version = 4.2.1 latest release date = release date|2008|5|1 latest preview version = latest preview date = operating system =… …   Wikipedia

  • C standard library — The C Standard Library consists of a set of sections of the ANSI C standard in the programming language C. They describe a collection of headers and library routines used to implement common operations such as input/output[1] and string handling …   Wikipedia

  • C Standard Library — Die Standard C Library ist eine genormte Bibliothek für die Programmiersprache C, die etwa 200 Funktionen enthält und in jedem hosted environment von C zu implementieren ist. Sie enthält häufig benötigte Funktionen für Ein und Ausgabe,… …   Deutsch Wikipedia

  • System (disambiguation) — The term system may refer to:* System, a set of entities, real or abstract, comprising a whole * Biological system, a group of organs * Computer system, the combination of hardware and software * Ecosystem, an entity comprising a large number of… …   Wikipedia

  • library — /luy brer ee, breuh ree, bree/, n., pl. libraries. 1. a place set apart to contain books, periodicals, and other material for reading, viewing, listening, study, or reference, as a room, set of rooms, or building where books may be read or… …   Universalium

  • Library (computing) — This article is about the programming concept. For Windows 7 Libraries, see Features new to Windows 7#Libraries. Illustration of an application which uses libvorbisfile to play an Ogg Vorbis file In computer science, a library is a collection of… …   Wikipedia

  • System time — Unix date command In computer science and computer programming, system time represents a computer system s notion of the passing of time. In this sense, time also includes the passing of days on the calendar. System time is measured by a system… …   Wikipedia

  • Library classification — forms part of the field of library and information science. It is a form of bibliographic classification (library classifications are used in library catalogs, while bibliographic classification also covers classification used in other kinds of… …   Wikipedia

  • Library of Congress classification — Library Science. a system for classifying books and other materials, using for its notation both letters and numerals to allow for expansion: originally developed at the Library of Congress for classifying its books and subsequently adopted by… …   Universalium

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”