Skip to content

Commit 113cd4b

Browse files
committed
Harden service init/exit lifecycle and cleanup flow
1 parent ebbcfcc commit 113cd4b

File tree

6 files changed

+49
-70
lines changed

6 files changed

+49
-70
lines changed

SD/3ds/FirmMux.3dsx

592 Bytes
Binary file not shown.

SD/3ds/FirmMux/boot.3dsx

592 Bytes
Binary file not shown.

SD/cias/FirmMux.cia

0 Bytes
Binary file not shown.

build_number.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
282
1+
283

include/build_id.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#ifndef FIRMUX_BUILD_ID
2-
#define FIRMUX_BUILD_ID "Build:282"
2+
#define FIRMUX_BUILD_ID "Build:283"
33
#endif

source/main.c

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4197,24 +4197,38 @@ int main(int argc, char** argv) {
41974197
aptJumpToHomeMenu();
41984198
return 0;
41994199
#endif
4200+
bool gfx_inited = false;
4201+
bool c3d_inited = false;
4202+
bool c2d_inited = false;
4203+
bool fs_inited = false;
4204+
bool ptmu_inited = false;
4205+
bool ptmsysm_inited = false;
4206+
bool ac_inited = false;
4207+
bool cfgu_inited = false;
4208+
bool mcu_inited = false;
4209+
bool audio_inited = false;
4210+
int rc = 1;
4211+
42004212
gfxInitDefault();
4213+
gfx_inited = true;
42014214
if (!C3D_Init(C3D_DEFAULT_CMDBUF_SIZE)) {
4202-
gfxExit();
4203-
return 1;
4215+
goto cleanup;
42044216
}
4217+
c3d_inited = true;
42054218
if (!C2D_Init(8192)) {
4206-
C3D_Fini();
4207-
gfxExit();
4208-
return 1;
4219+
goto cleanup;
42094220
}
4221+
c2d_inited = true;
42104222
C2D_Prepare();
42114223
C3D_AlphaBlend(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
4212-
ptmuInit();
4213-
ptmSysmInit();
4214-
acInit();
4215-
cfguInit();
4224+
if (R_FAILED(fsInit())) goto cleanup;
4225+
fs_inited = true;
4226+
if (R_SUCCEEDED(ptmuInit())) ptmu_inited = true;
4227+
if (R_SUCCEEDED(ptmSysmInit())) ptmsysm_inited = true;
4228+
if (R_SUCCEEDED(acInit())) ac_inited = true;
4229+
if (R_SUCCEEDED(cfguInit())) cfgu_inited = true;
42164230
load_time_format();
4217-
mcuHwcInit();
4231+
if (R_SUCCEEDED(mcuHwcInit())) mcu_inited = true;
42184232
aptSetHomeAllowed(true);
42194233
{
42204234
bool is_new = false;
@@ -4229,40 +4243,12 @@ int main(int argc, char** argv) {
42294243
g_top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
42304244
g_bottom = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);
42314245
if (!g_textbuf || !g_top || !g_bottom) {
4232-
icon_free(&g_title_preview_icon);
4233-
icon_free(&g_hb_preview_icon);
4234-
free_fonts();
4235-
if (g_textbuf) C2D_TextBufDelete(g_textbuf);
4236-
C2D_Fini();
4237-
C3D_Fini();
4238-
mcuHwcExit();
4239-
cfguExit();
4240-
ptmSysmExit();
4241-
ptmuExit();
4242-
acExit();
4243-
ndspExit();
4244-
fsExit();
4245-
gfxExit();
4246-
return 1;
4246+
goto cleanup;
42474247
}
42484248

42494249
Config* cfg = &g_cfg;
42504250
if (!load_or_create_config(cfg)) {
4251-
icon_free(&g_title_preview_icon);
4252-
icon_free(&g_hb_preview_icon);
4253-
free_fonts();
4254-
C2D_TextBufDelete(g_textbuf);
4255-
C2D_Fini();
4256-
C3D_Fini();
4257-
mcuHwcExit();
4258-
cfguExit();
4259-
ptmSysmExit();
4260-
ptmuExit();
4261-
acExit();
4262-
ndspExit();
4263-
fsExit();
4264-
gfxExit();
4265-
return 1;
4251+
goto cleanup;
42664252
}
42674253
for (int i = 0; i < cfg->target_count; i++) {
42684254
Target* t = &cfg->targets[i];
@@ -4295,21 +4281,7 @@ int main(int argc, char** argv) {
42954281

42964282
State* state = &g_state;
42974283
if (!load_state(state)) {
4298-
icon_free(&g_title_preview_icon);
4299-
icon_free(&g_hb_preview_icon);
4300-
free_fonts();
4301-
C2D_TextBufDelete(g_textbuf);
4302-
C2D_Fini();
4303-
C3D_Fini();
4304-
mcuHwcExit();
4305-
cfguExit();
4306-
ptmSysmExit();
4307-
ptmuExit();
4308-
acExit();
4309-
ndspExit();
4310-
fsExit();
4311-
gfxExit();
4312-
return 1;
4284+
goto cleanup;
43134285
}
43144286

43154287
if (!cfg->remember_last_position) {
@@ -4362,7 +4334,11 @@ int main(int argc, char** argv) {
43624334
rebuild_targets_from_backend(cfg, state, &current_target, &state_dirty, status_message, sizeof(status_message), &status_timer);
43634335

43644336
apply_theme_from_state_or_config(cfg, state);
4365-
audio_init();
4337+
audio_inited = audio_init();
4338+
if (!audio_inited) {
4339+
snprintf(status_message, sizeof(status_message), "Audio init failed");
4340+
status_timer = 180;
4341+
}
43664342
audio_set_bgm_enabled(state->bgm_enabled);
43674343

43684344
if (state->last_target[0] == 0) copy_str(state->last_target, sizeof(state->last_target), cfg->default_target);
@@ -6091,23 +6067,26 @@ int main(int argc, char** argv) {
60916067
}
60926068
}
60936069

6070+
rc = 0;
6071+
6072+
cleanup:
60946073
preview_manager_shutdown();
60956074
runtime_cache_shutdown();
60966075
icon_free(&g_top_bg_tex);
60976076
icon_free(&g_bottom_bg_tex);
60986077
icon_free(&g_title_preview_icon);
60996078
icon_free(&g_hb_preview_icon);
61006079
free_fonts();
6101-
C2D_TextBufDelete(g_textbuf);
6102-
C2D_Fini();
6103-
C3D_Fini();
6104-
mcuHwcExit();
6105-
cfguExit();
6106-
ptmSysmExit();
6107-
ptmuExit();
6108-
acExit();
6109-
ndspExit();
6110-
fsExit();
6111-
gfxExit();
6112-
return 0;
6080+
if (g_textbuf) C2D_TextBufDelete(g_textbuf);
6081+
if (c2d_inited) C2D_Fini();
6082+
if (c3d_inited) C3D_Fini();
6083+
if (mcu_inited) mcuHwcExit();
6084+
if (cfgu_inited) cfguExit();
6085+
if (ptmsysm_inited) ptmSysmExit();
6086+
if (ptmu_inited) ptmuExit();
6087+
if (ac_inited) acExit();
6088+
if (audio_inited) ndspExit();
6089+
if (fs_inited) fsExit();
6090+
if (gfx_inited) gfxExit();
6091+
return rc;
61136092
}

0 commit comments

Comments
 (0)