News:

The Forum Rules and Guidelines
Our forum has Rules and Guidelines. Please, be kind and read them ;).

Daemon + syslog support for server mode

Started by Ashley, November 27, 2012, 04:26:46 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Ashley

Another thing I'm considering implementing.

I'd like to add support for logging through syslog. I currently use syslog on my server for Simutrans logging (which allows me to direct the logging to a useful place and do some clever things with the chat logs). I'd like to avoid having to pipe the program output through the logger utility by providing a syslog facility directly.

This shouldn't be too hard to do by redirecting stdout/stderr through syslog functions and providing overrides for the existing logging macros/functions. I'll probably make this a compile-time option.

Following on from this (and somewhat dependent on it) is to add support for running Simutrans as a daemon. This isn't too hard to do (I will be writing the code only for Linux however). It'll be a compile-time option to enable support, coupled with a runtime command line flag to indicate to the server that it should *not* daemonise itself. (e.g. the default if compiled with daemon support will be to run as a daemon). There will also be a configurable location where the daemon will write its PID to. (Support for operating from a chroot jail would also be possible but I'll see)

Again, following on from this, would be to add support to the game to deal with signals sent to the server process. E.g. SIGKILL, SIGHUP etc. The majority of administrative functions would occur through simctrl, however some basic interaction via signals is needed for it to operate as a true daemon.

The overall aim is to make the server a little bit more server-like. :)

Comments appreciated.


Edit: There's a lot of printf() in the code. Shall I consider replacing this with logging statements to be part of the syslog mission?

Edit 2: Actually not as much printf() as I thought, mostly in simmain.cc and simsys_X.cc - I'll produce an initial patch to replace these with logging functions and work from there.

Edit 3: I'm going to reorganise simmain.cc a bit. I'll do all the parsing of command line arguments up-front, then reading the settings and so on, followed by reading object files etc. That way I can easily pick the correct place for it to daemonise (if desired). This should probably happen after argument parsing/config reading but before doing things like loading objects or maps...
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Ashley

Here's a patch, gets the basics of syslog logging working, though still some issues to iron out.

I'm thinking if we want to keep the behaviour of having some non-error but important output (e.g. the things which went to printf() in simmain etc.) then we can either have a #define that either uses printf, or syslog depending on whether SYSLOG is defined, or add another method to the log_t interface to print an "important" message.

I'm inclined toward the latter.

(I could make log_t a virtual interface and have slog_t inherit from it rather than using the #define, but I think that this is a reasonable solution given that syslog support is going to be a compile-time option...
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Markohs


I think this is a good idea.

Quote from: Timothy on November 28, 2012, 12:47:06 AM
or add another method to the log_t interface to print an "important" message.

I'd personally prefer this one too.

prissi

printf was used in simgraph16.cc simworld and some other places since the logging is not started resp. is not visible. But maybe this is no longer correct, I still need to test for this. Still central logging, whz not (as long as it also generates still a simu.log as default.)

About the patch: Why not putting this into log_t and using the functions already there? Almost all functions look very similar to their log counterpart. And with a 256 char limit using sprintf crashes during logging will occur.

Ashley

#4
I can merge it into log_t, it makes it easier to develop it as a separate file. As it currently stands if you define SYSLOG when you compile then you get logging to syslog only. I can modify this so that it becomes an option (though if you're compiling in the option to use SYSLOG, I really don't see why you'd want Simutrans to also log to a file...)

The 256 char sprintf is only for merging the method name with the format, I couldn't think of another way to do this (I could use dynamically allocated memory of course to avoid the limit).

I've implemented the daemon code, and I'm just cleaning up simmain() now since it seems like a good opportunity to do so.


Edit: Attached is current work on this, with daemon support and improved syslog (added a method to log_t and slog_t called important, which logs stuff which would previously have been sent directly to printf() (for the log_t implementation this goes to stdout).
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Ashley

And now with slog_t merged into log_t - actually not too inelegant to do it this way.

When compiling with linux syslog support is enabled, and if linux/posix then daemon support is enabled. These ought to work on other unix-like platforms but would need testing.

Adds command line parameters:

#ifdef SYSLOG                                                                                         
                " -syslog             enable logging to syslog\n"                                     
                "                     mutually exclusive with -log/-logfile\n"                       
                " -tag TAG            sets syslog tag (default 'simutrans')\n"                       
#endif                                                                                               
                         
#ifdef DAEMON                                                                                         
                " -daemon             Daemonise server\n"                                             
                " -pid FILE           Write pid to file specified (only with -daemon)\n"             
                "                     (defaults to \"pid\" in working directory)\n"                   
#endif                                                                                               


(All needs more testing but this is basically done).
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

prissi

I have no idea what the advantage of a daemon would be, as most people even struggle with basic command lines aparently ... And to those a normal program is easier to handle.

On a sidenote: I am pretty sure simutrans is not safe against exploits. Thus a daemon could cause more havoc that a program which is probably run by a very limited user interface.

But as this setting is only used for people compiling their own servers, it should be not a problem.

Ashley

#7
Well, it's useful to me, that's generally why I write patches...

A daemon has no greater privileges or ability to cause havoc than any other process running as the same user, indeed in many ways they are more secure due to their isolation from a controlling terminal. It's purely a convenience for those running network servers :)

I can maintain the code as a patch and just apply it to my servers manually if it's felt there's no value adding it to the main trunk though...


(On the subject of security, daemons can be written such that they chroot into an isolated directory - leaving them unable to write to other sections of the file system. This has a huge security benefit and is one end goal of these changes.)
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

prissi

Ok I confess my knowledge of Unix interna is pretty outdated. No objection to adding it if it gives exactly the same results are before for the normal user. Let me test it this evening.

Ashley

#9
What did you think of the patch? If there's any modifications needed let me know :)


Edit: Attached latest patch with some minor changes.
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Ters

There seems to be some pretty serious todos in that patch.

Ashley

Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Ters

Well, without any experience programming signal handlers, a reminder is pretty much all I could do. With a patch spanning many files, and two/three features, forgetting something is all too easy. Especially if the missing things isn't in your face at runtime.

Ashley

Ah ok, I thought you had spotted something specific :)

I should split the patch up into the syslog and daemon parts really. I need to find the time to set up a system I can actually build the GUI version of Simutrans on since I currently don't have one (except for the somewhat unstable OSX port which has its own problems at present...)

Aside from the modifications to simmain the changes should be low-impact since they don't even get compiled in unless you're compiling for a headless server. Indeed, maintaining this as a stand-alone patch would be easy if simmain were cleaned up along these lines (else finding a good place to put the daemonise call is really tricky).
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Ashley

This patch implements only syslog, with as little modification of the rest of the game as possible.

Currently I'm seeing really strange behaviour whereby running the game without either -log or -syslog (with or without -debug) causes a segfault in the object loading routine. I have no idea why and I have no idea how to go about finding out/fixing it...

Even with the patch applied and SYSLOG compiled out, it still segfaults when -log isn't specified on the command line.

The segfault is documented here: http://forum.simutrans.com/index.php?topic=11023.0

I'm becoming pretty frustrated with this and really not enjoying it so I'm going to stop working on it.
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Dwachs

here is an updated patch: In order to compile I had to
(a) change calls to syslog(...) to ::syslog(...) as there is the member variable log_t::syslog
(b) there was one call to vsyslog(..) without variable-argument list parameter, changed to ::syslog
(c) tee and log were not initialized in log_t::log_t
I also reverted the change to Makefile (-DDAMON), and tried to reduce amount of 'ifdefs

If this works for you, and the crash is gone, we can include this.
Parsley, sage, rosemary, and maggikraut.

Ashley

This looks good, easy enough to compile with the -DSYSLOG flag on the command line. Works ok :)

I can't promise to have the daemon patch done by this weekend, so if there's going to be a release then we can skip it (that one isn't too hard to apply as a patch anyway).

Thanks for looking at it!
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Dwachs

What about the crash you reported? Is this gone with the updated patch?
Parsley, sage, rosemary, and maggikraut.

Ashley

Still seeing the crash with long parameters in simuconf.tab, but the syslog stuff seems to work. I have a feeling this may just be some peculiarity of my build VM...
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.

Dwachs

Parsley, sage, rosemary, and maggikraut.

Ashley

I've compiled the daemon parts of this patch into my current network servers, going to run with it for a while and see if there are any issues, if not should be ok to incorporate too.
Use Firefox? Interested in IPv6? Try SixOrNot the IPv6 status indicator for Firefox.
Why not try playing Simutrans online? See the Game Servers board for details.