2 lib - common code for testing lib/utilinux:my_system() function
5 The Free Software Foundation, Inc.
8 Slava Zanko <slavazanko@gmail.com>, 2013
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "lib/global.h"
32 /* --------------------------------------------------------------------------------------------- */
35 static sigset_t
*sigemptyset_set__captured
;
36 /* @ThenReturnValue */
37 static int sigemptyset__return_value
= 0;
41 sigemptyset (sigset_t
* set
)
43 sigemptyset_set__captured
= set
;
44 return sigemptyset__return_value
;
47 /* --------------------------------------------------------------------------------------------- */
50 static GPtrArray
*sigaction_signum__captured
= NULL
;
52 static GPtrArray
*sigaction_act__captured
= NULL
;
54 static GPtrArray
*sigaction_oldact__captured
= NULL
;
55 /* @ThenReturnValue */
56 static int sigaction__return_value
= 0;
60 sigaction (int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
63 struct sigaction
*tmp_act
;
66 tmp_signum
= g_new (int, 1);
67 memcpy (tmp_signum
, &signum
, sizeof (int));
68 if (sigaction_signum__captured
!= NULL
)
69 g_ptr_array_add (sigaction_signum__captured
, tmp_signum
);
74 tmp_act
= g_new (struct sigaction
, 1);
75 memcpy (tmp_act
, act
, sizeof (struct sigaction
));
79 if (sigaction_act__captured
!= NULL
)
80 g_ptr_array_add (sigaction_act__captured
, tmp_act
);
85 tmp_act
= g_new (struct sigaction
, 1);
86 memcpy (tmp_act
, oldact
, sizeof (struct sigaction
));
90 if (sigaction_oldact__captured
!= NULL
)
91 g_ptr_array_add (sigaction_oldact__captured
, tmp_act
);
93 return sigaction__return_value
;
97 sigaction__init (void)
99 sigaction_signum__captured
= g_ptr_array_new ();
100 sigaction_act__captured
= g_ptr_array_new ();
101 sigaction_oldact__captured
= g_ptr_array_new ();
105 sigaction__deinit (void)
107 g_ptr_array_foreach (sigaction_signum__captured
, (GFunc
) g_free
, NULL
);
108 g_ptr_array_free (sigaction_signum__captured
, TRUE
);
110 g_ptr_array_foreach (sigaction_act__captured
, (GFunc
) g_free
, NULL
);
111 g_ptr_array_free (sigaction_act__captured
, TRUE
);
113 g_ptr_array_foreach (sigaction_oldact__captured
, (GFunc
) g_free
, NULL
);
114 g_ptr_array_free (sigaction_oldact__captured
, TRUE
);
117 /* --------------------------------------------------------------------------------------------- */
120 static GPtrArray
*signal_signum__captured
;
122 static GPtrArray
*signal_handler__captured
;
123 /* @ThenReturnValue */
124 static sighandler_t signal__return_value
= NULL
;
128 signal (int signum
, sighandler_t handler
)
131 sighandler_t
*tmp_handler
;
134 tmp_signum
= g_new (int, 1);
135 memcpy (tmp_signum
, &signum
, sizeof (int));
136 g_ptr_array_add (signal_signum__captured
, tmp_signum
);
139 if (handler
!= SIG_DFL
)
141 tmp_handler
= g_new (sighandler_t
, 1);
142 memcpy (tmp_handler
, handler
, sizeof (sighandler_t
));
145 tmp_handler
= (void *) SIG_DFL
;
146 g_ptr_array_add (signal_handler__captured
, tmp_handler
);
148 return signal__return_value
;
154 signal_signum__captured
= g_ptr_array_new ();
155 signal_handler__captured
= g_ptr_array_new ();
159 signal__deinit (void)
161 g_ptr_array_foreach (signal_signum__captured
, (GFunc
) g_free
, NULL
);
162 g_ptr_array_free (signal_signum__captured
, TRUE
);
164 g_ptr_array_foreach (signal_handler__captured
, (GFunc
) g_free
, NULL
);
165 g_ptr_array_free (signal_handler__captured
, TRUE
);
168 /* --------------------------------------------------------------------------------------------- */
170 /* @ThenReturnValue */
171 static pid_t fork__return_value
;
177 return fork__return_value
;
180 /* --------------------------------------------------------------------------------------------- */
182 static int my_exit__status__captured
;
188 my_exit__status__captured
= status
;
191 /* --------------------------------------------------------------------------------------------- */
194 static char *execvp__file__captured
= NULL
;
196 static GPtrArray
*execvp__args__captured
;
197 /* @ThenReturnValue */
198 static int execvp__return_value
= 0;
202 execvp (const char *file
, char *const argv
[])
205 execvp__file__captured
= g_strdup (file
);
207 for (one_arg
= (char **) argv
; *one_arg
!= NULL
; one_arg
++)
208 g_ptr_array_add (execvp__args__captured
, g_strdup (*one_arg
));
210 return execvp__return_value
;
216 execvp__args__captured
= g_ptr_array_new ();
220 execvp__deinit (void)
222 g_ptr_array_foreach (execvp__args__captured
, (GFunc
) g_free
, NULL
);
223 g_ptr_array_free (execvp__args__captured
, TRUE
);
224 g_free (execvp__file__captured
);
227 /* --------------------------------------------------------------------------------------------- */
229 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
230 struct sigaction *_act = (struct sigaction *) _pntr; \
231 fail_unless (_act->sa_handler == SIG_IGN, " sa_handler should be equals to SIG_IGN"); \
232 ck_assert_int_eq (_act->sa_flags, 0); \
235 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
236 struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
237 struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
238 fail_unless (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
239 "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
243 #define VERIFY_SIGACTION_CALLS() { \
244 ck_assert_int_eq (sigaction_signum__captured->len, 6); \
246 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
247 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
248 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
249 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
250 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
251 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
253 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
254 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
256 struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
257 fail_unless (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
258 "The 'act' in third call to sigaction() should be equals to startup_handler"); \
261 VERIFY_SIGACTION__IS_RESTORED (0, 3); \
262 VERIFY_SIGACTION__IS_RESTORED (1, 4); \
263 VERIFY_SIGACTION__IS_RESTORED (2, 5); \
265 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
266 "oldact in fourth call to sigaction() should be NULL"); \
267 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
268 "oldact in fifth call to sigaction() should be NULL"); \
269 fail_unless (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
270 "oldact in sixth call to sigaction() should be NULL"); \
273 /* --------------------------------------------------------------------------------------------- */
275 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
276 sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
277 fail_unless (tmp_handler == (sighandler_t *) SIG_DFL, \
278 "signal handler should be SIG_DFL"); \
282 #define VERIFY_SIGNAL_CALLS() { \
283 ck_assert_int_eq (signal_signum__captured->len, 4); \
284 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
285 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
286 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
287 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
289 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
290 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
291 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
292 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
295 /* --------------------------------------------------------------------------------------------- */
301 signal__return_value
= NULL
;
308 /* --------------------------------------------------------------------------------------------- */
316 sigaction__deinit ();
319 /* --------------------------------------------------------------------------------------------- */