我有创建/dev/shm/sem.serialize_ld
具有权限的共享信号量的 python 脚本
$ ll /dev/shm/sem.serialize_ld
-rw-rw-rw- 1 buildbot buildbot 32 mar 27 08:36 /dev/shm/sem.serialize_ld
在实践中,它总是由用户 buildbot 创建,但这不是设计假设。有时会发生问题 - 脚本被杀死SIGKILL
并且不释放信号量。开始时新的调用死锁。要手动解决这种情况,我想删除文件并重新开始。但是,只有 buildbot 和 root 用户可以这样做。
为什么其他用户不能删除此文件?
$ rm /dev/shm/sem.serialize_ld
rm: cannot remove '/dev/shm/sem.serialize_ld': Operation not permitted
系统是 Ubuntu 18.04.2 LTS
脚本代码为:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import posix_ipc
import subprocess
import sys
import signal
SEM_NAME = '/serialize_ld'
MAX_CONCURRENT = 2
PROGRAM = '/usr/bin/ld'
def main():
import os
os.umask(0)
sem = posix_ipc.Semaphore(SEM_NAME, posix_ipc.O_CREAT, mode=0o666, initial_value=MAX_CONCURRENT)
sem.acquire()
def release_semaphore(signum, frame):
print("exiting due to signal " + str(signum))
sem.release()
sem.close()
sys.exit(1)
signal.signal(signal.SIGTERM | signal.SIGINT, release_semaphore)
try:
subprocess.call([PROGRAM, *sys.argv[1:]])
finally:
sem.release()
sem.close()
if __name__ == "__main__":
main()