@@ -33,7 +33,8 @@ static void setup_oom_handling_cgroup_v2(int pid);
3333static void setup_oom_handling_cgroup_v1 (int pid );
3434static gboolean oom_cb_cgroup_v2 (int fd , GIOCondition condition , G_GNUC_UNUSED gpointer user_data );
3535static gboolean oom_cb_cgroup_v1 (int fd , GIOCondition condition , G_GNUC_UNUSED gpointer user_data );
36- static int write_oom_files ();
36+ static int create_oom_files ();
37+ static int create_oom_file (const char * base_path );
3738
3839void setup_oom_handling (int pid )
3940{
@@ -261,7 +262,7 @@ static gboolean oom_cb_cgroup_v1(int fd, GIOCondition condition, gpointer user_d
261262
262263 /* we catch the two other cases here, both of which are OOM kill events */
263264 ninfo ("OOM event received" );
264- write_oom_files ();
265+ create_oom_files ();
265266
266267 return G_SOURCE_CONTINUE ;
267268}
@@ -306,33 +307,39 @@ gboolean check_cgroup2_oom()
306307 continue ;
307308
308309 if (counter != last_counter ) {
309- if (write_oom_files () == 0 )
310+ if (create_oom_files () == 0 )
310311 last_counter = counter ;
311312 }
312313 return G_SOURCE_CONTINUE ;
313314 }
314315 return G_SOURCE_REMOVE ;
315316}
316317
317- /* write the appropriate files to tell the caller there was an oom event
318- * this can be used for v1 and v2 OOMS
318+ /* create the appropriate files to tell the caller there was an oom event
319+ * this can be used for v1 and v2 OOMs
319320 * returns 0 on success, negative value on failure
320321 */
321- static int write_oom_files ()
322+ static int create_oom_files ()
322323{
323324 ninfo ("OOM received" );
324- if (opt_persist_path ) {
325- _cleanup_free_ char * ctr_oom_file_path = g_build_filename (opt_persist_path , "oom" , NULL );
326- _cleanup_close_ int ctr_oom_fd = open (ctr_oom_file_path , O_CREAT | O_CLOEXEC , 0666 );
327- if (ctr_oom_fd < 0 ) {
328- nwarn ("Failed to write oom file" );
329- }
330- }
331- _cleanup_close_ int oom_fd = open ("oom" , O_CREAT | O_CLOEXEC , 0666 );
332- if (oom_fd < 0 ) {
333- nwarn ("Failed to write oom file" );
325+ int r = 0 ;
326+ r |= create_oom_file (opt_persist_path );
327+ r |= create_oom_file (opt_bundle_path );
328+ return r ;
329+ }
330+
331+ static int create_oom_file (const char * base_path )
332+ {
333+ if (base_path == NULL || base_path [0 ] == '\0' )
334+ return 0 ;
335+
336+ _cleanup_free_ char * ctr_oom_file_path = g_build_filename (base_path , "oom" , NULL );
337+ _cleanup_close_ int ctr_oom_fd = open (ctr_oom_file_path , O_CREAT | O_CLOEXEC , 0666 );
338+ if (ctr_oom_fd < 0 ) {
339+ nwarnf ("Failed to write oom file to the %s path" , base_path );
340+ return -1 ;
334341 }
335- return oom_fd >= 0 ? 0 : -1 ;
342+ return 0 ;
336343}
337344
338345#endif
0 commit comments