@@ -238,25 +238,18 @@ static void wall(const char *text)
238238 pclose (wf );
239239#else /* WIN32 */
240240# define MESSAGE_CMD "message.exe"
241- char * command ;
242-
243- /* first +1 is for the space between message and text
244- * second +1 is for trailing 0
245- * +2 is for ""
246- */
247- size_t commandsz = strlen (MESSAGE_CMD ) + 1 + 2 + strlen (text ) + 1 ;
241+ char * argv [3 ];
242+ int ret ;
248243
249- command = malloc (commandsz );
250- if (command == NULL ) {
251- upslog_with_errno (LOG_NOTICE , "Not enough memory for wall" );
252- return ;
253- }
244+ argv [0 ] = MESSAGE_CMD ;
245+ argv [1 ] = (char * )text ;
246+ argv [2 ] = NULL ;
254247
255- snprintf (command , commandsz , "%s \"%s\"" , MESSAGE_CMD , text );
256- if (system (command ) != 0 ) {
257- upslog_with_errno (LOG_NOTICE , "Can't invoke wall" );
248+ upsdebugx (6 , "%s: executing %s with message: %s" , __func__ , MESSAGE_CMD , NUT_STRARG (text ));
249+ ret = _spawnvp (_P_WAIT , MESSAGE_CMD , (const char * const * )argv );
250+ if (ret != 0 ) {
251+ upslog_with_errno (LOG_NOTICE , "Can't invoke wall (status: %d)" , ret );
258252 }
259- free (command );
260253#endif /* WIN32 */
261254}
262255
@@ -271,32 +264,37 @@ typedef struct async_notify_s {
271264
272265static unsigned __stdcall async_notify (LPVOID param )
273266{
274- char exec [ LARGEBUF ];
275- char notice [ LARGEBUF ] ;
267+ char * argv [ 3 ];
268+ int ret ;
276269
277270 /* the following code is a copy of the content of the NOT WIN32 part of
278- "notify" function below */
271+ * "notify" function below */
279272
280273 async_notify_t * data = (async_notify_t * )param ;
281274
282275 if (flag_isset (data -> flags , NOTIFY_WALL )) {
283- snprintf (notice ,LARGEBUF ,"%s: %s" , data -> date , data -> notice );
276+ char notice [LARGEBUF ];
277+
278+ snprintf (notice , sizeof (notice ), "%s: %s" , data -> date , data -> notice );
284279 wall (notice );
285280 }
286281
287282 if (flag_isset (data -> flags , NOTIFY_EXEC )) {
288283 if (notifycmd != NULL ) {
289- snprintf (exec , sizeof (exec ), "%s \"%s\"" , notifycmd , data -> notice );
290-
291- upsdebugx (6 , "%s: Calling NOTIFYCMD: %s" , __func__ , exec );
284+ upsdebugx (6 , "%s: Calling NOTIFYCMD: %s with notice: %s" ,
285+ __func__ , notifycmd , NUT_STRARG (data -> notice ));
292286 if (data -> upsname )
293287 setenv ("UPSNAME" , data -> upsname , 1 );
294288 else
295289 setenv ("UPSNAME" , "" , 1 );
296290
297291 setenv ("NOTIFYTYPE" , data -> ntype , 1 );
298- if (system (exec ) == -1 ) {
299- upslog_with_errno (LOG_ERR , "%s" , __func__ );
292+ argv [0 ] = notifycmd ;
293+ argv [1 ] = data -> notice ;
294+ argv [2 ] = NULL ;
295+ ret = _spawnvp (_P_WAIT , notifycmd , (const char * const * )argv );
296+ if (ret == -1 ) {
297+ upslog_with_errno (LOG_ERR , "%s: _spawnvp failed" , __func__ );
300298 }
301299 }
302300 }
@@ -314,12 +312,11 @@ static void notify(const char *notice, unsigned int flags, const char *ntype,
314312 const char * upsname )
315313{
316314#ifndef WIN32
317- char exec [LARGEBUF ];
318315 int ret ;
319316#endif /* !WIN32 */
320317
321318 upsdebugx (6 , "%s: sending notification for [%s]: type %s with flags 0x%04x: %s" ,
322- __func__ , upsname ? upsname : "upsmon itself" , ntype , flags , notice );
319+ __func__ , upsname ? upsname : "upsmon itself" , ntype , flags , NUT_STRARG ( notice ) );
323320
324321 if (flag_isset (flags , NOTIFY_IGNORE )) {
325322 upsdebugx (6 , "%s: NOTIFY_IGNORE" , __func__ );
@@ -328,7 +325,7 @@ static void notify(const char *notice, unsigned int flags, const char *ntype,
328325
329326 if (flag_isset (flags , NOTIFY_SYSLOG )) {
330327 upsdebugx (6 , "%s: NOTIFY_SYSLOG (as LOG_NOTICE)" , __func__ );
331- upslogx (LOG_NOTICE , "%s" , notice );
328+ upslogx (LOG_NOTICE , "%s" , NUT_STRARG ( notice ) );
332329 }
333330
334331#ifndef WIN32
@@ -357,20 +354,24 @@ static void notify(const char *notice, unsigned int flags, const char *ntype,
357354
358355 if (flag_isset (flags , NOTIFY_EXEC )) {
359356 if (notifycmd != NULL ) {
360- upsdebugx (6 , "%s (%schild): NOTIFY_EXEC: calling NOTIFYCMD as '%s \"%s\"'" ,
361- __func__ , use_pipe ? "grand" : "" , notifycmd , notice );
357+ char * argv [3 ];
362358
363- snprintf (exec , sizeof (exec ), "%s \"%s\"" , notifycmd , notice );
359+ upsdebugx (6 , "%s (%schild): NOTIFY_EXEC: calling NOTIFYCMD as '%s' with notice: '%s'" ,
360+ __func__ , use_pipe ? "grand" : "" , notifycmd , NUT_STRARG (notice ));
364361
365362 if (upsname )
366363 setenv ("UPSNAME" , upsname , 1 );
367364 else
368365 setenv ("UPSNAME" , "" , 1 );
369366
370367 setenv ("NOTIFYTYPE" , ntype , 1 );
371- if (system (exec ) == -1 ) {
372- upslog_with_errno (LOG_ERR , "%s" , __func__ );
373- }
368+ argv [0 ] = (char * )notifycmd ;
369+ argv [1 ] = (char * )notice ;
370+ argv [2 ] = NULL ;
371+ execvp (notifycmd , argv );
372+ /* execvp() only returns on error */
373+ upslog_with_errno (LOG_ERR , "%s: execvp(%s) failed" , __func__ , notifycmd );
374+ exit (EXIT_FAILURE );
374375 } else {
375376 upsdebugx (6 , "%s (%schild): NOTIFY_EXEC: no NOTIFYCMD was configured" , __func__ , use_pipe ? "grand" : "" );
376377 }
0 commit comments