@@ -707,14 +707,112 @@ static int bus_cgroup_set_boolean(
707707 return 1 ; \
708708 }
709709
710+ #define BUS_DEFINE_SET_CGROUP_PROTECTION (function , mask , scale ) \
711+ static int bus_cgroup_set_##function( \
712+ Unit *u, \
713+ const char *name, \
714+ uint64_t *p, \
715+ bool *s, \
716+ sd_bus_message *message, \
717+ UnitWriteFlags flags, \
718+ sd_bus_error *error) { \
719+ \
720+ uint64_t v = CGROUP_LIMIT_MIN; \
721+ bool nonempty = true; \
722+ char type; \
723+ int r; \
724+ \
725+ assert(p); \
726+ assert(s); \
727+ \
728+ r = sd_bus_message_peek_type(message, &type, NULL); \
729+ if (r < 0) \
730+ return r; \
731+ if (type == SD_BUS_TYPE_BOOLEAN) { \
732+ r = sd_bus_message_read(message, "b", &nonempty); \
733+ if (r < 0) \
734+ return r; \
735+ /* Bool is used to denote empty value only */ \
736+ if (nonempty ) \
737+ return - EINVAL ; \
738+ } else if (type != SD_BUS_TYPE_UINT64 ) { \
739+ return - EINVAL ; \
740+ } else { \
741+ r = sd_bus_message_read (message , "t" , & v ); \
742+ if (r < 0 ) \
743+ return r ; \
744+ } \
745+ \
746+ if (!UNIT_WRITE_FLAGS_NOOP (flags )) { \
747+ * p = v ; \
748+ unit_invalidate_cgroup (u , mask ); \
749+ if (!nonempty ) { \
750+ * s = false; \
751+ unit_write_settingf (u , flags , name , \
752+ "%s=" , name ); \
753+ } else if (v == CGROUP_LIMIT_MAX ) { \
754+ * s = true; \
755+ unit_write_settingf (u , flags , name , \
756+ "%s=infinity" , name ); \
757+ } else { \
758+ * s = true; \
759+ unit_write_settingf (u , flags , name , \
760+ "%s=%" PRIu64 , name , v ); \
761+ } \
762+ } \
763+ \
764+ return 1 ; \
765+ } \
766+ static int bus_cgroup_set_ ##function ##_scale( \
767+ Unit *u, \
768+ const char *name, \
769+ uint64_t *p, \
770+ bool *s, \
771+ sd_bus_message *message, \
772+ UnitWriteFlags flags, \
773+ sd_bus_error *error) { \
774+ \
775+ uint64_t v; \
776+ uint32_t raw; \
777+ int r; \
778+ \
779+ assert(p); \
780+ assert(s); \
781+ \
782+ r = sd_bus_message_read(message, "u", &raw); \
783+ if (r < 0) \
784+ return r; \
785+ \
786+ v = scale(raw, UINT32_MAX); \
787+ if (v >= UINT64_MAX) \
788+ return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, \
789+ "Value specified in %s is out of range", name); \
790+ \
791+ if (!UNIT_WRITE_FLAGS_NOOP(flags)) { \
792+ *p = v; \
793+ unit_invalidate_cgroup(u, mask); \
794+ \
795+ /* Prepare to chop off suffix */ \
796+ assert_se (endswith (name , "Scale" )); \
797+ \
798+ uint32_t scaled = DIV_ROUND_UP ((uint64_t ) raw * 1000 , (uint64_t ) UINT32_MAX ); \
799+ unit_write_settingf (u , flags , name , "%.*s=%" PRIu32 ".%" PRIu32 "%%" , \
800+ (int )(strlen (name ) - strlen ("Scale" )), name , \
801+ scaled / 10 , scaled % 10 ); \
802+ } \
803+ \
804+ * s = true; \
805+ return 1 ; \
806+ }
807+
710808DISABLE_WARNING_TYPE_LIMITS ;
711809BUS_DEFINE_SET_CGROUP_WEIGHT (cpu_weight , CGROUP_MASK_CPU , CGROUP_WEIGHT_IS_OK , CGROUP_WEIGHT_INVALID );
712810BUS_DEFINE_SET_CGROUP_WEIGHT (cpu_shares , CGROUP_MASK_CPU , CGROUP_CPU_SHARES_IS_OK , CGROUP_CPU_SHARES_INVALID );
713811BUS_DEFINE_SET_CGROUP_WEIGHT (io_weight , CGROUP_MASK_IO , CGROUP_WEIGHT_IS_OK , CGROUP_WEIGHT_INVALID );
714812BUS_DEFINE_SET_CGROUP_WEIGHT (blockio_weight , CGROUP_MASK_BLKIO , CGROUP_BLKIO_WEIGHT_IS_OK , CGROUP_BLKIO_WEIGHT_INVALID );
715813BUS_DEFINE_SET_CGROUP_LIMIT (memory , CGROUP_MASK_MEMORY , physical_memory_scale , 1 );
716- BUS_DEFINE_SET_CGROUP_LIMIT (memory_protection , CGROUP_MASK_MEMORY , physical_memory_scale , 0 );
717814BUS_DEFINE_SET_CGROUP_LIMIT (swap , CGROUP_MASK_MEMORY , physical_memory_scale , 0 );
815+ BUS_DEFINE_SET_CGROUP_PROTECTION (memory_protection , CGROUP_MASK_MEMORY , physical_memory_scale );
718816REENABLE_WARNING ;
719817
720818static int bus_cgroup_set_tasks_max (
@@ -840,33 +938,17 @@ int bus_cgroup_set_property(
840938 if (streq (name , "MemoryAccounting" ))
841939 return bus_cgroup_set_boolean (u , name , & c -> memory_accounting , CGROUP_MASK_MEMORY , message , flags , error );
842940
843- if (streq (name , "MemoryMin" )) {
844- r = bus_cgroup_set_memory_protection (u , name , & c -> memory_min , message , flags , error );
845- if (r > 0 )
846- c -> memory_min_set = true;
847- return r ;
848- }
941+ if (streq (name , "MemoryMin" ))
942+ return bus_cgroup_set_memory_protection (u , name , & c -> memory_min , & c -> memory_min_set , message , flags , error );
849943
850- if (streq (name , "MemoryLow" )) {
851- r = bus_cgroup_set_memory_protection (u , name , & c -> memory_low , message , flags , error );
852- if (r > 0 )
853- c -> memory_low_set = true;
854- return r ;
855- }
944+ if (streq (name , "MemoryLow" ))
945+ return bus_cgroup_set_memory_protection (u , name , & c -> memory_low , & c -> memory_low_set , message , flags , error );
856946
857- if (streq (name , "DefaultMemoryMin" )) {
858- r = bus_cgroup_set_memory_protection (u , name , & c -> default_memory_min , message , flags , error );
859- if (r > 0 )
860- c -> default_memory_min_set = true;
861- return r ;
862- }
947+ if (streq (name , "DefaultMemoryMin" ))
948+ return bus_cgroup_set_memory_protection (u , name , & c -> default_memory_min , & c -> default_memory_min_set , message , flags , error );
863949
864- if (streq (name , "DefaultMemoryLow" )) {
865- r = bus_cgroup_set_memory_protection (u , name , & c -> default_memory_low , message , flags , error );
866- if (r > 0 )
867- c -> default_memory_low_set = true;
868- return r ;
869- }
950+ if (streq (name , "DefaultMemoryLow" ))
951+ return bus_cgroup_set_memory_protection (u , name , & c -> default_memory_low , & c -> default_memory_low_set , message , flags , error );
870952
871953 if (streq (name , "MemoryHigh" ))
872954 return bus_cgroup_set_memory (u , name , & c -> memory_high , message , flags , error );
@@ -880,33 +962,17 @@ int bus_cgroup_set_property(
880962 if (streq (name , "MemoryLimit" ))
881963 return bus_cgroup_set_memory (u , name , & c -> memory_limit , message , flags , error );
882964
883- if (streq (name , "MemoryMinScale" )) {
884- r = bus_cgroup_set_memory_protection_scale (u , name , & c -> memory_min , message , flags , error );
885- if (r > 0 )
886- c -> memory_min_set = true;
887- return r ;
888- }
965+ if (streq (name , "MemoryMinScale" ))
966+ return bus_cgroup_set_memory_protection_scale (u , name , & c -> memory_min , & c -> memory_min_set , message , flags , error );
889967
890- if (streq (name , "MemoryLowScale" )) {
891- r = bus_cgroup_set_memory_protection_scale (u , name , & c -> memory_low , message , flags , error );
892- if (r > 0 )
893- c -> memory_low_set = true;
894- return r ;
895- }
968+ if (streq (name , "MemoryLowScale" ))
969+ return bus_cgroup_set_memory_protection_scale (u , name , & c -> memory_low , & c -> memory_low_set , message , flags , error );
896970
897- if (streq (name , "DefaultMemoryMinScale" )) {
898- r = bus_cgroup_set_memory_protection_scale (u , name , & c -> default_memory_min , message , flags , error );
899- if (r > 0 )
900- c -> default_memory_min_set = true;
901- return r ;
902- }
971+ if (streq (name , "DefaultMemoryMinScale" ))
972+ return bus_cgroup_set_memory_protection_scale (u , name , & c -> default_memory_min , & c -> default_memory_min_set , message , flags , error );
903973
904- if (streq (name , "DefaultMemoryLowScale" )) {
905- r = bus_cgroup_set_memory_protection_scale (u , name , & c -> default_memory_low , message , flags , error );
906- if (r > 0 )
907- c -> default_memory_low_set = true;
908- return r ;
909- }
974+ if (streq (name , "DefaultMemoryLowScale" ))
975+ return bus_cgroup_set_memory_protection_scale (u , name , & c -> default_memory_low , & c -> default_memory_low_set , message , flags , error );
910976
911977 if (streq (name , "MemoryHighScale" ))
912978 return bus_cgroup_set_memory_scale (u , name , & c -> memory_high , message , flags , error );
0 commit comments