define __KERNEL_STRICT_NAMES to avoid inclusion of kernel types on systems that carry...
[cake.git] / compiler / startup / detach.c
blobadb6bdc00a010ed58ed8b2be7ce5b165d2a4dc7e
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Common startup code
6 Lang: english
8 Use: To make a program detach itself from the launching CLI, use
9 detach.o as your program's start file, before any other usual start
10 file.
12 If you want the detached process to have a name different than the
13 detacher's one, define the following variable in your program's
14 source code:
16 STRPTR __detached_name = "My Preferred Program Name";
18 You can also decide to control exactly when to detach your program
19 from the shell. To do so, simply use the function
21 Detach();
23 declared in
25 <aros/detach.h>
27 when you want to detach the program. Not using this function will
28 result in your program being detached from the shell before the main()
29 function is reached.
31 #define DEBUG 0
33 #include <aros/config.h>
34 #include <dos/dos.h>
35 #include <proto/exec.h>
36 #include <proto/dos.h>
37 #include <aros/asmcall.h>
38 #include <aros/debug.h>
39 #include <aros/symbolsets.h>
40 #include <aros/startup.h>
42 int __detached_manages_detach;
43 int __detacher_go_away;
44 STRPTR __detached_name;
45 LONG __detached_return_value;
46 struct Process *__detacher_process;
48 AROS_UFP3(static LONG, __detach_trampoline,
49 AROS_UFHA(char *,argstr,A0),
50 AROS_UFHA(ULONG,argsize,D0),
51 AROS_UFHA(struct ExecBase *,SysBase,A6));
53 static void __startup_detach(void)
55 struct CommandLineInterface *cli;
56 struct Process *newproc;
57 BPTR mysegment = NULL;
58 STRPTR detached_name;
60 D(bug("Entering __startup_detach(\"%s\", %d, %x)\n", argstr, argsize, SysBase));
62 cli = Cli();
63 /* Without a CLI detaching makes no sense, just jump to
64 the real program. */
65 if (!cli)
67 __startup_entries_next();
69 else
71 mysegment = cli->cli_Module;
72 cli->cli_Module = NULL;
74 detached_name = __detached_name ? __detached_name : (STRPTR)FindTask(NULL)->tc_Node.ln_Name;
77 struct TagItem tags[] =
79 { NP_Seglist, (IPTR)mysegment },
80 { NP_Entry, (IPTR)&__detach_trampoline },
81 { NP_Name, (IPTR)detached_name },
82 { NP_Arguments, (IPTR)__argstr },
83 { NP_Cli, TRUE },
84 { TAG_DONE, 0 }
87 __detacher_process = (struct Process *)FindTask(NULL);
89 /* CreateNewProc() will take care of freeing the seglist */
90 newproc = CreateNewProc(tags);
93 if (!newproc)
95 cli->cli_Module = mysegment;
96 __detached_return_value = RETURN_ERROR;
98 else
99 while (!__detacher_go_away) Wait(SIGF_SINGLE);
101 if (__detached_return_value != RETURN_OK)
103 PutStr(FindTask(NULL)->tc_Node.ln_Name); PutStr(": Failed to detach.\n");
106 if (newproc)
108 Forbid();
109 Signal(&newproc->pr_Task, SIGF_SINGLE);
112 __aros_startup.as_startup_error = __detached_return_value;
115 D(bug("Leaving __startup_detach\n"));
118 ADD2SET(__startup_detach, program_entries, -100);
120 void __Detach(LONG retval);
122 AROS_UFH3(static LONG, __detach_trampoline,
123 AROS_UFHA(char *,argstr,A0),
124 AROS_UFHA(ULONG,argsize,D0),
125 AROS_UFHA(struct ExecBase *,SysBase,A6))
127 AROS_USERFUNC_INIT
129 LONG retval;
131 D(bug("Entering __detach_trampoline(\"%s\", %d, %x)\n", argstr, argsize, SysBase));
133 /* The program has two options: either take care of telling the detacher
134 process when exactly to go away, via the Detach() function, or let this
135 startup code take care of it. If __detached_manages_detach is TRUE, then
136 the detached process handles it, otherwise we handle it. */
138 if (!__detached_manages_detach)
139 __Detach(RETURN_OK);
141 __startup_entries_next();
143 /* At this point the detacher process might still be around,
144 If the program forgot to detach, or if it couldn't, but in any
145 case we need to tell the detacher to go away. */
146 __Detach(retval);
148 D(bug("Leaving __detach_trampoline\n"));
150 return 0;
152 AROS_USERFUNC_EXIT
155 void __Detach(LONG retval)
157 D(bug("Entering __Detach(%d)\n", retval));
159 if (__detacher_process != NULL)
161 __detached_return_value = retval;
162 __detacher_go_away = TRUE;
164 SetSignal(0, SIGF_SINGLE);
165 /* Tell the detacher process it can now go away */
166 Signal(&__detacher_process->pr_Task, SIGF_SINGLE);
168 /* Wait for it to say "goodbye" */
169 Wait(SIGF_SINGLE);
170 __detacher_process = NULL;
173 D(bug("Leaving __Detach\n"));