33 lines
1.3 KiB
C
33 lines
1.3 KiB
C
#define _POSIX_C_SOURCE 200809L
|
|
#include "hid_device.h"
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <sys/file.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
int main(void)
|
|
{
|
|
/* Real process/lock semantics exercised only on a temporary regular file,
|
|
* using the production admission function; no HID descriptor is opened. */
|
|
char path[] = "/tmp/labfy-lock-XXXXXX";
|
|
int fd = mkstemp(path); assert(fd >= 0 && hid_lock(fd) == 0);
|
|
int notify[2], resume[2]; assert(pipe(notify) == 0 && pipe(resume) == 0);
|
|
pid_t child = fork(); assert(child >= 0);
|
|
if (!child) {
|
|
assert(close(fd) == 0);
|
|
int other = open(path, O_RDWR); assert(other >= 0);
|
|
assert(hid_lock(other) == -1 && errno == EWOULDBLOCK);
|
|
assert(write(notify[1], "x", 1) == 1);
|
|
char byte; assert(read(resume[0], &byte, 1) == 1);
|
|
assert(hid_lock(other) == 0 && close(other) == 0);
|
|
_exit(0);
|
|
}
|
|
char byte; assert(read(notify[0], &byte, 1) == 1);
|
|
assert(close(fd) == 0 && write(resume[1], "x", 1) == 1);
|
|
int result; assert(waitpid(child, &result, 0) == child && WIFEXITED(result) && WEXITSTATUS(result) == 0);
|
|
assert(close(notify[0]) == 0 && close(notify[1]) == 0 && close(resume[0]) == 0 && close(resume[1]) == 0);
|
|
assert(unlink(path) == 0);
|
|
return 0;
|
|
}
|