2 lib - common code for testing lib/utilinux:my_system() function
4 Copyright (C) 2013-2024
5 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/vfs/vfs.h"
31 /* --------------------------------------------------------------------------------------------- */
34 static sigset_t
*sigemptyset_set__captured
;
35 /* @ThenReturnValue */
36 static int sigemptyset__return_value
= 0;
44 sigemptyset (sigset_t
*set
)
46 sigemptyset_set__captured
= set
;
47 return sigemptyset__return_value
;
50 /* --------------------------------------------------------------------------------------------- */
53 static GPtrArray
*sigaction_signum__captured
= NULL
;
55 static GPtrArray
*sigaction_act__captured
= NULL
;
57 static GPtrArray
*sigaction_oldact__captured
= NULL
;
58 /* @ThenReturnValue */
59 static int sigaction__return_value
= 0;
63 my_sigaction (int signum
, const struct sigaction
*act
, struct sigaction
*oldact
)
66 struct sigaction
*tmp_act
;
69 tmp_signum
= g_new (int, 1);
70 memcpy (tmp_signum
, &signum
, sizeof (*tmp_signum
));
71 if (sigaction_signum__captured
!= NULL
)
72 g_ptr_array_add (sigaction_signum__captured
, tmp_signum
);
77 tmp_act
= g_new (struct sigaction
, 1);
78 memcpy (tmp_act
, act
, sizeof (*tmp_act
));
82 if (sigaction_act__captured
!= NULL
)
83 g_ptr_array_add (sigaction_act__captured
, tmp_act
);
88 tmp_act
= g_new (struct sigaction
, 1);
89 memcpy (tmp_act
, oldact
, sizeof (*tmp_act
));
93 if (sigaction_oldact__captured
!= NULL
)
94 g_ptr_array_add (sigaction_oldact__captured
, tmp_act
);
96 return sigaction__return_value
;
100 sigaction__init (void)
102 sigaction_signum__captured
= g_ptr_array_new_with_free_func (g_free
);
103 sigaction_act__captured
= g_ptr_array_new_with_free_func (g_free
);
104 sigaction_oldact__captured
= g_ptr_array_new_with_free_func (g_free
);
108 sigaction__deinit (void)
110 g_ptr_array_free (sigaction_signum__captured
, TRUE
);
111 sigaction_signum__captured
= NULL
;
113 g_ptr_array_free (sigaction_act__captured
, TRUE
);
114 sigaction_act__captured
= NULL
;
116 g_ptr_array_free (sigaction_oldact__captured
, TRUE
);
117 sigaction_oldact__captured
= NULL
;
120 /* --------------------------------------------------------------------------------------------- */
123 static GPtrArray
*signal_signum__captured
;
125 static GPtrArray
*signal_handler__captured
;
126 /* @ThenReturnValue */
127 static sighandler_t signal__return_value
= NULL
;
131 my_signal (int signum
, sighandler_t handler
)
134 sighandler_t
*tmp_handler
;
137 tmp_signum
= g_new (int, 1);
138 memcpy (tmp_signum
, &signum
, sizeof (*tmp_signum
));
139 g_ptr_array_add (signal_signum__captured
, tmp_signum
);
142 if (handler
!= SIG_DFL
)
144 tmp_handler
= g_new (sighandler_t
, 1);
145 memcpy (tmp_handler
, handler
, sizeof (*tmp_handler
));
148 tmp_handler
= (void *) SIG_DFL
;
149 g_ptr_array_add (signal_handler__captured
, tmp_handler
);
151 return signal__return_value
;
157 signal_signum__captured
= g_ptr_array_new_with_free_func (g_free
);
158 signal_handler__captured
= g_ptr_array_new_with_free_func (g_free
);
162 signal__deinit (void)
164 g_ptr_array_free (signal_signum__captured
, TRUE
);
165 signal_signum__captured
= NULL
;
167 g_ptr_array_free (signal_handler__captured
, TRUE
);
168 signal_handler__captured
= NULL
;
171 /* --------------------------------------------------------------------------------------------- */
173 /* @ThenReturnValue */
174 static pid_t fork__return_value
;
180 return fork__return_value
;
183 /* --------------------------------------------------------------------------------------------- */
185 static int my_exit__status__captured
;
191 my_exit__status__captured
= status
;
194 /* --------------------------------------------------------------------------------------------- */
197 static char *execvp__file__captured
= NULL
;
199 static GPtrArray
*execvp__args__captured
;
200 /* @ThenReturnValue */
201 static int execvp__return_value
= 0;
205 my_execvp (const char *file
, char *const argv
[])
208 execvp__file__captured
= g_strdup (file
);
210 for (one_arg
= (char **) argv
; *one_arg
!= NULL
; one_arg
++)
211 g_ptr_array_add (execvp__args__captured
, g_strdup (*one_arg
));
213 return execvp__return_value
;
219 execvp__args__captured
= g_ptr_array_new_with_free_func (g_free
);
223 execvp__deinit (void)
225 g_ptr_array_free (execvp__args__captured
, TRUE
);
226 execvp__args__captured
= NULL
;
227 MC_PTR_FREE (execvp__file__captured
);
230 /* --------------------------------------------------------------------------------------------- */
232 #define VERIFY_SIGACTION__ACT_IGNORED(_pntr) { \
233 struct sigaction *_act = (struct sigaction *) _pntr; \
234 mctest_assert_ptr_eq (_act->sa_handler, SIG_IGN); \
235 ck_assert_int_eq (_act->sa_flags, 0); \
238 #define VERIFY_SIGACTION__IS_RESTORED(oldact_idx, act_idx) { \
239 struct sigaction *_oldact = (struct sigaction *) g_ptr_array_index(sigaction_oldact__captured, oldact_idx); \
240 struct sigaction *_act = (struct sigaction *) g_ptr_array_index(sigaction_act__captured, act_idx); \
241 ck_assert_msg (memcmp(_oldact, _act, sizeof(struct sigaction)) == 0, \
242 "sigaction(): oldact[%d] should be equals to act[%d]", oldact_idx, act_idx); \
246 #define VERIFY_SIGACTION_CALLS() { \
247 ck_assert_int_eq (sigaction_signum__captured->len, 6); \
249 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 0)), SIGINT); \
250 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 1)), SIGQUIT); \
251 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 2)), SIGTSTP); \
252 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 3)), SIGINT); \
253 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 4)), SIGQUIT); \
254 ck_assert_int_eq (*((int *) g_ptr_array_index(sigaction_signum__captured, 5)), SIGTSTP); \
256 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 0)); \
257 VERIFY_SIGACTION__ACT_IGNORED(g_ptr_array_index(sigaction_act__captured, 1)); \
259 struct sigaction *_act = g_ptr_array_index(sigaction_act__captured, 2); \
260 ck_assert_msg (memcmp (_act, &startup_handler, sizeof(struct sigaction)) == 0, \
261 "The 'act' in third call to sigaction() should be equals to startup_handler"); \
264 VERIFY_SIGACTION__IS_RESTORED (0, 3); \
265 VERIFY_SIGACTION__IS_RESTORED (1, 4); \
266 VERIFY_SIGACTION__IS_RESTORED (2, 5); \
268 ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 3) == NULL, \
269 "oldact in fourth call to sigaction() should be NULL"); \
270 ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 4) == NULL, \
271 "oldact in fifth call to sigaction() should be NULL"); \
272 ck_assert_msg (g_ptr_array_index(sigaction_oldact__captured, 5) == NULL, \
273 "oldact in sixth call to sigaction() should be NULL"); \
276 /* --------------------------------------------------------------------------------------------- */
278 #define VERIFY_SIGNAL_HANDLER_IS_SIG_DFL(_idx) { \
279 sighandler_t *tmp_handler = (sighandler_t *) g_ptr_array_index(signal_handler__captured, _idx);\
280 mctest_assert_ptr_eq (tmp_handler, (sighandler_t *) SIG_DFL); \
284 #define VERIFY_SIGNAL_CALLS() { \
285 ck_assert_int_eq (signal_signum__captured->len, 4); \
286 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 0)), SIGINT); \
287 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 1)), SIGQUIT); \
288 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 2)), SIGTSTP); \
289 ck_assert_int_eq (*((int *) g_ptr_array_index(signal_signum__captured, 3)), SIGCHLD); \
291 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (0); \
292 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (1); \
293 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (2); \
294 VERIFY_SIGNAL_HANDLER_IS_SIG_DFL (3); \
297 /* --------------------------------------------------------------------------------------------- */
303 signal__return_value
= NULL
;
310 /* --------------------------------------------------------------------------------------------- */
318 sigaction__deinit ();
321 /* --------------------------------------------------------------------------------------------- */