xref: /haiku/src/add-ons/kernel/file_systems/userlandfs/server/main.cpp (revision adb0d19d561947362090081e81d90dde59142026)
1 // main.cpp
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 #include <new>
8 
9 #include "Debug.h"
10 #include "ServerDefs.h"
11 #include "UserlandFSServer.h"
12 
13 // server signature
14 static const char* kServerSignature
15 	= "application/x-vnd.haiku.userlandfs-server";
16 
17 // usage
18 static const char* kUsage =
19 "Usage: %s <options> <file system> [ <port> ]\n"
20 "\n"
21 "Runs the userlandfs server for a given file system. Typically this is done\n"
22 "automatically by the kernel module when a volume is requested to be mounted,\n"
23 "but running the server manually can be useful for debugging purposes. The\n"
24 "<file system> argument specifies the name of the file system to be loaded.\n"
25 "<port> should not be given when starting the server manually; it is used by\n"
26 "the kernel module only.\n"
27 "\n"
28 "Options:\n"
29 "  --debug     - the file system server enters the debugger after the\n"
30 "                userland file system add-on has been loaded and is\n"
31 "                ready to be used.\n"
32 "  -h, --help  - print this text\n"
33 ;
34 
35 static int kArgC;
36 static char** kArgV;
37 
38 // print_usage
39 void
40 print_usage(bool toStdErr = true)
41 {
42 	fprintf((toStdErr ? stderr : stdout), kUsage, kArgV[0], kArgV[0]);
43 }
44 
45 // main
46 int
47 main(int argc, char** argv)
48 {
49 	kArgC = argc;
50 	kArgV = argv;
51 
52 	// init debugging
53 	init_debugging();
54 	struct DebuggingExiter {
55 		DebuggingExiter()	{}
56 		~DebuggingExiter()	{ exit_debugging(); }
57 	} _;
58 
59 	// parse arguments
60 	int argi = 1;
61 	for (; argi < argc; argi++) {
62 		const char* arg = argv[argi];
63 		int32 argLen = strlen(arg);
64 		if (argLen == 0) {
65 			print_usage();
66 			return 1;
67 		}
68 		if (arg[0] != '-')
69 			break;
70 		if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
71 			print_usage(false);
72 			return 0;
73 		} else if (strcmp(arg, "--debug") == 0) {
74 			gServerSettings.SetEnterDebugger(true);
75 		}
76 	}
77 
78 	// get file system, if any
79 	bool dispatcher = true;
80 	const char* fileSystem = NULL;
81 	if (argi < argc) {
82 		fileSystem = argv[argi++];
83 		dispatcher = false;
84 	}
85 
86 	// get the port, if any
87 	int32 port = -1;
88 	if (argi < argc) {
89 		port = atol(argv[argi++]);
90 		if (port <= 0) {
91 			print_usage();
92 			return 1;
93 		}
94 	}
95 
96 	if (argi < argc) {
97 		print_usage();
98 		return 1;
99 	}
100 
101 	// create and init the application
102 	status_t error = B_OK;
103 	UserlandFSServer* server
104 		= new(std::nothrow) UserlandFSServer(kServerSignature);
105 	if (!server) {
106 		fprintf(stderr, "Failed to create server.\n");
107 		return 1;
108 	}
109 	error = server->Init(fileSystem, port);
110 
111 	// run it, if everything went fine
112 	if (error == B_OK)
113 		server->Run();
114 
115 	delete server;
116 	return 0;
117 }
118