首页 > 编程 > C++ > 正文

Linux下C语言修改进程名称的方法

2020-05-23 14:16:56
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了Linux下C语言修改进程名称的方法,涉及Linux下使用C语言操作进程的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Linux下C语言修改进程名称的方法。分享给大家供大家参考。具体如下:

 

 
  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include "./util/setproctitle.c" 
  4. // extern char **environ; 
  5. // int main(int argc , char *argv[]) 
  6. // { 
  7. // int i; 
  8. // printf("argc:%d/n" , argc); 
  9. // for (i = 0; i < argc; ++i){ 
  10. // printf("0x%x/n" , argv[i]); 
  11. // printf("argv[%d]:%s/n" , i , argv[i]); 
  12. // } 
  13. // printf("evriron=%x/n" , environ[0]); 
  14. // return 0; 
  15. // } 
  16. int main(int argc, char **argv){ 
  17. spt_init(argc,argv); 
  18. setproctitle("设置进程名为:this is a test"); 
  19. sleep(1000); 
  20. return 0; 
  21. } 

setproctitle.c文件如下:

 

 
  1. /* ========================================================================== 
  2. * setproctitle.c - Linux/Darwin setproctitle. 
  3. * -------------------------------------------------------------------------- 
  4. * Copyright (C) 2010 William Ahern 
  5. * Copyright (C) 2013 Salvatore Sanfilippo 
  6. * Copyright (C) 2013 Stam He 
  7. * 
  8. * Permission is hereby granted, free of charge, to any person obtaining a 
  9. * copy of this software and associated documentation files (the 
  10. * "Software"), to deal in the Software without restriction, including 
  11. * without limitation the rights to use, copy, modify, merge, publish, 
  12. * distribute, sublicense, and/or sell copies of the Software, and to permit 
  13. * persons to whom the Software is furnished to do so, subject to the 
  14. * following conditions: 
  15. * 
  16. * The above copyright notice and this permission notice shall be included 
  17. * in all copies or substantial portions of the Software. 
  18. * 
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
  22. * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE. 
  26. * ========================================================================== 
  27. */ 
  28. #ifndef _GNU_SOURCE 
  29. #define _GNU_SOURCE 
  30. #endif 
  31.  
  32. #include <stddef.h> /* NULL size_t */ 
  33. #include <stdarg.h> /* va_list va_start va_end */ 
  34. #include <stdlib.h> /* malloc(3) setenv(3) clearenv(3) setproctitle(3) getprogname(3) */ 
  35. #include <stdio.h> /* vsnprintf(3) snprintf(3) */ 
  36. #include <string.h> /* strlen(3) strchr(3) strdup(3) memset(3) memcpy(3) */ 
  37. #include <errno.h> /* errno program_invocation_name program_invocation_short_name */ 
  38. #if !defined(HAVE_SETPROCTITLE) 
  39. #define HAVE_SETPROCTITLE (defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__) 
  40. #endif 
  41. #if !HAVE_SETPROCTITLE 
  42. #if (defined __linux || defined __APPLE__) 
  43. extern char **environ; 
  44. static struct { 
  45. /* original value */ 
  46. const char *arg0; 
  47. /* title space available */ 
  48. char *base, *end; 
  49. /* pointer to original nul character within base */ 
  50. char *nul; 
  51. _Bool reset; 
  52. int error; 
  53. } SPT; 
  54. #ifndef SPT_MIN 
  55. #define SPT_MIN(a, b) (((a) < (b))? (a) : (b)) 
  56. #endif 
  57. static inline size_t spt_min(size_t a, size_t b); 
  58. static int spt_clearenv(void); 
  59. static int spt_copyenv(char *oldenv[]); 
  60. static int spt_copyargs(int argc, char *argv[]) ; 
  61. void spt_init(int argc, char *argv[]); 
  62. void setproctitle(const char *fmt, ...); 
  63. static inline size_t spt_min(size_t a, size_t b) { 
  64. return SPT_MIN(a, b); 
  65. } /* spt_min() */ 
  66. /* 
  67. * For discussion on the portability of the various methods, see 
  68. * http://lists.freebsd.org/pipermail/freebsd-stable/2008-June/043136.html 
  69. */ 
  70. static int spt_clearenv(void) { 
  71. #if __GLIBC__ 
  72. clearenv(); 
  73. return 0; 
  74. #else 
  75. extern char **environ; 
  76. static char **tmp; 
  77. if (!(tmp = malloc(sizeof *tmp))) 
  78. return errno; 
  79. tmp[0] = NULL; 
  80. environ = tmp; 
  81. return 0; 
  82. #endif 
  83. } /* spt_clearenv() */ 
  84. static int spt_copyenv(char *oldenv[]) { 
  85. extern char **environ; 
  86. char *eq; 
  87. int i, error; 
  88. if (environ != oldenv) 
  89. return 0; 
  90. if ((error = spt_clearenv())) 
  91. goto error; 
  92. for (i = 0; oldenv[i]; i++) { 
  93. if (!(eq = strchr(oldenv[i], '='))) 
  94. continue; 
  95. *eq = '/0'; 
  96. error = (0 != setenv(oldenv[i], eq + 1, 1))? errno : 0; 
  97. *eq = '='; 
  98. if (error) 
  99. goto error; 
  100. } 
  101. return 0; 
  102. error: 
  103. environ = oldenv; 
  104. return error; 
  105. } /* spt_copyenv() */ 
  106. static int spt_copyargs(int argc, char *argv[]) { 
  107. char *tmp; 
  108. int i; 
  109. for (i = 1; i < argc || (i >= argc && argv[i]); i++) { 
  110. if (!argv[i]) 
  111. continue; 
  112. if (!(tmp = strdup(argv[i]))) 
  113. return errno; 
  114. argv[i] = tmp; 
  115. } 
  116. return 0; 
  117. } /* spt_copyargs() */ 
  118. void spt_init(int argc, char *argv[]) { 
  119. char **envp = environ; 
  120. char *base, *end, *nul, *tmp; 
  121. int i, error; 
  122. if (!(base = argv[0])) 
  123. return; 
  124. nul = &base[strlen(base)]; 
  125. end = nul + 1; 
  126. for (i = 0; i < argc || (i >= argc && argv[i]); i++) { 
  127. if (!argv[i] || argv[i] < end) 
  128. continue; 
  129. end = argv[i] + strlen(argv[i]) + 1; 
  130. } 
  131. for (i = 0; envp[i]; i++) { 
  132. if (envp[i] < end) 
  133. continue; 
  134. end = envp[i] + strlen(envp[i]) + 1; 
  135. } 
  136. if (!(SPT.arg0 = strdup(argv[0]))) 
  137. goto syerr; 
  138. if ((error = spt_copyenv(envp))) 
  139. goto error; 
  140. if ((error = spt_copyargs(argc, argv))) 
  141. goto error; 
  142. SPT.nul = nul; 
  143. SPT.base = base; 
  144. SPT.end = end; 
  145. return; 
  146. syerr: 
  147. error = errno; 
  148. error: 
  149. SPT.error = error; 
  150. } /* spt_init() */ 
  151. #ifndef SPT_MAXTITLE 
  152. #define SPT_MAXTITLE 255 
  153. #endif 
  154. void setproctitle(const char *fmt, ...) { 
  155. char buf[SPT_MAXTITLE + 1]; /* use buffer in case argv[0] is passed */ 
  156. va_list ap; 
  157. char *nul; 
  158. int len, error; 
  159. if (!SPT.base) 
  160. return; 
  161. if (fmt) { 
  162. va_start(ap, fmt); 
  163. len = vsnprintf(buf, sizeof buf, fmt, ap); 
  164. va_end(ap); 
  165. } else { 
  166. len = snprintf(buf, sizeof buf, "%s", SPT.arg0); 
  167. } 
  168. if (len <= 0) 
  169. { error = errno; goto error; } 
  170. if (!SPT.reset) { 
  171. memset(SPT.base, 0, SPT.end - SPT.base); 
  172. SPT.reset = 1; 
  173. } else { 
  174. memset(SPT.base, 0, spt_min(sizeof buf, SPT.end - SPT.base)); 
  175. } 
  176. len = spt_min(len, spt_min(sizeof buf, SPT.end - SPT.base) - 1); 
  177. memcpy(SPT.base, buf, len); 
  178. nul = &SPT.base[len]; 
  179. if (nul < SPT.nul) { 
  180. *SPT.nul = '.'; 
  181. } else if (nul == SPT.nul && &nul[1] < SPT.end) { 
  182. *SPT.nul = ' '; 
  183. *++nul = '/0'; 
  184. } 
  185. return; 
  186. error: 
  187. SPT.error = error; 
  188. } /* setproctitle() */ 
  189. #endif /* __linux || __APPLE__ */ 
  190. #endif /* !HAVE_SETPROCTITLE */ 

希望本文所述对大家的C语言程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表