2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
5 Desc: Common startup code
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
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
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
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()
33 #include <aros/config.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
;
60 D(bug("Entering __startup_detach(\"%s\", %d, %x)\n", argstr
, argsize
, SysBase
));
63 /* Without a CLI detaching makes no sense, just jump to
67 __startup_entries_next();
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
},
87 __detacher_process
= (struct Process
*)FindTask(NULL
);
89 /* CreateNewProc() will take care of freeing the seglist */
90 newproc
= CreateNewProc(tags
);
95 cli
->cli_Module
= mysegment
;
96 __detached_return_value
= RETURN_ERROR
;
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");
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
))
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
)
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. */
148 D(bug("Leaving __detach_trampoline\n"));
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" */
170 __detacher_process
= NULL
;
173 D(bug("Leaving __Detach\n"));