File: system.3

package info (click to toggle)
manpages-es 1.24a-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,256 kB
  • ctags: 7
  • sloc: makefile: 66; sh: 62
file content (149 lines) | stat: -rw-r--r-- 4,592 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
.\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one
.\" 
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\" 
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" License.
.\" Modified Sat Jul 24 17:51:15 1993 by Rik Faith (faith@cs.unc.edu)
.\" Modified 11 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
.\" Translated into Spanish Thu Mar 12 15:34:37 CET 1998 by Gerardo
.\" Aburruzaga Garca <gerardo.aburruzaga@uca.es>
.\" Translation revised Wed Aug 19 1998 by Juan Piernas <piernas@ditec.um.es>
.\"
.TH SYSTEM 3  "11 Mayo 1998" "GNU" "Manual del Programador de Linux"
.SH NOMBRE
system \- ejecuta una orden del intrprete de comandos (shell)
.SH SINOPSIS
.nf
.B #include <stdlib.h>
.sp
.BI "int system (const char * " "mandato" ");"
.fi
.SH DESCRIPCIN
.B system()
ejecuta una orden especificada en
.I mandato
llamando a
.BR "/bin/sh -c"
.IR mandato ,
y regresa despus de que la orden se haya terminado de ejecutar.
Durante la ejecucion de la orden, se bloquear
.B SIGCHLD
y no se hace caso de las seales
.B SIGINT
ni
.BR SIGQUIT .
.SH "VALOR DEVUELTO"
El valor devuelto es 127 si la llamada a
.B execve()
para
.B /bin/sh
falla, \-1 si se produce otro error, y el cdigo de salida de la
orden en cualquier otro caso.
.PP
Si el valor de
.I mandato
es 
.BR NULL ,
.B system()
devuelve un nmero distinto de cero si hay un intrprete de comandos
disponible, y cero si no.
.PP
.B system()
no afecta al estado de espera de cualquier otro proceso hijo.
.SH "CONFORME A"
C ANSI, POSIX.2, BSD 4.3
.SH "FALLOS"
.PP
Es extremadamente desafortunado que la versin de
.B system()
en libc no haga caso de las interrupciones. Esto hace imposible
interrumpir los programas que la llaman desde un bucle. 
Lo cual significa que para tales propsitos uno no debera utilizar
.B system()
sino una versin privada, como la siguiente (aviso: cdigo no probado!):
.br
.nf

int mi_system (const char *mandato) {
    int pid, status;

    if (mandato == 0)
        return 1;   /* En UNIX/Linux siempre hay un shell */
    pid = fork();
    if (pid == -1)
        return -1;
    if (pid == 0) {
        char *argv[4];
        argv[0] = "sh";
        argv[1] = "-c";
        argv[2] = mandato;
        argv[3] = 0;
        execve("/bin/sh", argv, environ);
        exit(127);
    }
    do {
        if (waitpid(pid, &status, 0) == -1) {
            if (errno != EINTR)
                return -1;
        } else
            return status;
    } while(1);
}
.fi
.PP
De hecho,
.B system()
no funcionar adecuadamente desde programas con privilegios suid o sgid en
sistemas en los que
.B /bin/sh
sea la versin 2 de
.BR bash ,
ya que bash 2 omite los privilegios en el momento de ejecutarse. (Debian usa
una versin modificada de bash que no hace esto cuando se invoca como
.BR sh ).
No llame a
.B system()
desde un programa con privilegios suid o sgid, porque pudiera ser que
se emplearan valores extraos para algunas variables de entorno
para comprometer la integridad del sistema.
En su lugar emplee la familia de funciones
.BR exec (3),
salvo
.BR execlp (3)
o
.BR execvp (3).
.PP
En realidad no se comprueba si el intrprete de comandos
.B /bin/sh
est disponible o no; en Linux siempre se supone que lo est. ISO C
especifica la comprobacin, pero POSIX.2 especifica que el valor devuelto
siempre ser no cero, ya que un sistema sin intrprete de comandos no es
conforme, y esto es lo que se implementa.
.PP
Es posible que una orden del intrprete de comandos devuelva 127, as que ese
cdigo no es una indicacin segura de que
.B execve()
haya fallado; compruebe el valor de
.I errno
para asegurarse.
.SH "VASE TAMBIN"
.BR sh (1),
.BR signal (2),
.BR exec (3)