Reputation: 2092
Please help us port the lockf
C call to Android. A situation is surrounded by #ifndef
/ #endif
lines here: https://github.com/PurpleI2P/i2pd/blob/710a35993db02644f7f28dbee739fe1e7a68a5a5/daemon/UnixDaemon.cpp#L164
I've tried flock
as suggested by SO answer [2] but it appears that flock
on a local Android (ndk v. 23.2.8568313
) is a struct
, not a function.
Here [1] there are words about per-process file and directory locking, will now experiment with that...
[1] https://stackoverflow.com/a/10308744/529442
[2] https://stackoverflow.com/a/24170314/529442
Upvotes: 2
Views: 71
Reputation: 196
Here is the implementation of lockf
in bionic that uses fcntl
. It is available in API level >= 24.
Upvotes: 0
Reputation: 2092
A working example inspired by a comment by @IanAbbott :
std::string pidfile="/storage/emulated/0/locktest"; //make sure it's writable for you
auto pidFH = open(pidfile.c_str(), O_RDWR | O_CREAT, 0600);
if (pidFH < 0)
{
std::cerr << "Could not create locktest file " << pidfile << ": " << strerror(errno) << std::endl;
return 1;
}
struct flock fl;
fl.l_len = 0;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
if (fcntl(pidFH, F_SETLK, &fl) != 0)
{
std::cerr << "Could not lock locktest file " << pidfile << ": " << strerror(errno) << std::endl;
return 2;
}
A code repository with this example: https://github.com/PurpleI2P/i2pd/pull/2052/commits/8e80a8b06fca685bdef778aae5698de03dd755a5
Upvotes: 0