在我看来,使用 POSIX共享内存对象很简单:
POSIX 共享内存对象具有内核持久性:共享内存对象将一直存在,直到系统关闭,或者直到所有进程都取消映射该对象并使用 shm_unlink 将其删除
每当您的程序启动时,它都可以shm_open
创建一个具有一致名称的新对象并将所有者设置为root
. 该对象不需要包含任何特定的值。POSIX 要求所有共享内存对象在重新启动之前一直存在,除非手动销毁(只有其所有者或创建者可以这样做......在这种情况下是 root 用户)。
每当您的程序启动时,它首先检查是否已经存在这样的共享内存对象,并以 root 作为所有者。由于只有 root 可以创建这样的对象,并且只有 root 或重新启动才能破坏它,因此您可以确定自上次重新启动以来您的程序是否已启动,唯一可能的规避是 root 用户shm_unlink
手动调用该对象.
我在下面写了一个测试和设置函数,它应该完全符合你的需要。除了所有权设置/检测之外,它可以工作:由于某种未知的原因shmctl
,我的系统上的两个调用都失败了,说“无效参数”。该man
页面shmctl
表示该EINVAL
错误指示无效的内存对象标识符或无效的命令。但是IPC_SET
andIPC_STAT
命令肯定是有效的,您可以观察程序的输出以查看每次创建和/或打开的有效对象标识符。
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <stdlib.h>
int rebooted_test_and_set() {
int err;
int rebooted;
struct shmid_ds shmst;
// create object if nonexistent, returning failure if already exists
int shmid = shm_open("/bootcheck", O_CREAT | O_EXCL);
if (shmid != -1) {
fprintf(stderr, "bootcheck object did not exist, so created: %d\n", shmid);
// object did not exist, so system has been rebooted
rebooted = 1;
// set owner to root, and no permissions for anyone
shmst.shm_perm.uid = 0;
shmst.shm_perm.gid = 0;
shmst.shm_perm.mode = 0;
if ((err = shmctl(shmid, IPC_SET, &shmst)) == -1) {
perror("shmctl: shmctl failed to set owner and permissions for bootcheck object");
exit(1);
}
} else {
// object already exists, so reopen with read access and verify that the owner is root
shmid = shm_open("/bootcheck", O_RDONLY);
if (shmid == -1) {
perror("shm_open: failed, perhaps due to insufficient privileges");
exit(1);
}
fprintf(stderr, "bootcheck object (%d) exists, so checking ownership\n", shmid);
if ((err = shmctl(shmid, IPC_STAT, &shmst)) == -1) {
perror("shmctl: shmctl failed");
exit(1);
}
if (shmst.shm_perm.uid == 0) {
// yes, the bootcheck owner is root,
// so we are confident the system has NOT been rebooted since last launch
rebooted = 0;
} else {
// uh oh, looks like someone created the object illegitimately.
// since that is only possible if the root-owned object did not exist,
// therefore we know that it never did exist since the last reboot
rebooted = 1;
}
}
return rebooted;
}
// for debugging purposes ONLY, so I don't have to keep rebooting to clear the object:
void rebooted_clear() {
if (shm_unlink("/bootcheck") == -1) {
perror("shm_unlink: failed, probably due to insufficient privileges or object nonexistent");
exit(1);
}
}
int main() {
int rebooted = rebooted_test_and_set();
printf("rebooted since last launch: %d\n", rebooted);
return 0;
}
如果有人有任何线索,我很难过。POSIX 共享内存的一些信息和示例在这里。