©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c5/limit.c

Go to the documentation of this file.
00001 /*
00002     getrlimit and setrlimit example
00003     AUP2, Sec. 5.16
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include "defs.h"
00022 #include <sys/resource.h>
00023 /*[pgm]*/
00024 static void showvalue(rlim_t lim)
00025 {
00026     /*
00027         All macros may equal RLIM_INFINITY; that test
00028         must be first; can't use switch statement.
00029     */
00030     if (lim == RLIM_INFINITY)
00031         printf("RLIM_INFINITY");
00032 #ifndef BSD_DERIVED
00033     else if (lim == RLIM_SAVED_CUR)
00034         printf("RLIM_SAVED_CUR");
00035     else if (lim == RLIM_SAVED_MAX)
00036         printf("RLIM_SAVED_MAX");
00037 #endif
00038     else
00039         printf("%llu", (unsigned long long)lim);
00040 }
00041 
00042 static bool showlimit(int resource, const char *name)
00043 {
00044     struct rlimit r;
00045 
00046     ec_neg1( getrlimit(resource, &r) )
00047     printf("%s: ", name);
00048     printf("rlim_cur = ");
00049     showvalue(r.rlim_cur);
00050     printf("; rlim_max = ");
00051     showvalue(r.rlim_max);
00052     printf("\n");
00053     return true;
00054 
00055 EC_CLEANUP_BGN
00056     return false;
00057 EC_CLEANUP_END
00058 }
00059 
00060 int main(void)
00061 {
00062     struct rlimit r;
00063     int fd;
00064     char buf[500] = { 0 };
00065 
00066     if (sizeof(rlim_t) > sizeof(long long))
00067         printf("Warning: rlim_t > long long; results may be wrong\n");
00068     ec_false( showlimit(RLIMIT_CORE, "RLIMIT_CORE") )
00069     ec_false( showlimit(RLIMIT_CPU, "RLIMIT_CPU") )
00070     ec_false( showlimit(RLIMIT_DATA, "RLIMIT_DATA") )
00071     ec_false( showlimit(RLIMIT_FSIZE, "RLIMIT_FSIZE") )
00072     ec_false( showlimit(RLIMIT_NOFILE, "RLIMIT_NOFILE") )
00073     ec_false( showlimit(RLIMIT_STACK, "RLIMIT_STACK") )
00074 #ifndef BSD_DERIVED
00075     ec_false( showlimit(RLIMIT_AS, "RLIMIT_AS") )
00076 #endif
00077     ec_neg1( getrlimit(RLIMIT_FSIZE, &r) )
00078     r.rlim_cur = 500;
00079     ec_neg1( setrlimit(RLIMIT_FSIZE, &r) )
00080     ec_neg1( fd = open("tmp", O_WRONLY | O_CREAT | O_TRUNC, PERM_FILE) )
00081     ec_neg1( write(fd, buf, sizeof(buf)) )
00082     ec_neg1( write(fd, buf, sizeof(buf)) )
00083     printf("Wrote two buffers! (?)\n");
00084     exit(EXIT_SUCCESS);
00085 
00086 EC_CLEANUP_BGN
00087     exit(EXIT_FAILURE);
00088 EC_CLEANUP_END
00089 }
00090 /*[]*/

Generated on Fri Apr 23 10:56:56 2004 for AUP2 Example Source by doxygen 1.3.1