xref: /minix/minix/lib/libpuffs/main.c (revision 0a6a1f1d)
1 
2 #include "fs.h"
3 
4 static message fs_msg;
5 static int fs_ipc_status;
6 static int fs_pending;
7 
8 #define PUFFS_MAX_ARGS 20
9 
10 /*===========================================================================*
11  *		            sef_cb_init_fresh                                *
12  *===========================================================================*/
13 static int sef_cb_init_fresh(int type, sef_init_info_t *info)
14 {
15 /* Initialize the Minix file server. */
16   return(OK);
17 }
18 
19 /*===========================================================================*
20  *		           sef_cb_signal_handler                             *
21  *===========================================================================*/
22 static void sef_cb_signal_handler(int signo)
23 {
24   /* Only check for termination signal, ignore anything else. */
25   if (signo != SIGTERM) return;
26 
27   exitsignaled = 1;
28   if (mounted)
29 	fs_sync();
30 
31   sef_cancel();
32 }
33 
34 /*===========================================================================*
35  *			       sef_local_startup			     *
36  *===========================================================================*/
37 static void sef_local_startup(void)
38 {
39   /* Register init callbacks. */
40   sef_setcb_init_fresh(sef_cb_init_fresh);
41 
42   /* Register signal callbacks. */
43   sef_setcb_signal_handler(sef_cb_signal_handler);
44 
45   /* Let SEF perform startup. */
46   sef_startup();
47 }
48 
49 /*===========================================================================*
50  *				get_work				     *
51  *===========================================================================*/
52 static int get_work(message *msg, int *ipc_status)
53 {
54   int r;
55 
56   for (;;) {
57 	if ((r = sef_receive_status(ANY, msg, ipc_status)) != OK) {
58 		if (r == EINTR) /* sef_cancel from signal handler? */
59 			break; /* see if we can exit the main loop */
60 		panic("sef_receive failed: %d", r);
61 	}
62 	if (msg->m_source == VFS_PROC_NR)
63 		break;
64 	lpuffs_debug("libpuffs: unexpected source %d\n", msg->m_source);
65   }
66 
67   return r;
68 }
69 
70 int __wrap_main(int argc, char *argv[]);
71 int __real_main(int argc, char* argv[]);
72 
73 int __wrap_main(int argc, char *argv[])
74 {
75   int i;
76   int new_argc = 0;
77   static char* new_argv[PUFFS_MAX_ARGS];
78   char *name;
79 
80   /* SEF local startup. */
81   env_setargs(argc, argv);
82   sef_local_startup();
83 
84   global_kcred.pkcr_type = PUFFCRED_TYPE_INTERNAL;
85 
86   if (argc < 3) {
87 	panic("Unexpected arguments, use:\
88 		mount -t fs /dev/ /dir [-o option1,option2]\n");
89   }
90 
91   name = argv[0] + strlen(argv[0]);
92   while (*name != '/' && name != argv[0])
93 	  name--;
94   if (name != argv[0])
95 	  name++;
96   strcpy(fs_name, name);
97 
98   new_argv[new_argc] = argv[0];
99   new_argc++;
100 
101   for (i = 1; i < argc; i++) {
102 	if (new_argc >= PUFFS_MAX_ARGS) {
103 		panic("Too many arguments, change PUFFS_MAX_ARGS");
104 	}
105 	new_argv[new_argc] = argv[i];
106 	new_argc++;
107   }
108 
109   assert(new_argc > 0);
110 
111   /* Get the mount request from VFS, so we can deal with it later. */
112   (void)get_work(&fs_msg, &fs_ipc_status);
113   fs_pending = TRUE;
114 
115   return __real_main(new_argc, new_argv);
116 }
117 
118 /*
119  * Receive a message unless one was already pending.  Process the message, and
120  * send a reply if necessary.  Return whether puffs should keep running.
121  */
122 int
123 lpuffs_pump(void)
124 {
125 
126 	if (fs_pending == TRUE || get_work(&fs_msg, &fs_ipc_status) == OK) {
127 		fs_pending = FALSE;
128 
129 		fsdriver_process(&puffs_table, &fs_msg, fs_ipc_status, FALSE);
130 	}
131 
132 	return mounted || !exitsignaled;
133 }
134 
135 /*
136  * Initialize MINIX3-specific settings.
137  */
138 void
139 lpuffs_init(struct puffs_usermount * pu)
140 {
141 
142 	buildpath = pu->pu_flags & PUFFS_FLAG_BUILDPATH; /* XXX */
143 
144 	LIST_INIT(&pu->pu_pnode_removed_lst);
145 
146 	global_pu = pu;
147 }
148