twitter / twemproxy

A fast, light-weight proxy for memcached and redis
Apache License 2.0
12.15k stars 2.06k forks source link

nutcracker sets umask only upon daemonization #682

Open bugfood opened 1 year ago

bugfood commented 1 year ago

Describe the bug When nutcracker is run with the -d option (to daemonize), nutcracker internally sets its umask to 0. When -d is not specified, nutcracker leaves its umask alone.

To Reproduce Steps to reproduce the behavior:

Set shell umask, e.g.:

umask 0002

Run nutcracker as e.g.:

nutcracker -d -c /etc/nutcracker/nutcracker0.yml -o /var/log/nutcracker/nutcracker0.log -s 2233 -v 5

Observe umask:

$ sudo gdb --pid="$(pidof nutcracker)" --batch -ex 'call/o umask(0)' -ex 'call umask($1)' 2> /dev/null | perl -ne 'print("$1\n")if(/^\$1 = (\d+)$/)'
0

Kill nutcracker and then run without -d, e.g.:

nutcracker -c /etc/nutcracker/nutcracker0.yml -o /var/log/nutcracker/nutcracker0.log -s 2233 -v 5

Observe umask:

$ sudo gdb --pid="$(pidof nutcracker)" --batch -ex 'call/o umask(0)' -ex 'call umask($1)' 2> /dev/null | perl -ne 'print("$1\n")if(/^\$1 = (\d+)$/)'
02

Expected behavior Nutcracker leaving the umask alone is arguably better (so that the local administrator can specify the umask), but either way, I think nutcracker should be internally consistent.

Additional context

This issue causes unexpected behavior when migrating from sysvinit to systemd. Common practice for systemd is to run daemons in the foreground.

A workaround for systemd is to use the UMask option.

https://www.freedesktop.org/software/systemd/man/systemd.exec.html

Credit for the gdb one-liner: https://stackoverflow.com/questions/165212/linux-getting-umask-of-an-already-running-process/38861278#38861278

Thanks, Corey

yuerugou54 commented 1 year ago

马晓栋已经收到您的来件,谢谢!