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

fix: open the file lock on "UNIX" with O_RDRW #791

Merged
merged 3 commits into from
Jan 26, 2023
Merged
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: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ jobs:

- name: Expose llvm PATH for Mac
if: ${{ runner.os == 'macOS' }}
run: echo $(brew --prefix llvm@14)/bin >> $GITHUB_PATH
run: echo $(brew --prefix llvm@15)/bin >> $GITHUB_PATH

- name: Installing Android SDK Dependencies
if: ${{ env['ANDROID_API'] }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

**Fixes**:

- Open the database file-lock on "UNIX" with `O_RDRW` ([#791](https://github.com/getsentry/sentry-native/pull/791))
- Better error messages in `sentry_transport_curl`. ([#777](https://github.com/getsentry/sentry-native/pull/777))
- Fix sporadic crash on Windows due to race condition when initializing background-worker thread-id. ([#785](https://github.com/getsentry/sentry-native/pull/785))
- Increased curl headers buffer size to 512 (in `sentry_transport_curl`). ([#784](https://github.com/getsentry/sentry-native/pull/784))
Expand Down
10 changes: 1 addition & 9 deletions src/path/sentry_path_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,7 @@ sentry__filelock_try_lock(sentry_filelock_t *lock)
{
lock->is_locked = false;

const int oflags =
#ifdef SENTRY_PLATFORM_AIX
// Under AIX, O_TRUNC can only be set if it can be written to, and
// flock (well, fcntl) will return EBADF if the fd is not read-write.
O_RDWR | O_CREAT | O_TRUNC;
#else
O_RDONLY | O_CREAT | O_TRUNC;
#endif
int fd = open(lock->path->path, oflags,
int fd = open(lock->path->path, O_RDWR | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd < 0) {
return false;
Expand Down
16 changes: 12 additions & 4 deletions src/sentry_database.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "sentry_json.h"
#include "sentry_options.h"
#include "sentry_session.h"
#include <errno.h>
#include <string.h>

sentry_run_t *
Expand Down Expand Up @@ -49,13 +50,20 @@ sentry__run_new(const sentry_path_t *database_path)
run->run_path = run_path;
run->session_path = session_path;
run->lock = sentry__filelock_new(lock_path);
if (!run->lock || !sentry__filelock_try_lock(run->lock)) {
sentry__run_free(run);
return NULL;
if (!run->lock) {
goto error;
}
if (!sentry__filelock_try_lock(run->lock)) {
SENTRY_WARNF("failed to lock file \"%s\" (%s)", lock_path->path,
strerror(errno));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we go the extra mile and use the thread-safe alternatives to strerror()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think it hurts, though we have a lock around sentry_init, and it is supposed to be called from the main thread during initialization, so maybe not worth it if its a large effort?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not a massive effort, but strerror_r() behaves differently in different environments, so I would probably add a wrapper with the right #ifdefs to sentry_util.

Interestingly while we do access errno in a couple of places, I haven't seen any conversions to readable strings for logging (though we also log write_buffer_with_flags()). What I am trying to get across here: while it might not be strictly necessary to have a thread-safe errno printer, I can imagine this to help with logging in other UNIXy code.

goto error;
}

sentry__path_create_dir_all(run->run_path);
return run;

error:
sentry__run_free(run);
return NULL;
}

void
Expand Down