size of argument list can be larger than ARG_MAX?

size of argument list can be larger than ARG_MAX?

Post by Michael Wa » Tue, 21 Apr 1998 04:00:00



As I understand
The size of environment variable and the size of argument list, i.e.
strlen(envp[0]) + strlen(envp[1]) + .. + strlen(argv[0]) + strlen(argv[1]) + ..
can not be larger than ARG_MAX, which is 1048320 on Solaris.

By using env -i, none of environment variables are used, so
the size of argument list can not be larger than ARG_MAX,
which is 1048320 on Solaris.

However the following example can be compiled and run with
env -i ./a.out
And this shows that the size of argument list can be
1048355+6=1048461. What is the matter?

I run different tests, and all show that the size of argument list can be
larger than ARG_MAX, but how much larger is different.

Please help me to understand this issue, thanks.

#include <stdlib.h>
#include <string.h>

#ifndef _ARG_MAX
#define _ARG_MAX 1048355
#endif

int
main(int argc, char **argv, char **envp)
{
    char *arg = malloc((size_t) _ARG_MAX);
    memset(arg, ' ', _ARG_MAX);

    execlp("/bin/ls", arg, '\0');
    perror("exec");

Quote:}

 
 
 

1. max size of arguments can be >, =, < ARG_MAX, a bug?

I am writing an application that I need to know the maximum bytes of
arguments. This ain't hard, cause the man page for exec* says the
conbined maximum of arguments size and environment variable is ARG_MAX,
and ARG_MAX is defined in /usr/include/limits.h, and it is 1048320
for solaris.

By using env -i, I do not inherit the environment. So maximum bytes of
arguments = 1048320. But my tests show that the maximum can be greater
than, equal to, less than 1048320. The following crude code show that
in that particular example, the maximum is 1048415. What is the problem?

Thanks.

[julie:/opt/mwang/c]env -i ./maxargs 1048405
arg0:1048404
arg1:1
arg2:3
arg3:2
arg4:5
======
total:1048415 NOT 1048420!

[julie:/opt/mwang/c]env -i ./maxargs 1048406
exec: Arg list too long

maxargs.c:

#include <stdlib.h>

int
main(int argc, char **argv)
{
    size_t _arg_max = (size_t) atoi(*(argv+1));
    char *arg = malloc(_arg_max);
    memset(arg, ' ', _arg_max);
    arg[_arg_max-1]='\0';
    execlp("./numargs", arg, "1", "123", "21", "12345", '\0');
    perror("exec");

numargs.c:

int
main(int argc, char **argv) {
    int i, n;
    for (i=0; i<argc; i++) {
       n=strlen(*(argv+i));
       printf("arg%d:%d\n", i, n);
    }

2. Loss of character attributes when text scrolls off dtterm

3. Listing files not in argument list

4. I would like to dedicate this tune to BillG...

5. Restore mksysb on hard drive with larger PP size

6. Help! What could it be?

7. How to make /home larger than maximum partition size?

8. StarOffice 3.1

9. Look for files larger than a certain size

10. Larger Inital TCP Window Size

11. Window's larger than screen size in KDE/kwin?

12. restvg fails on 4.1 - PP size on larger disk ?

13. aix 5L: pp-size expands system when restoring to larger disks