Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3795318
  • 博文数量: 93
  • 博客积分: 3189
  • 博客等级: 中校
  • 技术积分: 4231
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-02 13:29
个人简介

出没于杭州和青岛的程序猿一枚,对内核略懂一二

文章分类

全部博文(93)

文章存档

2016年(2)

2015年(3)

2014年(11)

2013年(29)

2012年(16)

2011年(5)

2010年(5)

2009年(22)

分类: LINUX

2013-09-29 16:53:56

欢迎转载,转载请注明出处http://forever.blog.chinaunix.net

Author: Tony


现场程序出现bug,由于时间紧急需要将进程重启,为了保留调试信息一般会让进程coredump。而如果这个时候你发现进程启动时候的limits没有合理设置,因为core文件大小的限制无法coredump,怎么办?

现场程序因为启动时没有合理设置open files大小,导致无法继续接受请求,而有不能重启网元,怎么办?


其实从内核2.6.36开始,已经增加了系统调用prlimit设置正在运行进程的limits,但是由于SLES SP2的util linux版本太低,并不能直接使用,于是开发了一个小工具processlimit。
使用方法跟ulimit相似。

processlimit使用实例:

       修改进程A的core file size为无限制: processlimit -p `pidof A` -c unlimited
       修改进程B的open files为4096: processlimit -p `pidof B` -n 4096


processlimit代码:

点击(此处)折叠或打开

  1. /*******************************************************************************
  2.   This program is used to change a runing process

  3.   such as:
  4.     There is a bug on program A which is runing, but it started with core
  5.     size 1K. How to coredump it?

  6.     Yeah, processlimit can do
  7.         #processlimit -p `pidof A` -c unlimited
  8.         #kill -6 `pidof A`
  9.     
  10.   
  11.   Copyright(c) 2008-2013

  12.   This program is free software; you can redistribute it and/or modify it
  13.   under the terms and conditions of the GNU General Public License,
  14.   version 2, as published by the Free Software Foundation.

  15.   This program is distributed in the hope it will be useful, but WITHOUT
  16.   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  17.   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  18.   more details.

  19.   You should have received a copy of the GNU General Public License along with
  20.   this program; if not, write to the Free Software Foundation, Inc.,
  21.   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.

  22.   The full GNU General Public License is included in this distribution in
  23.   the file called "COPYING".

  24.   Version: 0.1

  25.   Date: 2013-09-27 14:02:47 CST

  26.   Contact Information:
  27.   Tony <tingw.liu@gmail.com>
  28.   Jiangxi Road 11, Qingdao, China.
  29. *******************************************************************************/




  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <sys/time.h>
  34. #include <sys/resource.h>
  35. #include <stdlib.h>
  36. #include <limits.h>
  37. #include <getopt.h>


  38. #define SYS_PRLIMIT 302

  39. char option;
  40. const char *options = "hc:d:f:p:n:s:";
  41. const struct option long_options[]={
  42.     {"help", 0, NULL, 'h'},
  43.     {"pid", 1, NULL, 'p'},
  44.     {"core size", 1, NULL, 'c'},
  45.     {"data seg size", 1, NULL, 'd'},
  46.     {"file size", 1, NULL, 'f'},
  47.     {"open files", 1, NULL, 'n'},
  48.     {"stack size", 1, NULL, 's'},
  49.     {NULL, 0, NULL, 0}
  50. };

  51. void help()
  52. {
  53.     printf("Usage: processlimit -p pid [-cdfns] [limit]\n");
  54.     printf("\nIt is similar to ulimit which for running process,"
  55.             "you can get param information from 'unlimit -a'!\n");
  56.     printf("Caution: All value unit is 1 (blocks, bytes, files...)\n");
  57.     printf("\n\nAny question to Tony \n");
  58.     exit(-1);
  59. }

  60. int main(int argc, char **argv)
  61. {
  62.     unsigned int pid = 0;
  63.     struct rlimit newlimit;
  64.     long int value;
  65.     char *softlimit;
  66.     int ret, type = 0;
  67.     int done = 0;
  68.     opterr = 0;
  69.     while ((option = getopt_long(argc, argv, options, long_options, NULL)) != -1)
  70.     {
  71.         switch (option)
  72.         {
  73.             case 'h':
  74.                 help();
  75.                 break;
  76.             case 'p':
  77.                 pid = atoi(optarg);
  78.                 break;
  79.             case 'c':
  80.                 softlimit = strdup(optarg);
  81.                 type = RLIMIT_CORE;
  82.                 ++done;
  83.                 break;
  84.             case 'd':
  85.                 softlimit = strdup(optarg);
  86.                 type = RLIMIT_DATA;
  87.                 ++done;
  88.                 break;
  89.             case 'f':
  90.                 softlimit = strdup(optarg);
  91.                 type = RLIMIT_FSIZE;
  92.                 ++done;
  93.                 break;
  94.             case 'n':
  95.                 softlimit = strdup(optarg);
  96.                 type = RLIMIT_NOFILE;
  97.                 ++done;
  98.                 break;
  99.             case 's':
  100.                 softlimit = strdup(optarg);
  101.                 type = RLIMIT_STACK;
  102.                 ++done;
  103.                 break;
  104.             default:
  105.                 help();
  106.         }
  107.     }
  108.     if (pid == 0 || done != 1)
  109.         help();

  110.     if (strncmp(softlimit, "unlimited", strlen(softlimit)) == 0) {
  111.         newlimit.rlim_cur = RLIM_INFINITY;
  112.         newlimit.rlim_max = RLIM_INFINITY;
  113.     } else {
  114.         value = strtol(softlimit, NULL, 10);
  115.         if (value == LONG_MIN || value == LONG_MAX) {
  116.             printf("Can't convert softlimit to long int\n");
  117.             return -1;
  118.         }
  119.         newlimit.rlim_cur = value;
  120.         newlimit.rlim_max = value;
  121.     }
  122.     free(softlimit);    
  123.     ret = syscall(SYS_PRLIMIT, pid, type, &newlimit, NULL);
  124.     if (ret) {
  125.         printf("Can't change softlimt and hardlimit!\n");
  126.         return -1;
  127.     }
  128. }



阅读(7255) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~