@@ -33,8 +33,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
33
33
#include < sys/sysctl.h>
34
34
#elif defined(_WIN32)
35
35
#include < algorithm>
36
+ #elif defined(__LINUX)
37
+ #include < sys/types.h>
36
38
#endif
37
39
#if !defined(_WIN32)
40
+ #include < sys/stat.h>
38
41
#include < unistd.h>
39
42
#include < sys/utsname.h>
40
43
#endif
@@ -137,7 +140,8 @@ void signal_handler_init(void)
137
140
/*
138
141
Multithreading support
139
142
*/
140
- int getNumberOfProcessors () {
143
+ int getNumberOfProcessors ()
144
+ {
141
145
#if defined(_SC_NPROCESSORS_ONLN)
142
146
143
147
return sysconf (_SC_NPROCESSORS_ONLN);
@@ -170,7 +174,8 @@ int getNumberOfProcessors() {
170
174
}
171
175
172
176
173
- bool threadBindToProcessor (threadid_t tid, int pnumber) {
177
+ bool threadBindToProcessor (threadid_t tid, int pnumber)
178
+ {
174
179
#if defined(_WIN32)
175
180
176
181
HANDLE hThread = OpenThread (THREAD_ALL_ACCESS, 0 , tid);
@@ -224,7 +229,8 @@ bool threadBindToProcessor(threadid_t tid, int pnumber) {
224
229
}
225
230
226
231
227
- bool threadSetPriority (threadid_t tid, int prio) {
232
+ bool threadSetPriority (threadid_t tid, int prio)
233
+ {
228
234
#if defined(_WIN32)
229
235
230
236
HANDLE hThread = OpenThread (THREAD_ALL_ACCESS, 0 , tid);
@@ -533,17 +539,20 @@ void initializePaths()
533
539
534
540
static irr::IrrlichtDevice* device;
535
541
536
- void initIrrlicht (irr::IrrlichtDevice * _device) {
542
+ void initIrrlicht (irr::IrrlichtDevice * _device)
543
+ {
537
544
device = _device;
538
545
}
539
546
540
547
#ifndef SERVER
541
- v2u32 getWindowSize () {
548
+ v2u32 getWindowSize ()
549
+ {
542
550
return device->getVideoDriver ()->getScreenSize ();
543
551
}
544
552
545
553
546
- float getDisplayDensity () {
554
+ float getDisplayDensity ()
555
+ {
547
556
float gui_scaling = g_settings->getFloat (" gui_scaling" );
548
557
// using Y here feels like a bug, this needs to be discussed later!
549
558
if (getWindowSize ().Y <= 800 ) {
@@ -556,7 +565,8 @@ float getDisplayDensity() {
556
565
return (4.0 /3.0 ) * gui_scaling;
557
566
}
558
567
559
- v2u32 getDisplaySize () {
568
+ v2u32 getDisplaySize ()
569
+ {
560
570
IrrlichtDevice *nulldevice = createDevice (video::EDT_NULL);
561
571
562
572
core::dimension2d<u32> deskres = nulldevice->getVideoModeList ()->getDesktopResolution ();
@@ -566,5 +576,93 @@ v2u32 getDisplaySize() {
566
576
}
567
577
#endif
568
578
579
+ #ifdef SERVER
580
+ #ifdef _WIN32
581
+ void daemonize ()
582
+ {
583
+ errorstream << " daemonize not implemented on windows" << std::endl;
584
+ }
585
+ #else // assume posix like os
586
+
587
+ static std::string get_pidfile_path ()
588
+ {
589
+ // make it static to make sure it won't change after first call to this fct
590
+ static std::string path_pidfile = " " ;
591
+ static bool initialized = false ;
592
+
593
+ if (initialized)
594
+ {
595
+ return path_pidfile;
596
+ }
597
+
598
+ g_settings->getNoEx (" pidfile" , path_pidfile);
599
+
600
+ if (path_pidfile == " " ) {
601
+ #ifdef RUN_IN_PLACE
602
+ path_pidfile = " pidfile.pid" ;
603
+ #else
604
+ path_pidfile = " /var/run/minetest.pid" ;
605
+ #endif
606
+ }
607
+ initialized = true ;
608
+ return path_pidfile;
609
+ }
610
+
611
+
612
+ void daemonize ()
613
+ {
614
+ std::string path_pidfile = get_pidfile_path ();
615
+
616
+ FILE* pidfile = fopen (path_pidfile.c_str ()," r" );
617
+
618
+ if (pidfile) {
619
+ int pid = 0 ;
620
+ if (fscanf (pidfile, " %i" , &pid) == 1 ) {
621
+ if (kill (pid, 0 ) == 0 ) {
622
+ errorstream <<
623
+ " Minetestserver is already running with pid: "
624
+ << pid << std::endl;
625
+ exit (-1 );
626
+ }
627
+ } else {
628
+ errorstream << " Pidfile \" " << path_pidfile << " \" "
629
+ " already exists but content is invalid" << std::endl <<
630
+ " Delete it manually if you're sure minetest isn't running!"
631
+ << std::endl;
632
+ exit (-1 );
633
+ }
634
+ fclose (pidfile);
635
+ pidfile = 0 ;
636
+ }
637
+
638
+ pid_t pid = fork ();
639
+
640
+ if (pid > 0 ) {
641
+ pidfile = fopen (path_pidfile.c_str ()," w+" );
642
+ if (pidfile) {
643
+ fprintf (pidfile," %i" ,pid);
644
+ fclose (pidfile);
645
+ } else {
646
+ errorstream << " Failed to create pidfile: \" " << path_pidfile
647
+ << " \" " << std::endl;
648
+ }
649
+ exit (0 );
650
+ } else if (pid == 0 ) {
651
+ fclose (stdout);
652
+ fclose (stderr);
653
+ return ;
654
+ }
655
+
656
+ errorstream << " Failed to daemonize minetest, exiting" << std::endl;
657
+ exit (-1 );
658
+ }
659
+
660
+ void cleanup_pid ()
661
+ {
662
+ unlink (get_pidfile_path ().c_str ());
663
+ }
664
+ #endif
665
+ #endif
666
+
569
667
} // namespace porting
570
668
0 commit comments