Search code examples
macrosracketsandbox

Determin if a racket program is in a sandbox


Is it possible to determine if a Racket program is being run in a sandbox?

The reason I ask is because I have a Racket macro that creates a file. And the DrRacket background expander prevents a file from being created (as it should). However, in doing so, it causes an error to appear at the bottom of the window saying the file could not be created.

So, I would like to determine if I am in a sandbox, and if I am, don't create the file, and kindly finish up the macro.


Solution

  • In general, you cannot determine if you are in a sandbox. However, you do have a chance to catch the errors that are thrown when you try to perform a restricted operation. However, the catch is that you do not know what type of error is going to be thrown. So one thing that you can do is to just catch all of them. Use with-handlers to catch the error and exn:fail? to catch all errors.

    (with-handlers ([exn:fail?
                     (lambda (x) (displayln "failing cleanly"))])
        (make-temporary-file))
    

    Be careful here that an error here may occur that is not related to being in a sandbox. For example, you could potentially get an error just because a file could not be created:

    (with-handlers ([exn:fail:filesystem?
                     (lambda (x) (displayln "Coudln't open file"))]
                    [exn:fail?
                     (lambda (x) (displayln "failing gracefully"))])
      (make-temporary-file))