And*_*rew 16 linux directory directory-structure
我正在开发一个 LAMP 网络应用程序,并且在某处有一个计划进程,它不断创建一个shop
在站点根目录中调用的文件夹。每次出现这种情况都会与应用程序中的重写规则发生冲突,这不好。
在我找到有问题的脚本之前,有没有办法防止shop
在根目录中创建任何名为的文件夹?我知道我可以更改文件夹的权限以防止其内容被更改,但我还没有找到一种方法来防止创建特定名称的文件夹。
hee*_*ayl 31
您不能,因为创建目录的用户具有在父目录上写入的足够权限。
相反,您可以利用inotify
Linux 内核提供的系统调用系列来监视给定目录mv
中目录的创建(和可选-ing)shop
,如果已创建(或可选mv
-ed)rm
目录。
在这种情况下您需要的用户空间程序是inotifywait
(随附inotify-tools
,如果需要请先安装)。
假设目录shop
将驻留在/foo/bar
目录中,让我们为/foo/bar/shop
创建设置监控,rm
如果创建则立即:
inotifywait -qme create /foo/bar | \
awk '/,ISDIR shop$/ { system("rm -r -- /foo/bar/shop") }'
Run Code Online (Sandbox Code Playgroud)
inotifywait -qme create /foo/bar
手表/foo/bar
目录可能创建的,即手表任何任何文件/目录create
事件
如果创建,则awk '/,ISDIR shop$/ { system("rm -r -- /foo/bar/shop") }'
检查该文件是否恰好是一个目录且名称为shop
( /,ISDIR shop$/
),如果是,则检查rm
该目录 ( system("rm -r -- /foo/bar/shop")
)
您需要以对目录具有写权限的用户身份运行该命令才能从目录/foo/bar
中删除shop
。
如果您也想监视mv
-ing 操作,也可以添加监视moved_to
事件:
inotifywait -qme create,moved_to /foo/bar | \
awk '/,ISDIR shop$/ { system("rm -r -- /foo/bar/shop") }'
Run Code Online (Sandbox Code Playgroud)
请注意,如果您正在寻找一个名为 的文件,而不是目录shop
:
inotifywait -qme create /foo/bar | \
awk '$NF == "shop" { system("rm -- /foo/bar/shop") }'
inotifywait -qme create,moved_to /foo/bar | \
awk '$NF == "shop" { system("rm -- /foo/bar/shop") }'
Run Code Online (Sandbox Code Playgroud)
Mia*_*ati 30
根据防止创建某个名称的文件夹的问题,从字面上回答。
touch shop
如果存在同名文件,则无法创建目录
mkdir: cannot create directory ‘shop’: File exists
mkdir
用LD_PRELOAD
...劫持系统调用怎么样?
$ ls
test.c
$ cat test.c
#define _GNU_SOURCE
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dlfcn.h>
typedef int (*orig_mkdir_func_type)(const char *path, mode_t mode);
int mkdir(const char *path, mode_t mode) {
if(!strcmp(path, "shop")) return 1;
orig_mkdir_func_type orig_func;
orig_func = (orig_mkdir_func_type)dlsym(RTLD_NEXT, "mkdir");
return orig_func(path, mode);
}
$ gcc -shared -fPIC test.c -o test.so
$ LD_PRELOAD='./test.so' mkdir test
$ LD_PRELOAD='./test.so' mkdir shop
mkdir: cannot create directory ‘shop’: No such file or directory
$ ls
test test.c test.so
Run Code Online (Sandbox Code Playgroud)
请注意,在此处理程序中,您可以记录要创建此目录的进程的 PID:
$ cat test.c
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dlfcn.h>
typedef int (*orig_mkdir_func_type)(const char *path, mode_t mode);
int mkdir(const char *path, mode_t mode) {
if(!strcmp(path, "shop")) {
FILE* fp = fopen("/tmp/log.txt", "w");
fprintf(fp, "PID of evil script: %d\n", (int)getpid());
fclose(fp);
}
orig_mkdir_func_type orig_func;
orig_func = (orig_mkdir_func_type)dlsym(RTLD_NEXT, "mkdir");
return orig_func(path, mode);
}
$ gcc -shared -fPIC test.c -o test.so
$ LD_PRELOAD='./test.so' mkdir shop
$ cat /tmp/log.txt
PID of evil script: 8706
Run Code Online (Sandbox Code Playgroud)
您需要将它放在~/.bashrc
根目录(或运行您的应用程序的任何人)中以确保将使用它:
export LD_PRELOAD=/path/to/test.so
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4416 次 |
最近记录: |