@@ -999,6 +999,7 @@ static void source_free(sd_event_source *s) {
999999 free (s -> description );
10001000 free (s );
10011001}
1002+ DEFINE_TRIVIAL_CLEANUP_FUNC (sd_event_source * , source_free );
10021003
10031004static int source_set_pending (sd_event_source * s , bool b ) {
10041005 int r ;
@@ -1092,7 +1093,7 @@ _public_ int sd_event_add_io(
10921093 sd_event_io_handler_t callback ,
10931094 void * userdata ) {
10941095
1095- sd_event_source * s ;
1096+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
10961097 int r ;
10971098
10981099 assert_return (e , - EINVAL );
@@ -1115,13 +1116,12 @@ _public_ int sd_event_add_io(
11151116 s -> enabled = SD_EVENT_ON ;
11161117
11171118 r = source_io_register (s , s -> enabled , events );
1118- if (r < 0 ) {
1119- source_free (s );
1119+ if (r < 0 )
11201120 return r ;
1121- }
11221121
11231122 if (ret )
11241123 * ret = s ;
1124+ TAKE_PTR (s );
11251125
11261126 return 0 ;
11271127}
@@ -1196,7 +1196,7 @@ _public_ int sd_event_add_time(
11961196 void * userdata ) {
11971197
11981198 EventSourceType type ;
1199- sd_event_source * s ;
1199+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
12001200 struct clock_data * d ;
12011201 int r ;
12021202
@@ -1248,20 +1248,17 @@ _public_ int sd_event_add_time(
12481248
12491249 r = prioq_put (d -> earliest , s , & s -> time .earliest_index );
12501250 if (r < 0 )
1251- goto fail ;
1251+ return r ;
12521252
12531253 r = prioq_put (d -> latest , s , & s -> time .latest_index );
12541254 if (r < 0 )
1255- goto fail ;
1255+ return r ;
12561256
12571257 if (ret )
12581258 * ret = s ;
1259+ TAKE_PTR (s );
12591260
12601261 return 0 ;
1261-
1262- fail :
1263- source_free (s );
1264- return r ;
12651262}
12661263
12671264static int signal_exit_callback (sd_event_source * s , const struct signalfd_siginfo * si , void * userdata ) {
@@ -1277,7 +1274,7 @@ _public_ int sd_event_add_signal(
12771274 sd_event_signal_handler_t callback ,
12781275 void * userdata ) {
12791276
1280- sd_event_source * s ;
1277+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
12811278 struct signal_data * d ;
12821279 sigset_t ss ;
12831280 int r ;
@@ -1317,16 +1314,15 @@ _public_ int sd_event_add_signal(
13171314 e -> signal_sources [sig ] = s ;
13181315
13191316 r = event_make_signal_data (e , sig , & d );
1320- if (r < 0 ) {
1321- source_free (s );
1317+ if (r < 0 )
13221318 return r ;
1323- }
13241319
13251320 /* Use the signal name as description for the event source by default */
13261321 (void ) sd_event_source_set_description (s , signal_to_string (sig ));
13271322
13281323 if (ret )
13291324 * ret = s ;
1325+ TAKE_PTR (s );
13301326
13311327 return 0 ;
13321328}
@@ -1339,7 +1335,7 @@ _public_ int sd_event_add_child(
13391335 sd_event_child_handler_t callback ,
13401336 void * userdata ) {
13411337
1342- sd_event_source * s ;
1338+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
13431339 int r ;
13441340
13451341 assert_return (e , - EINVAL );
@@ -1369,24 +1365,22 @@ _public_ int sd_event_add_child(
13691365 s -> enabled = SD_EVENT_ONESHOT ;
13701366
13711367 r = hashmap_put (e -> child_sources , PID_TO_PTR (pid ), s );
1372- if (r < 0 ) {
1373- source_free (s );
1368+ if (r < 0 )
13741369 return r ;
1375- }
13761370
13771371 e -> n_enabled_child_sources ++ ;
13781372
13791373 r = event_make_signal_data (e , SIGCHLD , NULL );
13801374 if (r < 0 ) {
13811375 e -> n_enabled_child_sources -- ;
1382- source_free (s );
13831376 return r ;
13841377 }
13851378
13861379 e -> need_process_child = true;
13871380
13881381 if (ret )
13891382 * ret = s ;
1383+ TAKE_PTR (s );
13901384
13911385 return 0 ;
13921386}
@@ -1397,7 +1391,7 @@ _public_ int sd_event_add_defer(
13971391 sd_event_handler_t callback ,
13981392 void * userdata ) {
13991393
1400- sd_event_source * s ;
1394+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
14011395 int r ;
14021396
14031397 assert_return (e , - EINVAL );
@@ -1415,13 +1409,12 @@ _public_ int sd_event_add_defer(
14151409 s -> enabled = SD_EVENT_ONESHOT ;
14161410
14171411 r = source_set_pending (s , true);
1418- if (r < 0 ) {
1419- source_free (s );
1412+ if (r < 0 )
14201413 return r ;
1421- }
14221414
14231415 if (ret )
14241416 * ret = s ;
1417+ TAKE_PTR (s );
14251418
14261419 return 0 ;
14271420}
@@ -1432,7 +1425,7 @@ _public_ int sd_event_add_post(
14321425 sd_event_handler_t callback ,
14331426 void * userdata ) {
14341427
1435- sd_event_source * s ;
1428+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
14361429 int r ;
14371430
14381431 assert_return (e , - EINVAL );
@@ -1454,13 +1447,12 @@ _public_ int sd_event_add_post(
14541447 s -> enabled = SD_EVENT_ON ;
14551448
14561449 r = set_put (e -> post_sources , s );
1457- if (r < 0 ) {
1458- source_free (s );
1450+ if (r < 0 )
14591451 return r ;
1460- }
14611452
14621453 if (ret )
14631454 * ret = s ;
1455+ TAKE_PTR (s );
14641456
14651457 return 0 ;
14661458}
@@ -1471,7 +1463,7 @@ _public_ int sd_event_add_exit(
14711463 sd_event_handler_t callback ,
14721464 void * userdata ) {
14731465
1474- sd_event_source * s ;
1466+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
14751467 int r ;
14761468
14771469 assert_return (e , - EINVAL );
@@ -1494,13 +1486,12 @@ _public_ int sd_event_add_exit(
14941486 s -> enabled = SD_EVENT_ONESHOT ;
14951487
14961488 r = prioq_put (s -> event -> exit , s , & s -> exit .prioq_index );
1497- if (r < 0 ) {
1498- source_free (s );
1489+ if (r < 0 )
14991490 return r ;
1500- }
15011491
15021492 if (ret )
15031493 * ret = s ;
1494+ TAKE_PTR (s );
15041495
15051496 return 0 ;
15061497}
@@ -1817,11 +1808,10 @@ _public_ int sd_event_add_inotify(
18171808 sd_event_inotify_handler_t callback ,
18181809 void * userdata ) {
18191810
1820- bool rm_inotify = false, rm_inode = false;
18211811 struct inotify_data * inotify_data = NULL ;
18221812 struct inode_data * inode_data = NULL ;
18231813 _cleanup_close_ int fd = -1 ;
1824- sd_event_source * s ;
1814+ _cleanup_ ( source_freep ) sd_event_source * s = NULL ;
18251815 struct stat st ;
18261816 int r ;
18271817
@@ -1859,13 +1849,13 @@ _public_ int sd_event_add_inotify(
18591849 /* Allocate an inotify object for this priority, and an inode object within it */
18601850 r = event_make_inotify_data (e , SD_EVENT_PRIORITY_NORMAL , & inotify_data );
18611851 if (r < 0 )
1862- goto fail ;
1863- rm_inotify = r > 0 ;
1852+ return r ;
18641853
18651854 r = event_make_inode_data (e , inotify_data , st .st_dev , st .st_ino , & inode_data );
1866- if (r < 0 )
1867- goto fail ;
1868- rm_inode = r > 0 ;
1855+ if (r < 0 ) {
1856+ event_free_inotify_data (e , inotify_data );
1857+ return r ;
1858+ }
18691859
18701860 /* Keep the O_PATH fd around until the first iteration of the loop, so that we can still change the priority of
18711861 * the event source, until then, for which we need the original inode. */
@@ -1878,30 +1868,18 @@ _public_ int sd_event_add_inotify(
18781868 LIST_PREPEND (inotify .by_inode_data , inode_data -> event_sources , s );
18791869 s -> inotify .inode_data = inode_data ;
18801870
1881- rm_inode = rm_inotify = false;
1882-
18831871 /* Actually realize the watch now */
18841872 r = inode_data_realize_watch (e , inode_data );
18851873 if (r < 0 )
1886- goto fail ;
1874+ return r ;
18871875
18881876 (void ) sd_event_source_set_description (s , path );
18891877
18901878 if (ret )
18911879 * ret = s ;
1880+ TAKE_PTR (s );
18921881
18931882 return 0 ;
1894-
1895- fail :
1896- source_free (s );
1897-
1898- if (rm_inode )
1899- event_free_inode_data (e , inode_data );
1900-
1901- if (rm_inotify )
1902- event_free_inotify_data (e , inotify_data );
1903-
1904- return r ;
19051883}
19061884
19071885static sd_event_source * event_source_free (sd_event_source * s ) {
0 commit comments