Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow PRoot to directly access FDs #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions exports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
libandroid_shmget;
libandroid_shmat;
libandroid_shmdt;
libandroid_shmat_fd;
libandroid_shmdt_fd;
shmctl;
shmget;
shmat;
Expand Down
3 changes: 3 additions & 0 deletions shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ extern void *shmat(int shmid, void const* shmaddr, int shmflg);
#define shmdt libandroid_shmdt
extern int shmdt(void const* shmaddr);

extern int libandroid_shmat_fd(int shmid, size_t* out_size);
extern int libandroid_shmdt_fd(int fd);

__END_DECLS

#endif
54 changes: 54 additions & 0 deletions shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,60 @@ int shmdt(void const* shmaddr)
return 0;
}

/* Let PRoot attach shared memory segment to another process. */
int libandroid_shmat_fd(int shmid, size_t* out_size)
{
ashv_check_pid();

int socket_id = ashv_socket_id_from_shmid(shmid);
int fd;

pthread_mutex_lock(&mutex);

int idx = ashv_find_local_index(shmid);
if (idx == -1 && socket_id != ashv_local_socket_id) {
idx = ashv_read_remote_segment(shmid);
}

if (idx == -1) {
DBG ("%s: shmid %x does not exist", __PRETTY_FUNCTION__, shmid);
pthread_mutex_unlock(&mutex);
errno = EINVAL;
return -1;
}

fd = shmem[idx].descriptor;
*out_size = shmem[idx].size;
DBG ("%s: mapped for FD %d ID %d", __PRETTY_FUNCTION__, shmem[idx].descriptor, idx);
pthread_mutex_unlock (&mutex);

return fd;
}

/* Let PRoot detach shared memory segment after last process detached. */
int libandroid_shmdt_fd(int fd)
{
ashv_check_pid();

pthread_mutex_lock(&mutex);
for (size_t i = 0; i < shmem_amount; i++) {
if (shmem[i].descriptor == fd) {
DBG("%s: unmapped for FD %d ID %zu shmid %x", __PRETTY_FUNCTION__, shmem[i].descriptor, i, shmem[i].id);
if (shmem[i].markedForDeletion || ashv_socket_id_from_shmid(shmem[i].id) != ashv_local_socket_id) {
DBG ("%s: deleting shmid %x", __PRETTY_FUNCTION__, shmem[i].id);
android_shmem_delete(i);
}
pthread_mutex_unlock(&mutex);
return 0;
}
}
pthread_mutex_unlock(&mutex);

DBG("%s: invalid fd %d", __PRETTY_FUNCTION__, fd);
/* Could be a remove segment, do not report an error for that. */
return 0;
}

/* Shared memory control operation. */
int shmctl(int shmid, int cmd, struct shmid_ds *buf)
{
Expand Down