mistake in example in getrlimit(2)

Asked by Reverend Homer

here is example from getrlimit(2):

#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>

#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)

int
main(int argc, char *argv[])
{
    struct rlimit old, new;
    struct rlimit *newp;
    pid_t pid;

    if (!(argc == 2 || argc == 4)) {
 fprintf(stderr, "Usage: %s <pid> [<new-soft-limit> "
  "<new-hard-limit>]\n", argv[0]);
 exit(EXIT_FAILURE);
    }

    pid = atoi(argv[1]); /* PID of target process */

    newp = NULL;
    if (argc == 4) {
 new.rlim_cur = atoi(argv[2]);
 new.rlim_max = atoi(argv[3]);
 newp = &new;
    }

    /* Set CPU time limit of target process; retrieve and display
       previous limit */

    if (prlimit(pid, RLIMIT_CPU, newp, &old) == -1)
 errExit("prlimit-1");
    printf("Previous limits: soft=%lld; hard=%lld\n",
     (long long) old.rlim_cur, (long long) old.rlim_max);

    /* Retrieve and display new CPU time limit */

    if (prlimit(pid, RLIMIT_CPU, NULL, &old) == -1)
 errExit("prlimit-2");
    printf("New limits: soft=%lld; hard=%lld\n",
     (long long) old.rlim_cur, (long long) old.rlim_max);

    exit(EXIT_FAILURE);
}

As you can see, this program will always exit with EXIT_FAILURE status. It will be clearer and more correct to replace last line with "exit(EXIT_SUCCESS);"

ProblemType: Bug
DistroRelease: Ubuntu 16.04
Package: manpages-dev 4.04-2
ProcVersionSignature: Ubuntu 4.4.0-47.68-generic 4.4.24
Uname: Linux 4.4.0-47-generic x86_64
NonfreeKernelModules: kpatch_livepatch_Ubuntu_4_4_0_47_68_generic_15 kpatch_livepatch_Ubuntu_4_4_0_47_68_generic_14
ApportVersion: 2.20.1-0ubuntu2.4
Architecture: amd64
Date: Mon Dec 26 20:06:31 2016
Dependencies: manpages 4.04-2
InstallationDate: Installed on 2016-09-29 (87 days ago)
InstallationMedia: Ubuntu 16.04.1 LTS "Xenial Xerus" - Release amd64 (20160719)
PackageArchitecture: all
SourcePackage: manpages
UpgradeStatus: No upgrade log present (probably fresh install)

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu manpages Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was originally filed as bug #1652652.

Revision history for this message
Reverend Homer (mk-43-ecko) said :
#1

Actually I don't know if it is a bug or architecture specific thing, so convert this to the question

Revision history for this message
Manfred Hampl (m-hampl) said :
#2

The original source of that example seems to be kernel.org, see e.g. http://man7.org/linux/man-pages/man2/getrlimit.2.html

I suggest you report a bug there, see https://www.kernel.org/doc/man-pages/reporting_bugs.html

Revision history for this message
Michael Kerrisk (mtk-manpages) said :
#3

Upstream maintainer here. The initial reportis correct. This should read

exit(EXIT_SUCCESS);

Fixed for upcoming upstream release 4.10.

Can you help with this problem?

Provide an answer of your own, or ask Reverend Homer for more information if necessary.

To post a message you must log in.