68 lines
1.3 KiB
C
68 lines
1.3 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, "tty", 3) == 0 &&
|
|
strcmp(entry->d_name, "tty") != 0 &&
|
|
strcmp(entry->d_name, "tty0") != 0) {
|
|
|
|
snprintf(device, sizeof(device), "%s%s", DEVICE_PATH, entry->d_name);
|
|
break;
|
|
}
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
if (entry == NULL) {
|
|
fprintf(stderr, "No suitable tty device found (excluding tty and tty0)\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;
|
|
}
|