@@ -44,6 +44,27 @@ static void mSDLDeinit(struct mSDLRenderer* renderer);
44
44
45
45
static int mSDLRun (struct mSDLRenderer * renderer , struct mArguments * args );
46
46
47
+ #ifdef USE_PLEDGE_UNVEIL
48
+ #include <dirent.h>
49
+ static bool mUnveil (struct mCore * core );
50
+ static bool mPledgeBroad (struct mArguments * args );
51
+ static bool mPledgeNarrow (struct mArguments * args );
52
+
53
+ struct VDirDE ;
54
+ struct VDirEntryDE {
55
+ struct VDirEntry d ;
56
+ struct VDirDE * p ;
57
+ struct dirent * ent ;
58
+ };
59
+
60
+ struct VDirDE {
61
+ struct VDir d ;
62
+ DIR * de ;
63
+ struct VDirEntryDE vde ;
64
+ char * path ;
65
+ };
66
+ #endif
67
+
47
68
static struct VFile * _state = NULL ;
48
69
49
70
static void _loadState (struct mCoreThread * thread ) {
@@ -150,6 +171,15 @@ int main(int argc, char** argv) {
150
171
renderer .player .bindings = & renderer .core -> inputMap ;
151
172
mSDLInitBindingsGBA (& renderer .core -> inputMap );
152
173
mSDLInitEvents (& renderer .events );
174
+
175
+ #ifdef USE_PLEDGE_UNVEIL
176
+ if (!mPledgeBroad (& args )) {
177
+ freeArguments (& args );
178
+ fprintf (stderr , "pledge\n" );
179
+ return 1 ;
180
+ }
181
+ #endif
182
+
153
183
mSDLEventsLoadConfig (& renderer .events , mCoreConfigGetInput (& renderer .core -> config ));
154
184
mSDLAttachPlayer (& renderer .events , & renderer .player );
155
185
mSDLPlayerLoadConfig (& renderer .player , mCoreConfigGetInput (& renderer .core -> config ));
@@ -247,6 +277,17 @@ int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
247
277
state -> close (state );
248
278
}
249
279
}
280
+ #ifdef USE_PLEDGE_UNVEIL
281
+ if (!mUnveil (renderer -> core )) {
282
+ didFail = true;
283
+ fprintf (stderr , "unveil\n" );
284
+ }
285
+
286
+ if (!mPledgeNarrow (args )) {
287
+ didFail = true;
288
+ fprintf (stderr , "pledge\n" );
289
+ }
290
+ #endif
250
291
renderer -> runloop (renderer , & thread );
251
292
mSDLPauseAudio (& renderer -> audio );
252
293
if (mCoreThreadHasCrashed (& thread )) {
@@ -295,3 +336,59 @@ static void mSDLDeinit(struct mSDLRenderer* renderer) {
295
336
296
337
SDL_Quit ();
297
338
}
339
+
340
+ #ifdef USE_PLEDGE_UNVEIL
341
+ static bool mUnveil (struct mCore * core ) {
342
+ int slot , snret ;
343
+ char name [PATH_MAX ];
344
+ struct VDirDE * base = (struct VDirDE * ) core -> dirs .base ;
345
+ for (slot = 1 ; slot < 10 ; slot ++ ) {
346
+ snret = snprintf (name , sizeof (name ), "%s/%s.ss%i" , base -> path , core -> dirs .baseName , slot );
347
+ if (snret == -1 || snret >= sizeof (name )) {
348
+ return false;
349
+ }
350
+ if (unveil (name , "rwc" ) == -1 ) {
351
+ return false;
352
+ }
353
+ }
354
+ return true;
355
+ }
356
+
357
+ static bool mPledgeBroad (struct mArguments * args ) {
358
+ if (args -> debuggerType == DEBUGGER_CLI ) {
359
+ if (pledge ("stdio rpath wpath cpath inet fattr unix dns tty drm audio unveil" , NULL ) == -1 ) {
360
+ return false;
361
+ }
362
+ #ifdef USE_GDB_STUB
363
+ } else if (args -> debuggerType == DEBUGGER_GDB ) {
364
+ if (pledge ("stdio rpath wpath cpath inet fattr unix dns drm audio unveil" , NULL ) == -1 ) {
365
+ return false;
366
+ }
367
+ #endif
368
+ } else {
369
+ if (pledge ("stdio rpath wpath cpath inet fattr unix dns drm audio unveil" , NULL ) == -1 ) {
370
+ return false;
371
+ }
372
+ }
373
+ return true;
374
+ }
375
+
376
+ static bool mPledgeNarrow (struct mArguments * args ) {
377
+ if (args -> debuggerType == DEBUGGER_CLI ) {
378
+ if (pledge ("stdio rpath wpath cpath fattr tty drm audio" , NULL ) == -1 ) {
379
+ return false;
380
+ }
381
+ #ifdef USE_GDB_STUB
382
+ } else if (args -> debuggerType == DEBUGGER_GDB ) {
383
+ if (pledge ("stdio rpath wpath cpath inet fattr drm audio" , NULL ) == -1 ) {
384
+ return false;
385
+ }
386
+ #endif
387
+ } else {
388
+ if (pledge ("stdio rpath wpath cpath fattr drm audio" , NULL ) == -1 ) {
389
+ return false;
390
+ }
391
+ }
392
+ return true;
393
+ }
394
+ #endif
0 commit comments