lockdev/Sanity/basic/test_lockdev.c
František Hrdina 2c6af5856b Add basic test
2024-07-09 09:19:27 +02:00

65 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <lockdev.h>
#define DEVICE_PATH "/dev/"
int main() {
struct dirent *entry;
DIR *dp;
char device[256];
int fd;
dp = opendir(DEVICE_PATH);
if (dp == NULL) {
perror("opendir");
return 1;
}
while ((entry = readdir(dp))) {
if (strncmp(entry->d_name, "ttyS", 4) == 0) {
snprintf(device, sizeof(device), "%s%s", DEVICE_PATH, entry->d_name);
break;
}
}
closedir(dp);
if (entry == NULL) {
fprintf(stderr, "No tty device found ttyS*\n");
return 1;
}
printf("Device in use: %s\n", device);
fd = open(device, O_RDWR);
if (fd == -1) {
perror("open");
return 1;
}
if (dev_lock(device) == -1) {
perror("dev_lock");
close(fd);
return 1;
}
printf("Device is locked.\n");
if (dev_unlock(device, 0) == -1) {
perror("dev_unlock");
close(fd);
return 1;
}
printf("Device is unlocked.\n");
close(fd);
return 0;
}