ETXTBSY以及如何覆盖它 [英] ETXTBSY and how to override it

查看:799
本文介绍了ETXTBSY以及如何覆盖它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写一个正在执行的可执行文件,但是我无法打开写入。例如:

  #include< stdio.h> 
#include< fcntl.h>
$ b $ int main(int argc,char ** argv)
{
int fd = open(argv [0],O_RDWR);
if(fd == -1)perror(NULL);
返回0;

$ / code>



 
%uname -rs
FreeBSD 8.0-STABLE
%./example_ETXTBSY
文本文件繁忙

一些解释在Linux中ETXTBSY到底是什么,但是,是否有可能重写这个错误?



PS

我不是在写病毒。

解决方案

如果你是试图替换一个正在执行的文件,而不是修改一个可执行文件,你可以先取消链接(),然后打开它进行写入。

  #include< stdio.h> 
#include< fcntl.h>
#include< sys / stat.h>
$ b $ int main(int argc,char ** argv)
{
unlink(argv [0]);
int fd = open(argv [0],O_RDWR | O_CREAT,S_IRWXU | S_IRWXG | S_IRWXO);
if(fd == -1)perror(NULL);
返回0;





$ b如果你想要访问实际的运行过程,是ptrace()。



(编辑添加模式位)

I need to write to an executable file that is being executed, but I can't open it for writing. For example:

#include <stdio.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd = open(argv[0], O_RDWR);
    if (fd == -1) perror(NULL);
    return 0;
}

% uname -rs
FreeBSD 8.0-STABLE
% ./example_ETXTBSY
Text file busy

There are some explanations what the heck is ETXTBSY in Linux, but nevertheless, is it possible to override this error?

P.S.
I'm not trying to write a virus.

解决方案

If you are trying to replace an executing file, as opposed to modifying an executable on the fly, you can unlink() it first and then open it for writing.

#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
    unlink(argv[0]);
    int fd = open(argv[0], O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO);
        if (fd == -1) perror(NULL);
            return 0;
}

If you are trying to get access to the actual running process, your best bet is ptrace().

(Edited to add the mode bits.)

这篇关于ETXTBSY以及如何覆盖它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆