testing_api_cmd_exec_wget.c (3921B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 3, 8 or (at your option) any later version. 9 10 TALER is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with TALER; see the file COPYING. If not, see 17 <http://www.gnu.org/licenses/> 18 */ 19 /** 20 * @file testing/testing_api_cmd_exec_wget.c 21 * @brief run a wget command 22 * @author Christian Grothoff 23 */ 24 #include "taler/platform.h" 25 #include "taler/taler_json_lib.h" 26 #include <gnunet/gnunet_curl_lib.h> 27 #include "taler/taler_signatures.h" 28 #include "taler/taler_testing_lib.h" 29 30 31 /** 32 * State for a "wget" CMD. 33 */ 34 struct WgetState 35 { 36 /** 37 * Process for the wgeter. 38 */ 39 struct GNUNET_Process *wget_proc; 40 41 /** 42 * URL to used by the wget. 43 */ 44 const char *url; 45 }; 46 47 48 /** 49 * Run the command; use the `wget` program. 50 * 51 * @param cls closure. 52 * @param cmd command currently being executed. 53 * @param is interpreter state. 54 */ 55 static void 56 wget_run (void *cls, 57 const struct TALER_TESTING_Command *cmd, 58 struct TALER_TESTING_Interpreter *is) 59 { 60 struct WgetState *ws = cls; 61 62 (void) cmd; 63 ws->wget_proc = GNUNET_process_create (GNUNET_OS_INHERIT_STD_ERR); 64 if (GNUNET_OK != 65 GNUNET_process_run_command_va (ws->wget_proc, 66 "wget", 67 "wget", 68 ws->url, 69 NULL)) 70 { 71 GNUNET_break (0); 72 GNUNET_process_destroy (ws->wget_proc); 73 ws->wget_proc = NULL; 74 TALER_TESTING_interpreter_fail (is); 75 return; 76 } 77 TALER_TESTING_wait_for_sigchld (is); 78 } 79 80 81 /** 82 * Free the state of a "wget" CMD, and possibly 83 * kills its process if it did not terminate regularly. 84 * 85 * @param cls closure. 86 * @param cmd the command being freed. 87 */ 88 static void 89 wget_cleanup (void *cls, 90 const struct TALER_TESTING_Command *cmd) 91 { 92 struct WgetState *ws = cls; 93 94 (void) cmd; 95 if (NULL != ws->wget_proc) 96 { 97 GNUNET_break (GNUNET_OK == 98 GNUNET_process_kill (ws->wget_proc, 99 SIGKILL)); 100 GNUNET_process_wait (ws->wget_proc, 101 true, 102 NULL, 103 NULL); 104 GNUNET_process_destroy (ws->wget_proc); 105 ws->wget_proc = NULL; 106 } 107 GNUNET_free (ws); 108 } 109 110 111 /** 112 * Offer "wget" CMD internal data to other commands. 113 * 114 * @param cls closure. 115 * @param[out] ret result. 116 * @param trait name of the trait. 117 * @param index index number of the object to offer. 118 * @return #GNUNET_OK on success. 119 */ 120 static enum GNUNET_GenericReturnValue 121 wget_traits (void *cls, 122 const void **ret, 123 const char *trait, 124 unsigned int index) 125 { 126 struct WgetState *ws = cls; 127 struct TALER_TESTING_Trait traits[] = { 128 TALER_TESTING_make_trait_process (&ws->wget_proc), 129 TALER_TESTING_trait_end () 130 }; 131 132 return TALER_TESTING_get_trait (traits, 133 ret, 134 trait, 135 index); 136 } 137 138 139 struct TALER_TESTING_Command 140 TALER_TESTING_cmd_exec_wget (const char *label, 141 const char *url) 142 { 143 struct WgetState *ws; 144 145 ws = GNUNET_new (struct WgetState); 146 ws->url = url; 147 148 { 149 struct TALER_TESTING_Command cmd = { 150 .cls = ws, 151 .label = label, 152 .run = &wget_run, 153 .cleanup = &wget_cleanup, 154 .traits = &wget_traits 155 }; 156 157 return cmd; 158 } 159 } 160 161 162 /* end of testing_api_cmd_exec_wget.c */