c - 更改 argv[0] 在 Ubuntu 中不起作用 (C)

标签 c gcc ubuntu

所以我有一个在 C 运行时更改进程名称的任务,我偶然发现了这个:http://www.uofr.net/~greg/processname.html

现在,首先,我意识到改变 argv[0] 的危险(但在 Linux 和 UNIX 方面还有很长的路要走),所以请不要讲授它,我只是想知道为什么会这样无法在 Ubuntu 上运行。

这是我使用的测试源:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main(int argc, char *argv[]) {

    int argv0size = strlen(argv[0]);
    int onetwothree = 1;
    char* abc = (char *) malloc(sizeof(char) * (17 + 1));
    sprintf(abc,"ApplicationName%.3d",onetwothree); 
    strncpy(argv[0], abc ,argv0size);     
    getchar(); 
}

我只在 Mac OS X 和 Ubuntu 上测试过它,它可以在 Mac OS X 上运行。我不知道为什么它不能在 Ubuntu 上运行。它不起作用的可能原因是什么?

我使用 gcc 作为编译器。

最佳答案

So I have this task of changing the process name during runtime in C, and I've stumbled upon this: http://www.uofr.net/~greg/processname.html

我建议您使用的引用资料不是由操作系统或 C 语言专家编写的。也许它适用于某些操作系统,但不需要在 C 语言中以这种方式工作。

Now before anything else, I am aware of the dangers of altering argv[0] (but have a long way to go about Linux and UNIX stuffs) so please don't lecture about it, I just want to know why it isn't working on Ubuntu.

正如 WhozCraig 在评论中所说,您当然可以修改 argv 数组以及它指向的数组。这并不意味着 Ubuntu 需要在其进程列表中使用该修改。

I've tested it on Mac OS X and Ubuntu only, and it's working on Mac OS X. I don't know why it's not working on Ubuntu. What could be the possible reasons for it not working?

如果您正在寻找理由,请考虑滥用的可能性。如果在进程列表中使用您自己的 argv[0] 副本,kaiten 等特洛伊木马将能够像以前一样伪造其进程名称,从而使它们更难以检测和删除。

关于c - 更改 argv[0] 在 Ubuntu 中不起作用 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15778162/

相关文章:

C - 强制字符串参数在只读内存中

c++ - 如何使 "whose type uses the anonymous namespace [-Werror]"gcc 版本 4.8.2 静音

python - 坚持在 Python 中使用 Megam(nltk.classify.MaxentClassifier)

ubuntu - Launchpad PPA 的包装

c - c代码的性能

php - 在 Ubuntu Linux 12 中启用 Excel PHP PDO 驱动程序

c - 此代码是否由 C 标准保证?

c - OpenSSL RSA_sign() 返回零错误代码

c - 如何在 macOs Mojave 上设置环境变量?

c - C 链表或二叉树中的 argc/argv