1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
|
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% RRRR EEEEE SSSSS OOO U U RRRR CCCC EEEEE %
% R R E SS O O U U R R C E %
% RRRR EEE SSS O O U U RRRR C EEE %
% R R E SS O O U U R R C E %
% R R EEEEE SSSSS OOO UUU R R CCCC EEEEE %
% %
% %
% Get/Set MagickCore Resources %
% %
% Software Design %
% John Cristy %
% September 2002 %
% %
% %
% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% obtain a copy of the License at %
% %
% http://www.imagemagick.org/script/license.php %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
% See the License for the specific language governing permissions and %
% limitations under the License. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
/*
Include declarations.
*/
#include "magick/studio.h"
#include "magick/cache.h"
#include "magick/configure.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/hashmap.h"
#include "magick/log.h"
#include "magick/image.h"
#include "magick/memory_.h"
#include "magick/option.h"
#include "magick/policy.h"
#include "magick/random_.h"
#include "magick/registry.h"
#include "magick/resource_.h"
#include "magick/semaphore.h"
#include "magick/signature-private.h"
#include "magick/string_.h"
#include "magick/string-private.h"
#include "magick/splay-tree.h"
#include "magick/thread-private.h"
#include "magick/token.h"
#include "magick/utility.h"
/*
Typedef declarations.
*/
typedef struct _ResourceInfo
{
MagickOffsetType
area,
memory,
map,
disk,
file,
thread,
time;
MagickSizeType
area_limit,
memory_limit,
map_limit,
disk_limit,
file_limit,
thread_limit,
time_limit;
} ResourceInfo;
/*
Global declarations.
*/
static RandomInfo
*random_info = (RandomInfo *) NULL;
static ResourceInfo
resource_info =
{
MagickULLConstant(0),
MagickULLConstant(0),
MagickULLConstant(0),
MagickULLConstant(0),
MagickULLConstant(0),
MagickULLConstant(0),
MagickULLConstant(0),
MagickULLConstant(2048)*1024*1024,
MagickULLConstant(1536)*1024*1024,
MagickULLConstant(8192)*1024*1024,
MagickResourceInfinity,
MagickULLConstant(768),
MagickULLConstant(8),
MagickResourceInfinity
};
static SemaphoreInfo
*resource_semaphore = (SemaphoreInfo *) NULL;
static SplayTreeInfo
*temporary_resources = (SplayTreeInfo *) NULL;
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% A c q u i r e M a g i c k R e s o u r c e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% AcquireMagickResource() acquires resources of the specified type.
% MagickFalse is returned if the specified resource is exhausted otherwise
% MagickTrue.
%
% The format of the AcquireMagickResource() method is:
%
% MagickBooleanType AcquireMagickResource(const ResourceType type,
% const MagickSizeType size)
%
% A description of each parameter follows:
%
% o type: the type of resource.
%
% o size: the number of bytes needed from for this resource.
%
*/
MagickExport MagickBooleanType AcquireMagickResource(const ResourceType type,
const MagickSizeType size)
{
char
resource_current[MaxTextExtent],
resource_limit[MaxTextExtent],
resource_request[MaxTextExtent];
MagickBooleanType
status;
MagickSizeType
limit;
status=MagickFalse;
(void) FormatMagickSize(size,MagickFalse,resource_request);
if (resource_semaphore == (SemaphoreInfo *) NULL)
AcquireSemaphoreInfo(&resource_semaphore);
LockSemaphoreInfo(resource_semaphore);
switch (type)
{
case AreaResource:
{
resource_info.area=(MagickOffsetType) size;
limit=resource_info.area_limit;
status=(resource_info.area_limit == MagickResourceInfinity) ||
(size < limit) ? MagickTrue : MagickFalse;
(void) FormatMagickSize((MagickSizeType) resource_info.area,MagickFalse,
resource_current);
(void) FormatMagickSize(resource_info.area_limit,MagickFalse,
resource_limit);
break;
}
case MemoryResource:
{
resource_info.memory+=size;
limit=resource_info.memory_limit;
status=(resource_info.memory_limit == MagickResourceInfinity) ||
((MagickSizeType) resource_info.memory < limit) ?
MagickTrue : MagickFalse;
(void) FormatMagickSize((MagickSizeType) resource_info.memory,MagickTrue,
resource_current);
(void) FormatMagickSize(resource_info.memory_limit,MagickTrue,
resource_limit);
break;
}
case MapResource:
{
resource_info.map+=size;
limit=resource_info.map_limit;
status=(resource_info.map_limit == MagickResourceInfinity) ||
((MagickSizeType) resource_info.map < limit) ?
MagickTrue : MagickFalse;
(void) FormatMagickSize((MagickSizeType) resource_info.map,MagickTrue,
resource_current);
(void) FormatMagickSize(resource_info.map_limit,MagickTrue,
resource_limit);
break;
}
case DiskResource:
{
resource_info.disk+=size;
limit=resource_info.disk_limit;
status=(resource_info.disk_limit == MagickResourceInfinity) ||
((MagickSizeType) resource_info.disk < limit) ?
MagickTrue : MagickFalse;
(void) FormatMagickSize((MagickSizeType) resource_info.disk,MagickFalse,
resource_current);
(void) FormatMagickSize(resource_info.disk_limit,MagickFalse,
resource_limit);
break;
}
case FileResource:
{
resource_info.file+=size;
limit=resource_info.file_limit;
status=(resource_info.file_limit == MagickResourceInfinity) ||
((MagickSizeType) resource_info.file < limit) ?
MagickTrue : MagickFalse;
(void) FormatMagickSize((MagickSizeType) resource_info.file,MagickFalse,
resource_current);
(void) FormatMagickSize((MagickSizeType) resource_info.file_limit,
MagickFalse,resource_limit);
break;
}
case ThreadResource:
{
resource_info.thread+=size;
limit=resource_info.thread_limit;
status=(resource_info.thread_limit == MagickResourceInfinity) ||
((MagickSizeType) resource_info.thread < limit) ?
MagickTrue : MagickFalse;
(void) FormatMagickSize((MagickSizeType) resource_info.thread,MagickFalse,
resource_current);
(void) FormatMagickSize((MagickSizeType) resource_info.thread_limit,
MagickFalse,resource_limit);
break;
}
case TimeResource:
{
resource_info.time+=size;
limit=resource_info.time_limit;
status=(resource_info.time_limit == MagickResourceInfinity) ||
((MagickSizeType) resource_info.time < limit) ?
MagickTrue : MagickFalse;
(void) FormatMagickSize((MagickSizeType) resource_info.time,MagickFalse,
resource_current);
(void) FormatMagickSize((MagickSizeType) resource_info.time_limit,
MagickFalse,resource_limit);
break;
}
default:
break;
}
UnlockSemaphoreInfo(resource_semaphore);
(void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s: %s/%s/%s",
MagickOptionToMnemonic(MagickResourceOptions,(long) type),resource_request,
resource_current,resource_limit);
return(status);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ A s y n c h r o n o u s R e s o u r c e C o m p o n e n t T e r m i n u s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% AsynchronousResourceComponentTerminus() destroys the resource environment.
% It differs from ResourceComponentTerminus() in that it can be called from a
% asynchronous signal handler.
%
% The format of the ResourceComponentTerminus() method is:
%
% ResourceComponentTerminus(void)
%
*/
MagickExport void AsynchronousResourceComponentTerminus(void)
{
const char
*path;
if (temporary_resources == (SplayTreeInfo *) NULL)
return;
/*
Remove any lingering temporary files.
*/
ResetSplayTreeIterator(temporary_resources);
path=(const char *) GetNextKeyInSplayTree(temporary_resources);
while (path != (const char *) NULL)
{
(void) remove(path);
path=(const char *) GetNextKeyInSplayTree(temporary_resources);
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% A c q u i r e U n i q u e F i l e R e s o u r c e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% AcquireUniqueFileResource() returns a unique file name, and returns a file
% descriptor for the file open for reading and writing.
%
% The format of the AcquireUniqueFileResource() method is:
%
% int AcquireUniqueFileResource(char *path)
%
% A description of each parameter follows:
%
% o path: Specifies a pointer to an array of characters. The unique path
% name is returned in this array.
%
*/
static void *DestroyTemporaryResources(void *temporary_resource)
{
(void) remove((char *) temporary_resource);
return((void *) NULL);
}
static MagickBooleanType GetPathTemplate(char *path)
{
char
*directory;
ExceptionInfo
*exception;
MagickBooleanType
status;
register char
*p;
struct stat
attributes;
(void) CopyMagickString(path,"magick-XXXXXXXX",MaxTextExtent);
exception=AcquireExceptionInfo();
directory=(char *) GetImageRegistry(StringRegistryType,"temporary-path",
exception);
exception=DestroyExceptionInfo(exception);
if (directory == (char *) NULL)
directory=GetEnvironmentValue("MAGICK_TEMPORARY_PATH");
if (directory == (char *) NULL)
directory=GetEnvironmentValue("MAGICK_TMPDIR");
if (directory == (char *) NULL)
directory=GetPolicyValue("temporary-path");
if (directory == (char *) NULL)
directory=GetEnvironmentValue("TMPDIR");
#if defined(__WINDOWS__) || defined(__OS2__)
if (directory == (char *) NULL)
directory=GetEnvironmentValue("TMP");
if (directory == (char *) NULL)
directory=GetEnvironmentValue("TEMP");
#endif
#if defined(__VMS)
if (directory == (char *) NULL)
directory=GetEnvironmentValue("MTMPDIR");
#endif
#if defined(P_tmpdir)
if (directory == (char *) NULL)
directory=ConstantString(P_tmpdir);
#endif
if (directory == (char *) NULL)
return(MagickTrue);
if (strlen(directory) > (MaxTextExtent-15))
{
directory=DestroyString(directory);
return(MagickTrue);
}
status=GetPathAttributes(directory,&attributes);
if ((status == MagickFalse) || !S_ISDIR(attributes.st_mode))
{
directory=DestroyString(directory);
return(MagickTrue);
}
if (directory[strlen(directory)-1] == *DirectorySeparator)
(void) FormatMagickString(path,MaxTextExtent,"%smagick-XXXXXXXX",directory);
else
(void) FormatMagickString(path,MaxTextExtent,"%s%smagick-XXXXXXXX",
directory,DirectorySeparator);
directory=DestroyString(directory);
if (*DirectorySeparator != '/')
for (p=path; *p != '\0'; p++)
if (*p == *DirectorySeparator)
*p='/';
return(MagickTrue);
}
MagickExport int AcquireUniqueFileResource(char *path)
{
#if !defined(O_NOFOLLOW)
#define O_NOFOLLOW 0
#endif
#if !defined(TMP_MAX)
# define TMP_MAX 238328
#endif
char
*resource;
int
c,
file;
register char
*p;
register long
i;
static const char
portable_filename[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
StringInfo
*key;
unsigned char
*datum;
assert(path != (char *) NULL);
(void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
if (random_info == (RandomInfo *) NULL)
random_info=AcquireRandomInfo();
file=(-1);
for (i=0; i < TMP_MAX; i++)
{
/*
Get temporary pathname.
*/
(void) GetPathTemplate(path);
#if defined(MAGICKCORE_HAVE_MKSTEMP)
file=mkstemp(path);
#if defined(__OS2__)
setmode(file,O_BINARY);
#endif
if (file != -1)
break;
#endif
key=GetRandomKey(random_info,8);
p=path+strlen(path)-8;
datum=GetStringInfoDatum(key);
for (i=0; i < 8; i++)
{
c=(int) (datum[i] & 0x3f);
*p++=portable_filename[c];
}
key=DestroyStringInfo(key);
file=open(path,O_RDWR | O_CREAT | O_EXCL | O_BINARY | O_NOFOLLOW,S_MODE);
if ((file > 0) || (errno != EEXIST))
break;
}
(void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
if (file == -1)
return(file);
if (resource_semaphore == (SemaphoreInfo *) NULL)
AcquireSemaphoreInfo(&resource_semaphore);
LockSemaphoreInfo(resource_semaphore);
if (temporary_resources == (SplayTreeInfo *) NULL)
temporary_resources=NewSplayTree(CompareSplayTreeString,
RelinquishMagickMemory,DestroyTemporaryResources);
UnlockSemaphoreInfo(resource_semaphore);
resource=ConstantString(path);
(void) AddValueToSplayTree(temporary_resources,resource,resource);
return(file);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% G e t M a g i c k R e s o u r c e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% GetMagickResource() returns the specified resource.
%
% The format of the GetMagickResource() method is:
%
% MagickSizeType GetMagickResource(const ResourceType type)
%
% A description of each parameter follows:
%
% o type: the type of resource.
%
*/
MagickExport MagickSizeType GetMagickResource(const ResourceType type)
{
MagickSizeType
resource;
resource=0;
LockSemaphoreInfo(resource_semaphore);
switch (type)
{
case AreaResource:
{
resource=(MagickSizeType) resource_info.area;
break;
}
case MemoryResource:
{
resource=(MagickSizeType) resource_info.memory;
break;
}
case MapResource:
{
resource=(MagickSizeType) resource_info.map;
break;
}
case DiskResource:
{
resource=(MagickSizeType) resource_info.disk;
break;
}
case FileResource:
{
resource=(MagickSizeType) resource_info.file;
break;
}
case ThreadResource:
{
resource=(MagickSizeType) resource_info.thread;
break;
}
case TimeResource:
{
resource=(MagickSizeType) resource_info.time;
break;
}
default:
break;
}
UnlockSemaphoreInfo(resource_semaphore);
return(resource);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% G e t M a g i c k R e s o u r c e L i m i t %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% GetMagickResource() returns the specified resource limit.
%
% The format of the GetMagickResourceLimit() method is:
%
% MagickSizeType GetMagickResourceLimit(const ResourceType type)
%
% A description of each parameter follows:
%
% o type: the type of resource.
%
*/
MagickExport MagickSizeType GetMagickResourceLimit(const ResourceType type)
{
MagickSizeType
resource;
resource=0;
if (resource_semaphore == (SemaphoreInfo *) NULL)
AcquireSemaphoreInfo(&resource_semaphore);
LockSemaphoreInfo(resource_semaphore);
switch (type)
{
case AreaResource:
{
resource=resource_info.area_limit;
break;
}
case MemoryResource:
{
resource=resource_info.memory_limit;
break;
}
case MapResource:
{
resource=resource_info.map_limit;
break;
}
case DiskResource:
{
resource=resource_info.disk_limit;
break;
}
case FileResource:
{
resource=resource_info.file_limit;
break;
}
case ThreadResource:
{
resource=resource_info.thread_limit;
break;
}
case TimeResource:
{
resource=resource_info.time_limit;
break;
}
default:
break;
}
UnlockSemaphoreInfo(resource_semaphore);
return(resource);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% L i s t M a g i c k R e s o u r c e I n f o %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ListMagickResourceInfo() lists the resource info to a file.
%
% The format of the ListMagickResourceInfo method is:
%
% MagickBooleanType ListMagickResourceInfo(FILE *file,
% ExceptionInfo *exception)
%
% A description of each parameter follows.
%
% o file: An pointer to a FILE.
%
% o exception: return any errors or warnings in this structure.
%
*/
MagickExport MagickBooleanType ListMagickResourceInfo(FILE *file,
ExceptionInfo *magick_unused(exception))
{
char
area_limit[MaxTextExtent],
disk_limit[MaxTextExtent],
map_limit[MaxTextExtent],
memory_limit[MaxTextExtent],
time_limit[MaxTextExtent];
if (file == (const FILE *) NULL)
file=stdout;
if (resource_semaphore == (SemaphoreInfo *) NULL)
AcquireSemaphoreInfo(&resource_semaphore);
LockSemaphoreInfo(resource_semaphore);
(void) FormatMagickSize(resource_info.area_limit,MagickFalse,area_limit);
(void) FormatMagickSize(resource_info.memory_limit,MagickTrue,memory_limit);
(void) FormatMagickSize(resource_info.map_limit,MagickTrue,map_limit);
(void) FormatMagickSize(resource_info.disk_limit,MagickFalse,disk_limit);
(void) CopyMagickString(time_limit,"unlimited",MaxTextExtent);
if (resource_info.time_limit != MagickResourceInfinity)
(void) FormatMagickString(time_limit,MaxTextExtent,"%lu",(unsigned long)
resource_info.time_limit);
(void) fprintf(file,"File Area Memory Map"
" Disk Thread Time\n");
(void) fprintf(file,"--------------------------------------------------------"
"-----------------------\n");
(void) fprintf(file,"%4lu %11s %11s %11s %11s %6lu %11s\n",
(unsigned long) resource_info.file_limit,area_limit,memory_limit,map_limit,
disk_limit,(unsigned long) resource_info.thread_limit,time_limit);
(void) fflush(file);
UnlockSemaphoreInfo(resource_semaphore);
return(MagickTrue);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e l i n q u i s h M a g i c k R e s o u r c e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% RelinquishMagickResource() relinquishes resources of the specified type.
%
% The format of the RelinquishMagickResource() method is:
%
% void RelinquishMagickResource(const ResourceType type,
% const MagickSizeType size)
%
% A description of each parameter follows:
%
% o type: the type of resource.
%
% o size: the size of the resource.
%
*/
MagickExport void RelinquishMagickResource(const ResourceType type,
const MagickSizeType size)
{
char
resource_current[MaxTextExtent],
resource_limit[MaxTextExtent],
resource_request[MaxTextExtent];
(void) FormatMagickSize(size,MagickFalse,resource_request);
if (resource_semaphore == (SemaphoreInfo *) NULL)
AcquireSemaphoreInfo(&resource_semaphore);
LockSemaphoreInfo(resource_semaphore);
switch (type)
{
case AreaResource:
{
resource_info.area=(MagickOffsetType) size;
(void) FormatMagickSize((MagickSizeType) resource_info.area,MagickFalse,
resource_current);
(void) FormatMagickSize(resource_info.area_limit,MagickFalse,
resource_limit);
break;
}
case MemoryResource:
{
resource_info.memory-=size;
(void) FormatMagickSize((MagickSizeType) resource_info.memory,
MagickTrue,resource_current);
(void) FormatMagickSize(resource_info.memory_limit,MagickTrue,
resource_limit);
break;
}
case MapResource:
{
resource_info.map-=size;
(void) FormatMagickSize((MagickSizeType) resource_info.map,MagickTrue,
resource_current);
(void) FormatMagickSize(resource_info.map_limit,MagickTrue,
resource_limit);
break;
}
case DiskResource:
{
resource_info.disk-=size;
(void) FormatMagickSize((MagickSizeType) resource_info.disk,MagickFalse,
resource_current);
(void) FormatMagickSize(resource_info.disk_limit,MagickFalse,
resource_limit);
break;
}
case FileResource:
{
resource_info.file-=size;
(void) FormatMagickSize((MagickSizeType) resource_info.file,MagickFalse,
resource_current);
(void) FormatMagickSize((MagickSizeType) resource_info.file_limit,
MagickFalse,resource_limit);
break;
}
case ThreadResource:
{
resource_info.thread-=size;
(void) FormatMagickSize((MagickSizeType) resource_info.thread,MagickFalse,
resource_current);
(void) FormatMagickSize((MagickSizeType) resource_info.thread_limit,
MagickFalse,resource_limit);
break;
}
case TimeResource:
{
resource_info.time-=size;
(void) FormatMagickSize((MagickSizeType) resource_info.time,MagickFalse,
resource_current);
(void) FormatMagickSize((MagickSizeType) resource_info.time_limit,
MagickFalse,resource_limit);
break;
}
default:
break;
}
UnlockSemaphoreInfo(resource_semaphore);
(void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s: %s/%s/%s",
MagickOptionToMnemonic(MagickResourceOptions,(long) type),resource_request,
resource_current,resource_limit);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e l i n q u i s h U n i q u e F i l e R e s o u r c e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% RelinquishUniqueFileResource() relinquishes a unique file resource.
%
% The format of the RelinquishUniqueFileResource() method is:
%
% MagickBooleanType RelinquishUniqueFileResource(const char *path)
%
% A description of each parameter follows:
%
% o name: the name of the temporary resource.
%
*/
MagickExport MagickBooleanType RelinquishUniqueFileResource(const char *path)
{
char
cache_path[MaxTextExtent];
assert(path != (const char *) NULL);
(void) LogMagickEvent(ResourceEvent,GetMagickModule(),"%s",path);
if (temporary_resources != (SplayTreeInfo *) NULL)
{
register char
*p;
ResetSplayTreeIterator(temporary_resources);
p=(char *) GetNextKeyInSplayTree(temporary_resources);
while (p != (char *) NULL)
{
if (LocaleCompare(p,path) == 0)
break;
p=(char *) GetNextKeyInSplayTree(temporary_resources);
}
if (p != (char *) NULL)
(void) DeleteNodeFromSplayTree(temporary_resources,p);
}
(void) CopyMagickString(cache_path,path,MaxTextExtent);
AppendImageFormat("cache",cache_path);
(void) remove(cache_path);
return(remove(path) == 0 ? MagickTrue : MagickFalse);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ R e s o u r c e C o m p o n e n t G e n e s i s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ResourceComponentGenesis() instantiates the resource component.
%
% The format of the ResourceComponentGenesis method is:
%
% MagickBooleanType ResourceComponentGenesis(void)
%
*/
static inline unsigned long MagickMax(const unsigned long x,
const unsigned long y)
{
if (x > y)
return(x);
return(y);
}
static inline MagickSizeType StringToSizeType(const char *string,
const double interval)
{
double
value;
value=SiPrefixToDouble(string,interval);
if (value >= (double) MagickULLConstant(~0))
return(MagickULLConstant(~0));
return((MagickSizeType) value);
}
MagickExport MagickBooleanType ResourceComponentGenesis(void)
{
char
*limit;
long
files,
pages,
pagesize;
MagickSizeType
memory;
/*
Set Magick resource limits.
*/
AcquireSemaphoreInfo(&resource_semaphore);
pagesize=GetMagickPageSize();
pages=(-1);
#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
pages=sysconf(_SC_PHYS_PAGES);
#endif
memory=(MagickSizeType) pages*pagesize;
if ((pagesize <= 0) || (pages <= 0))
memory=2048UL*1024UL*1024UL;
#if defined(PixelCacheThreshold)
memory=PixelCacheThreshold;
#endif
(void) SetMagickResourceLimit(AreaResource,2UL*memory);
(void) SetMagickResourceLimit(MemoryResource,3UL*memory/2UL);
(void) SetMagickResourceLimit(MapResource,4UL*memory);
limit=GetEnvironmentValue("MAGICK_AREA_LIMIT");
if (limit == (char *) NULL)
limit=GetPolicyValue("area");
if (limit != (char *) NULL)
{
(void) SetMagickResourceLimit(AreaResource,StringToSizeType(limit,100.0));
limit=DestroyString(limit);
}
limit=GetEnvironmentValue("MAGICK_MEMORY_LIMIT");
if (limit == (char *) NULL)
limit=GetPolicyValue("memory");
if (limit != (char *) NULL)
{
(void) SetMagickResourceLimit(MemoryResource,
StringToSizeType(limit,100.0));
limit=DestroyString(limit);
}
limit=GetEnvironmentValue("MAGICK_MAP_LIMIT");
if (limit == (char *) NULL)
limit=GetPolicyValue("map");
if (limit != (char *) NULL)
{
(void) SetMagickResourceLimit(MapResource,StringToSizeType(limit,100.0));
limit=DestroyString(limit);
}
limit=GetEnvironmentValue("MAGICK_DISK_LIMIT");
if (limit == (char *) NULL)
limit=GetPolicyValue("disk");
if (limit != (char *) NULL)
{
(void) SetMagickResourceLimit(DiskResource,StringToSizeType(limit,100.0));
limit=DestroyString(limit);
}
files=(-1);
#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
files=sysconf(_SC_OPEN_MAX);
#endif
#if defined(MAGICKCORE_HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
if (files < 0)
{
struct rlimit
resources;
if (getrlimit(RLIMIT_NOFILE,&resources) != -1)
files=resources.rlim_cur;
}
#endif
#if defined(MAGICKCORE_HAVE_GETDTABLESIZE) && defined(MAGICKCORE_POSIX_SUPPORT)
if (files < 0)
files=getdtablesize();
#endif
if (files < 0)
files=64;
(void) SetMagickResourceLimit(FileResource,MagickMax((unsigned long)
(3*files/4),64));
limit=GetEnvironmentValue("MAGICK_FILE_LIMIT");
if (limit == (char *) NULL)
limit=GetPolicyValue("file");
if (limit != (char *) NULL)
{
(void) SetMagickResourceLimit(FileResource,StringToSizeType(limit,100.0));
limit=DestroyString(limit);
}
(void) SetMagickResourceLimit(ThreadResource,GetOpenMPMaximumThreads());
limit=GetEnvironmentValue("MAGICK_THREAD_LIMIT");
if (limit == (char *) NULL)
limit=GetPolicyValue("thread");
if (limit != (char *) NULL)
{
SetOpenMPMaximumThreads(StringToUnsignedLong(limit));
(void) SetMagickResourceLimit(ThreadResource,StringToSizeType(limit,
100.0));
limit=DestroyString(limit);
}
limit=GetEnvironmentValue("MAGICK_TIME_LIMIT");
if (limit == (char *) NULL)
limit=GetPolicyValue("time");
if (limit != (char *) NULL)
{
(void) SetMagickResourceLimit(TimeResource,StringToSizeType(limit,100.0));
limit=DestroyString(limit);
}
return(MagickTrue);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
+ R e s o u r c e C o m p o n e n t T e r m i n u s %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ResourceComponentTerminus() destroys the resource component.
%
% The format of the ResourceComponentTerminus() method is:
%
% ResourceComponentTerminus(void)
%
*/
MagickExport void ResourceComponentTerminus(void)
{
if (resource_semaphore == (SemaphoreInfo *) NULL)
AcquireSemaphoreInfo(&resource_semaphore);
LockSemaphoreInfo(resource_semaphore);
if (temporary_resources != (SplayTreeInfo *) NULL)
temporary_resources=DestroySplayTree(temporary_resources);
if (random_info != (RandomInfo *) NULL)
random_info=DestroyRandomInfo(random_info);
UnlockSemaphoreInfo(resource_semaphore);
DestroySemaphoreInfo(&resource_semaphore);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% S e t M a g i c k R e s o u r c e L i m i t %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% SetMagickResourceLimit() sets the limit for a particular resource.
%
% The format of the SetMagickResourceLimit() method is:
%
% MagickBooleanType SetMagickResourceLimit(const ResourceType type,
% const MagickSizeType limit)
%
% A description of each parameter follows:
%
% o type: the type of resource.
%
% o limit: the maximum limit for the resource.
%
*/
MagickExport MagickBooleanType SetMagickResourceLimit(const ResourceType type,
const MagickSizeType limit)
{
if (resource_semaphore == (SemaphoreInfo *) NULL)
AcquireSemaphoreInfo(&resource_semaphore);
LockSemaphoreInfo(resource_semaphore);
switch (type)
{
case AreaResource:
{
resource_info.area_limit=limit;
break;
}
case MemoryResource:
{
resource_info.memory_limit=limit;
break;
}
case MapResource:
{
resource_info.map_limit=limit;
break;
}
case DiskResource:
{
resource_info.disk_limit=limit;
break;
}
case FileResource:
{
resource_info.file_limit=limit;
break;
}
case ThreadResource:
{
SetOpenMPMaximumThreads((unsigned long) limit);
resource_info.thread_limit=GetOpenMPMaximumThreads();
break;
}
case TimeResource:
{
resource_info.time_limit=limit;
break;
}
default:
break;
}
UnlockSemaphoreInfo(resource_semaphore);
return(MagickTrue);
}
|