首页 > 编程 > 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; 

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. return 0; 
  101. error: 
  102. environ = oldenv; 
  103. return error; 
  104. /* spt_copyenv() */ 
  105. static int spt_copyargs(int argc, char *argv[]) { 
  106. char *tmp; 
  107. int i; 
  108. for (i = 1; i < argc || (i >= argc && argv[i]); i++) { 
  109. if (!argv[i]) 
  110. continue
  111. if (!(tmp = strdup(argv[i]))) 
  112. return errno; 
  113. argv[i] = tmp; 
  114. return 0; 
  115. /* spt_copyargs() */ 
  116. void spt_init(int argc, char *argv[]) { 
  117. char **envp = environ; 
  118. char *base, *end, *nul, *tmp; 
  119. int i, error; 
  120. if (!(base = argv[0])) 
  121. return
  122. nul = &base[strlen(base)]; 
  123. end = nul + 1; 
  124. for (i = 0; i < argc || (i >= argc && argv[i]); i++) { 
  125. if (!argv[i] || argv[i] < end) 
  126. continue
  127. end = argv[i] + strlen(argv[i]) + 1; 
  128. for (i = 0; envp[i]; i++) { 
  129. if (envp[i] < end) 
  130. continue
  131. end = envp[i] + strlen(envp[i]) + 1; 
  132. if (!(SPT.arg0 = strdup(argv[0]))) 
  133. goto syerr; 
  134. if ((error = spt_copyenv(envp))) 
  135. goto error; 
  136. if ((error = spt_copyargs(argc, argv))) 
  137. goto error; 
  138. SPT.nul = nul; 
  139. SPT.base = base; 
  140. SPT.end = end; 
  141. return
  142. syerr: 
  143. error = errno; 
  144. error: 
  145. SPT.error = error; 
  146. /* spt_init() */ 
  147. #ifndef SPT_MAXTITLE 
  148. #define SPT_MAXTITLE 255 
  149. #endif 
  150. void setproctitle(const char *fmt, ...) { 
  151. char buf[SPT_MAXTITLE + 1]; /* use buffer in case argv[0] is passed */ 
  152. va_list ap; 
  153. char *nul; 
  154. int len, error; 
  155. if (!SPT.base) 
  156. return
  157. if (fmt) { 
  158. va_start(ap, fmt); 
  159. len = vsnprintf(buf, sizeof buf, fmt, ap); 
  160. va_end(ap); 
  161. else { 
  162. len = snprintf(buf, sizeof buf, "%s", SPT.arg0); 
  163. if (len <= 0) 
  164. { error = errno; goto error; } 
  165. if (!SPT.reset) { 
  166. memset(SPT.base, 0, SPT.end - SPT.base); 
  167. SPT.reset = 1; 
  168. else { 
  169. memset(SPT.base, 0, spt_min(sizeof buf, SPT.end - SPT.base)); 
  170. len = spt_min(len, spt_min(sizeof buf, SPT.end - SPT.base) - 1); 
  171. memcpy(SPT.base, buf, len); 
  172. nul = &SPT.base[len]; 
  173. if (nul < SPT.nul) { 
  174. *SPT.nul = '.'
  175. else if (nul == SPT.nul && &nul[1] < SPT.end) { 
  176. *SPT.nul = ' '
  177. *++nul = '/0'
  178. return
  179. error: 
  180. SPT.error = error; 
  181. /* setproctitle() */ 
  182. #endif /* __linux || __APPLE__ */ 
  183. #endif /* !HAVE_SETPROCTITLE */ 

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

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