当前位置: 首页 > news >正文

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

目录

      • 1. 代码
        • (1)启动进程execvp
        • (2)替换的新进程new_proc
      • 2. 验证
        • (1)new_proc与execvp源文件同一目录
        • (2)new_proc与execvp软链接同一目录
      • 3. 总结
      • 4. errno.h

用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 */
http://www.lryc.cn/news/128348.html

相关文章:

  • 【Go】锁相关
  • git环境超详细配置说明
  • 使用阿里云服务器搭建PostgreSQL主从架构图文流程
  • Linux的基本权限(文件,目录)
  • 网络编程(12): TCP重传、滑动窗口、流量控制、拥塞控制
  • Docker安装RabbitMQ服务端
  • vueuse常用方法
  • 选择大型语言模型自定义技术
  • 算法概述-Java常用算法
  • CCLINK转MODBUS-TCP网关cclink通讯接线图 终端电阻
  • 香蕉派 BPI-P2 Pro采用RK3308芯片,512M内存,8G存储,支持PoE供电
  • 「隐语小课」拆分学习之“水平拆分学习”
  • WPF--关于Action事件小结
  • 创建一个 React+Typescript 项目
  • Java课题笔记~ 数据提交的方式
  • VUE3给页面添加按钮事件
  • 基于centos7.9通过nginx实现负载均衡以及反向代理
  • 前端原生写自定义旋转变换轮播图
  • linux tomcat server.xml 项目访问路径变更不生效
  • 介绍原型模式:快速构建和复制对象的设计模式
  • Unity的TimeScale的影响范围分析
  • 爬虫逆向实战(五)--猿人学第三题
  • [虚幻引擎] UE使用虚拟纹理在模型上显示挖空效果
  • vue3中reactive和ref的比较
  • Beats:使用 Filebeat 将 golang 应用程序记录到 Elasticsearch - 8.x
  • 【STM32+ESP8266上云连载①】给ESP8266烧录AT固件
  • 深入解析Spring基本概念和核心思想
  • Redis数据结构——快速列表quicklist、快表
  • excel统计函数篇3之rank系列
  • Flink 火焰图