@@ -4248,9 +4248,40 @@ static char *proctag = NULL, *proctag_for_upsdebug = NULL,
42484248static void proctag_cleanup (void )
42494249{
42504250 if (proctag ) {
4251- upsdebugx (2 , "a %s sub-process (%s) is exiting now" ,
4252- NUT_STRARG (getmyprocbasename ()), getproctag ());
4251+ char * pn = xstrdup (getmyprocbasename ());
4252+ char * tn = xstrdup (proctag );
4253+
4254+ if (strlen (EXEEXT ) > 0 ) {
4255+ /* TOTHINK: Generalize provided-if-missing strcasestr()? */
4256+ char * s ;
4257+ if (pn ) {
4258+ s = strstr (pn , EXEEXT );
4259+ if (s ) * s = '\0' ;
4260+ }
4261+
4262+ if (tn ) {
4263+ s = strstr (tn , EXEEXT );
4264+ if (s ) * s = '\0' ;
4265+ }
4266+ }
4267+
4268+ if (pn && tn && !strcmp (pn , tn )) {
4269+ /* Avoid reporting this line as misleading "sub-process"
4270+ * after a plain singular setproctag(progname) call in
4271+ * a NUT program: */
4272+ upsdebugx (2 , "a process (%s) is exiting now" , pn );
4273+ } else {
4274+ /* Some ptr not set, or strings not equal */
4275+ upsdebugx (2 , "a %s sub-process (%s) is exiting now" ,
4276+ NUT_STRARG (pn ), proctag );
4277+ }
4278+
4279+ if (pn )
4280+ free (pn );
4281+ if (tn )
4282+ free (tn );
42534283 }
4284+
42544285 setproctag (NULL );
42554286}
42564287
@@ -4292,7 +4323,50 @@ void setproctag(const char *tag)
42924323 proctag_for_upsdebug_buflen = strlen (tag ) + 2 ;
42934324 proctag_for_upsdebug = (char * )xcalloc (proctag_for_upsdebug_buflen , sizeof (char ));
42944325 if (proctag_for_upsdebug ) {
4295- snprintf (proctag_for_upsdebug , proctag_for_upsdebug_buflen , ":%s" , tag );
4326+ char * pn = xstrdup (getmyprocbasename ());
4327+ char * tn = xstrdup (tag );
4328+ int tagged = 0 ;
4329+
4330+ if (strlen (EXEEXT ) > 0 ) {
4331+ /* TOTHINK: Generalize provided-if-missing strcasestr()?
4332+ * One implementation is currently tucked away in
4333+ * libusb0.c because net-snmp may provide another...
4334+ */
4335+ char * s ;
4336+ if (pn ) {
4337+ s = strstr (pn , EXEEXT );
4338+ if (s ) * s = '\0' ;
4339+ }
4340+
4341+ if (tn ) {
4342+ s = strstr (tn , EXEEXT );
4343+ if (s ) * s = '\0' ;
4344+ }
4345+ }
4346+
4347+ if (pn && tn && getenv ("NUT_DEBUG_PROCNAME" ) != NULL && strcmp (pn , tn )) {
4348+ /* Only add the process name if asked for and substantially
4349+ * different from tag value -- e.g. do not duplicate text
4350+ * when callers initialize with settagname(progname) */
4351+ char * s = NULL ;
4352+ proctag_for_upsdebug_buflen += strlen (pn ) + 1 ;
4353+ s = (char * )xcalloc (proctag_for_upsdebug_buflen , sizeof (char ));
4354+ if (s ) {
4355+ snprintf (s , proctag_for_upsdebug_buflen , ":%s:%s" , pn , tag );
4356+ free (proctag_for_upsdebug );
4357+ proctag_for_upsdebug = s ;
4358+ tagged = 1 ;
4359+ }
4360+ }
4361+
4362+ if (!tagged ) {
4363+ snprintf (proctag_for_upsdebug , proctag_for_upsdebug_buflen , ":%s" , tag );
4364+ }
4365+
4366+ if (pn )
4367+ free (pn );
4368+ if (tn )
4369+ free (tn );
42964370 }
42974371}
42984372
0 commit comments