软链接与exec进程替换运行路径问题

文章讨论了如何使用execvp启动新进程new_proc,指出当new_proc位于execvp软链接目录而非源文件目录时,可以成功替换执行。环境变量列表显示了软链接路径而非源文件路径,强调了路径选择在进程替换中的重要性。
摘要由CSDN通过智能技术生成

用execvp软链接启动进程,execvp中进行进程替换执行进程new_proc,new_proc如果用./表示的当前相对路径执行,那么new_proc应该放在execvp源文件的同一目录,还是execvp的软链接所在目录?

1. 代码

(1)启动进程execvp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(void)
{
    int pid = 0;

    pid = fork();
    if (pid < 0)
    {
        printf("[%s,%d] fork failed, ret=%d\n", __FUNCTION__, __LINE__, pid);
    }
    else if (pid == 0)
    {
        char *const argv[] = {"./new_proc", NULL};
        if (-1 == execvp(argv[0], argv))
        {
            printf("[%s,%d] execvp failed!, error: %s (%d)\n", __FUNCTION__, __LINE__, strerror(errno), errno);
            exit(1);
        }
    }
	
	for (;;)
	{
		printf("pid = %d\n", pid);
		sleep(1);
	}

    return 0;	
}
(2)替换的新进程new_proc
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	for (;;)
	{
		printf("new_proc\n");
		sleep(1);
	}
	
	return 0;
}

2. 验证

(1)new_proc与execvp源文件同一目录
[exec]$ pwd
/test/exec
[exec]$ ls -l *
lrwxrwxrwx 1 test test 14 Aug 17 13:44 execvp -> package/execvp
package:
total 24
-rwxrwxr-x 1 test test 8808 Aug 17 13:49 execvp
-rwxrwxr-x 1 test test 8496 Aug 17 11:27 new_proc

[exec]$ ./execvp 
pid = 13487
[main,21] execvp failed!, error: No such file or directory (2)
pid = 13487
pid = 13487

[~]$ ps x | grep execvp
13486 pts/4    S+     0:00 ./execvp
13487 pts/4    Z+     0:00 [execvp] <defunct>
[~]$ cat /proc/13486/environ
...
PWD=/test/exec

执行会报错“No such file or directory”,查看/proc/pid/environ,其中包含了进程环境变量的列表,发现包含了软链接路径,但是没有源文件路径。

(2)new_proc与execvp软链接同一目录
[exec]$ ls -l *
lrwxrwxrwx 1 test test   14 Aug 17 13:44 execvp -> package/execvp
-rwxrwxr-x 1 test test 8496 Aug 17 11:27 new_proc
package:
total 12
-rwxrwxr-x 1 test test 8808 Aug 17 14:51 execvp

[exec]$ ./execvp 
pid = 25910
new_proc
pid = 25910
new_proc

可进行进程替换运行。

3. 总结

用软链接启动进程时,进程的环境变量列表会包含软链接的路径,但是没有源文件所在路径,可通过/proc/pid/environ查看。
所以进程替换需要考虑软链接路径,而不是源文件路径。

4. errno.h

使用strerror(errno)转换错误码为字符串的时候,需要加上头文件#include <string.h>,不然好像没有输出。

#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值