What is the maximum allowed length for a process name?

What is the maximum length allowed for a process name? I am reading the process name from the file /proc/[pid]/stat , and I would like to know the maximum buffer that I need.

I'm sure there is a limit that is configurable, but just can't find out where it is.

+7
linux unix process procfs
source share
1 answer

According to man 2 prctl :

PR_SET_NAME (since Linux 2.6.9)

Name the calling thread using the value at the location pointed to by (char *) arg2. A name can be up to 16 bytes long and must be terminated with a null value if it contains fewer bytes.

So, I would choose a long buffer of 16 bytes .


EDIT:

Let me do this some more.

Each process on Linux corresponds to a struct task_struct in the kernel, which is defined in include/linux/sched.h .

There is a char comm[TASK_COMM_LEN] field in this definition, which, according to the comment, refers to the name of the executable file, excluding the path:

  char comm[TASK_COMM_LEN]; /* executable name excluding path - access with [gs]et_task_comm (which lock it with task_lock()) - initialized normally by setup_new_exec */ 

Its size, TASK_COMM_LEN , is defined above in the same header file, here , 16 bytes :

 /* Task command name length */ #define TASK_COMM_LEN 16 

Also, citing LDD3 p. 22:

...

The following statement prints the process identifier and the command name of the current process by accessing specific fields in the struct task_struct :

 printk(KERN_INFO "The process is \"%s\" (pid %i)\n", current->comm, current->pid); 

The command name stored in current->comm is the base name of the program file ( truncated to 15 characters, if necessary ), which is executed by the current process.

+9
source share

All Articles