@@ -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
@@ -273,30 +266,35 @@ static unsigned __stdcall async_notify(LPVOID param)
273266{
274267 char exec [LARGEBUF ];
275268 char notice [LARGEBUF ];
269+ char * argv [3 ];
270+ int ret ;
276271
277272 /* the following code is a copy of the content of the NOT WIN32 part of
278- "notify" function below */
273+ * "notify" function below */
279274
280275 async_notify_t * data = (async_notify_t * )param ;
281276
282277 if (flag_isset (data -> flags , NOTIFY_WALL )) {
283- snprintf (notice ,LARGEBUF , "%s: %s" , data -> date , data -> notice );
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 }
@@ -319,7 +317,7 @@ static void notify(const char *notice, unsigned int flags, const char *ntype,
319317#endif /* !WIN32 */
320318
321319 upsdebugx (6 , "%s: sending notification for [%s]: type %s with flags 0x%04x: %s" ,
322- __func__ , upsname ? upsname : "upsmon itself" , ntype , flags , notice );
320+ __func__ , upsname ? upsname : "upsmon itself" , ntype , flags , NUT_STRARG );
323321
324322 if (flag_isset (flags , NOTIFY_IGNORE )) {
325323 upsdebugx (6 , "%s: NOTIFY_IGNORE" , __func__ );
@@ -328,7 +326,7 @@ static void notify(const char *notice, unsigned int flags, const char *ntype,
328326
329327 if (flag_isset (flags , NOTIFY_SYSLOG )) {
330328 upsdebugx (6 , "%s: NOTIFY_SYSLOG (as LOG_NOTICE)" , __func__ );
331- upslogx (LOG_NOTICE , "%s" , notice );
329+ upslogx (LOG_NOTICE , "%s" , NUT_STRARG ( notice ) );
332330 }
333331
334332#ifndef WIN32
@@ -357,20 +355,24 @@ static void notify(const char *notice, unsigned int flags, const char *ntype,
357355
358356 if (flag_isset (flags , NOTIFY_EXEC )) {
359357 if (notifycmd != NULL ) {
360- upsdebugx (6 , "%s (%schild): NOTIFY_EXEC: calling NOTIFYCMD as '%s \"%s\"'" ,
361- __func__ , use_pipe ? "grand" : "" , notifycmd , notice );
358+ char * argv [3 ];
362359
363- snprintf (exec , sizeof (exec ), "%s \"%s\"" , notifycmd , notice );
360+ upsdebugx (6 , "%s (%schild): NOTIFY_EXEC: calling NOTIFYCMD as '%s' with notice: '%s'" ,
361+ __func__ , use_pipe ? "grand" : "" , notifycmd , NUT_STRARG (notice ));
364362
365363 if (upsname )
366364 setenv ("UPSNAME" , upsname , 1 );
367365 else
368366 setenv ("UPSNAME" , "" , 1 );
369367
370368 setenv ("NOTIFYTYPE" , ntype , 1 );
371- if (system (exec ) == -1 ) {
372- upslog_with_errno (LOG_ERR , "%s" , __func__ );
373- }
369+ argv [0 ] = (char * )notifycmd ;
370+ argv [1 ] = (char * )notice ;
371+ argv [2 ] = NULL ;
372+ execvp (notifycmd , argv );
373+ /* execvp() only returns on error */
374+ upslog_with_errno (LOG_ERR , "%s: execvp(%s) failed" , __func__ , notifycmd );
375+ exit (EXIT_FAILURE );
374376 } else {
375377 upsdebugx (6 , "%s (%schild): NOTIFY_EXEC: no NOTIFYCMD was configured" , __func__ , use_pipe ? "grand" : "" );
376378 }
0 commit comments