]> icculus.org git repositories - taylor/freespace2.git/blob - src/freespace2/main.cpp
069f7349f27958cba85461a934805c261dbd9779
[taylor/freespace2.git] / src / freespace2 / main.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 #include "pstypes.h"
10
11 #ifdef PLAT_UNIX
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <signal.h>
16 #include <fcntl.h>
17 #endif
18
19
20 extern "C" int game_main(const char *szCmdLine);
21
22
23 #ifdef PLAT_UNIX
24 static void daemonize()
25 {
26         pid_t pid = fork();
27
28         if (pid == -1) {
29                 exit(EXIT_FAILURE);
30         } else if (pid != 0) {
31                 _exit(0);
32         }
33
34         if (setsid() == -1) {
35                 exit(EXIT_FAILURE);
36         }
37
38         signal(SIGHUP, SIG_IGN);
39
40         pid = fork();
41
42         if (pid == -1) {
43                 exit(EXIT_FAILURE);
44         } else if (pid != 0) {
45                 _exit(0);
46         }
47
48         if (chdir("/") == -1) {
49                 exit(EXIT_FAILURE);
50         }
51
52         umask(0);
53
54         close(STDIN_FILENO);
55         close(STDOUT_FILENO);
56         close(STDERR_FILENO);
57
58         open("/dev/null", O_RDONLY);
59         open("/dev/null", O_WRONLY);
60         open("/dev/null", O_RDWR);
61 }
62 #endif
63
64
65 extern "C"
66 int main(int argc, char *argv[])
67 {
68         char *argptr = NULL;
69         int i;
70         int len = 0;
71         int retr = 0;
72
73 #ifdef PLAT_UNIX
74         // if we are standalone headless, daemonize
75         bool daemon = false;
76         bool standalone = false;
77
78         for (i = 1; i < argc; i++) {
79                 if ( !daemon && SDL_strstr(argv[i], "-daemon") ) {
80                         daemon = true;
81                 }
82
83                 if ( !standalone ) {
84                         if ( SDL_strstr(argv[i], "-standalone") ) {
85                                 standalone = true;
86                         }
87
88                         if ( !SDL_strcmp(argv[i], "-b") ) {
89                                 standalone = true;
90                         }
91                 }
92         }
93
94         if (standalone && daemon) {
95                 daemonize();
96         }
97
98         // make sure we create files with user access only
99         umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
100 #endif
101
102         for (i = 1; i < argc; i++) {
103                 len += strlen(argv[i]) + 1;
104         }
105
106         if (len > 0) {
107                 argptr = (char *)SDL_malloc(len+5);
108
109                 if (argptr == NULL) {
110                         SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", "Ran out of memory in main()!", NULL);
111                         exit(1);
112                 }
113
114                 memset(argptr, 0, len+5);
115
116                 for (i = 1; i < argc; i++) {
117                         SDL_strlcat(argptr, argv[i], len+5);
118                         SDL_strlcat(argptr, " ", len+5);
119                 }
120         }
121
122         try {
123                 retr = game_main(argptr);
124         } catch(...) {
125                 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error!", "Exception caught in main()!", NULL);
126         }
127
128         if (argptr) {
129                 SDL_free(argptr);
130         }
131
132         return retr;    
133 }