Cannot create file on SD card from Sandboxed App

I’m having trouble creating a file on the SD card from a Sandboxed App. Here is what works. I can manually create a file on the SD card from a command prompt, so I have mounted the SD card correctly. When I run my simple app, it executes successfully but no file is created on the SD card. From my research, I believe I need a SMACK setting of some type, but can’t figure out what to do.

I have attached my app code below and would appreciate any help.

fstest2.adef file

sandboxed: true
version: 1.0.0
maxFileSystemBytes: 512K
start: manual

executables:
{
fstest2 = ( fstest2Component )
}

processes:
{
envVars:
{
LE_LOG_LEVEL = DEBUG
}

run:
{
	( fstest2 )
}

faultAction: restart

}

requires:
{
device:
{
// Request read and write access to the SD card.
[rw] /dev/mmcblk0p1 /dev/mmcblk0p1
}

dir:
{
	/mnt/userrw/sdcard	/
}

}

Component.cdef file
sources:
{
fstest2Component.c
}

fstest2Component.c file

#include “legato.h”

COMPONENT_INIT
{
LE_INFO(“Test File Creation.”);
uint8_t write_buffer_tx[] =
{
0x54, 0x45, 0x53, 0x54, 0x32
};
le_result_t res;
le_fs_FileRef_t fp;
size_t filesize;
res = le_fs_Open("/test2.txt", LE_FS_CREAT|LE_FS_RDWR|LE_FS_APPEND|LE_FS_SYNC , &fp);
if(res == LE_OK){
LE_INFO(“File Created Successfully.”);
} else {
LE_INFO(“File Creation Failed %d.”, res);
}
res = le_fs_Write(fp, write_buffer_tx, NUM_ARRAY_MEMBERS(write_buffer_tx));
if(res == LE_OK){
LE_INFO(“File Write Successful.”);
} else {
LE_INFO(“File Write Failed %d.”, res);
}
le_fs_Close(fp);
res = le_fs_GetSize("/test2.txt", &filesize);
if(res == LE_OK){
LE_INFO(“File Get Size Successful. size=%d”, filesize);
} else {
LE_INFO(“File Get Size Failed %d.”, res);
}
}

Log Output

Oct 8 22:37:14 | fstest2[16521]/fstest2_exe T=main | _main.c main() 61 | == Starting Event Processing Loop ==
Oct 8 22:37:14 | fstest2[16521]/fstest2Component T=main | fstest2Component.c _fstest2Component_COMPONENT_INIT() 5 | Test File Creation.
Oct 8 22:37:14 | fstest2[16521]/fstest2Component T=main | fstest2Component.c _fstest2Component_COMPONENT_INIT() 15 | File Created Successfully.
Oct 8 22:37:14 | fstest2[16521]/fstest2Component T=main | fstest2Component.c _fstest2Component_COMPONENT_INIT() 21 | File Write Successful.
Oct 8 22:37:14 | fstest2[16521]/fstest2Component T=main | fstest2Component.c _fstest2Component_COMPONENT_INIT() 28 | File Get Size Successful. size=5

Does it work properly unsandboxed?

Can you change the following line to
res = le_fs_Open(“/test2.txt”, LE_FS_CREAT|LE_FS_RDWR|LE_FS_APPEND|LE_FS_SYNC , &fp);
to
res = le_fs_Open(“/sdcard/test2.txt”, LE_FS_CREAT|LE_FS_RDWR|LE_FS_APPEND|LE_FS_SYNC , &fp);

with the app UnSandboxed, files get created in /data/le_fs. I made the change requested and it created the file in /data/le_fs/sdcard/test2.txt. With the app Sandboxed, the app runs successfully, but I can’t find the file on the SD card or /data/le_fs.

Try just using the linux file calls such as open().

res = le_fs_Open(“/test2.txt”, LE_FS_CREAT|LE_FS_RDWR|LE_FS_APPEND|LE_FS_SYNC , &fp);
should be something like
FILE* fptr = fopen(“/sdcard/test2.txt”, “a”);

I get the following error: File Creation Failed ‘Permission denied’.
So I guess my problem now is at the OS level?

Can you run the app unsandboxed?

Unsandboxed, if I try fopen("/sdcard/test3.txt", “a”) then I get [LE_FILENAME fs_TryLazyUmount() 97 | Could not lazy unmount ‘/legato/systems/current/appsWriteable/.new.fstest3’. No such file or directory.]

if I try fopen("/test3.txt", “a”) then I get [File Creation Failed ‘Read-only file system’.]

folder permissions are as follows:
drwxr-xr-x 2 root root 16384 Oct 11 22:53 sdcard

I can create a file in the sdcard folder at a command line.

I finally got the unsandboxed app to write to the SD card by removing the “requires:” section from the adef file.

So my question now is, what to I need to add to the adef file to allow it work sandboxed.

Can you change the folder permissions by doing chmod? Currently, you seem to have only root access.
If you cannot change, permissions or it doesnt work then it is most probably related to the SMACK settings and there is not much we can do.

I have tried changing folder permissions and folder ownership, but neither work. So can a sandboxed app create a file in /tmp folder outside the app?

Hey @spmaurer!!

Were you able to find a way to work your application? Also, Could you please list the commands you used to make files and mount the sd card on it, i am encountering errors.

Any help is much appreciated!!

Thanks!