]> git.ipfire.org Git - thirdparty/bash.git/blame - examples/loadables/setpgid.c
bash-5.1-alpha release
[thirdparty/bash.git] / examples / loadables / setpgid.c
CommitLineData
a0c0a00f
CR
1/* setpgid.c: bash loadable wrapper for setpgid system call
2
3 An example of how to wrap a system call with a loadable builtin.
4
5 Originally contributed by Jason Vas Dias <jason.vas.dias@gmail.com>
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#include <config.h>
22
23#if defined (HAVE_UNISTD_H)
24# include <unistd.h>
25#endif
26#include <errno.h>
27#include <string.h>
28
29#include "bashtypes.h"
30#include "posixtime.h"
31
32#include <stdio.h>
33
34#include "builtins.h"
35#include "shell.h"
36#include "common.h"
37
38#include "bashgetopt.h"
39
40#if !defined (_POSIX_VERSION)
41# define setpgid(pid, pgrp) setpgrp (pid, pgrp)
42#endif
43
44int
45setpgid_builtin (list)
46 WORD_LIST *list;
47{
48 register WORD_LIST *wl;
49 intmax_t pid_arg, pgid_arg;
50 pid_t pid, pgid;
51 char *pidstr, *pgidstr;
52
53 wl = list;
54 pid = pgid = 0;
55
56 if (wl == 0 || wl->next == 0)
57 {
58 builtin_usage ();
59 return (EX_USAGE);
60 }
61
62 pidstr = wl->word ? wl->word->word : 0;
63 pgidstr = wl->next->word ? wl->next->word->word : 0;
64
65 if (pidstr == 0 || pgidstr == 0)
66 {
67 builtin_usage ();
68 return (EX_USAGE);
69 }
70
71 if (legal_number (pidstr, &pid_arg) == 0)
72 {
73 builtin_error ("%s: pid argument must be numeric", pidstr);
74 return (EXECUTION_FAILURE);
75 }
76 if (pid_arg < 0)
77 {
78 builtin_error("%s: negative pid values not allowed", pidstr);
79 return (EXECUTION_FAILURE);
80 }
81 pid = pid_arg;
82
83 if (legal_number (pgidstr, &pgid_arg) == 0)
84 {
85 builtin_error ("%s: pgrp argument must be numeric", pgidstr);
86 return (EXECUTION_FAILURE);
87 }
88 if (pgid_arg < 0)
89 {
90 builtin_error ("%s: negative pgrp values not allowed", pgidstr);
91 return (EXECUTION_FAILURE);
92 }
93 pgid = pgid_arg;
94
95 errno = 0;
96 if (setpgid(pid, pgid) < 0)
97 {
98 builtin_error("setpgid failed: %s", strerror (errno));
99 return (EXECUTION_FAILURE);
100 }
101 return (EXECUTION_SUCCESS);
102}
103
104const char *setpgid_doc[] = {
105 "invoke the setpgid(2) system call",
106 "",
107 "Arguments:",
712f80b0 108 " pid : numeric process identifier, >= 0",
a0c0a00f
CR
109 " pgrpid: numeric process group identifier, >=0",
110 "See the setpgid(2) manual page.",
111 (const char *)NULL
112};
113
114struct builtin setpgid_struct = {
115 "setpgid",
116 setpgid_builtin,
117 BUILTIN_ENABLED,
118 (char **)setpgid_doc,
119 "setpgid pid pgrpid",
120 0
121};