mrmekon / circadian

Suspend-On-Idle Daemon for GNU/Linux Power Management
GNU General Public License v3.0
254 stars 16 forks source link

Use locale `LC_ALL=C` #21

Closed bbx0 closed 1 year ago

bbx0 commented 1 year ago

I noticed when a non-default locale is used the FROM header name in w changes to the local language. Also the time format is different, which trips the w time parsing (there is an additional space before m).

As for root the locale is usually not changed this is likely not a problem. Still, I would provide C as the default to avoid unexpected changes in command outputs.

$ circadian --test |grep -e ^x -e ^w
w*              : 242
xssstate*       : w command arguments invalid
xprintidle*     : w command arguments invalid

$ LC_ALL=C circadian --test |grep -e ^x -e ^w
w*              : 14520
xssstate*       : 4294967
xprintidle*     : 4294967

$ w -usf
 14:16:04 up  4:14,  1 user,  load average: 1,07, 1,20, 0,95
USER     TTY      VON               IDLE WHAT
bbx0      tty2     tty2              4:14 m /usr/lib/gdm-wayland-session /usr/bin/gnome-session

$ LC_ALL=C w -usf
 14:16:11 up  4:14,  1 user,  load average: 0.98, 1.17, 0.94
USER     TTY      FROM              IDLE WHAT
bbx0      tty2     tty2              4:14m /usr/lib/gdm-wayland-session /usr/bin/gnome-session
mrmekon commented 1 year ago

Oh, boy! I forgot localization exists. Great catch, I'll merge and issue an immediate new patch release.

I find it very intriguing that the space between the number and unit is locale-dependent...

In addition invalid idle extraction, this also breaks the new code that searches for a literal "FROM" header, since the header names are localized as well.

All of these w troubles could be replaced by directly parsing /var/run/utmp instead, but that has platform-specific struct sizing, so it's not trivial to get it right either.

bbx0 commented 1 year ago

Thanks for picking this up so fast!

The spacing thing also got me off guard, I was just puzzled why those numbers would not line up.

It's almost like w works against being. :) There is utmp-rs but not sure about its usefulness here.