< prev index next >

src/os/bsd/vm/perfMemory_bsd.cpp

Print this page


   1 /*
   2  * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 358 //
 359 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
 360 
 361   // Open the directory.
 362   DIR* dirp = open_directory_secure(dirname);
 363   if (dirp == NULL) {
 364     // Directory doesn't exist or is insecure, so there is nothing to cleanup.
 365     return dirp;
 366   }
 367   int fd = dirfd(dirp);
 368 
 369   // Open a fd to the cwd and save it off.
 370   int result;
 371   RESTARTABLE(::open(".", O_RDONLY), result);
 372   if (result == OS_ERR) {
 373     *saved_cwd_fd = -1;
 374   } else {
 375     *saved_cwd_fd = result;
 376   }
 377 
 378   // Set the current directory to dirname by using the fd of the directory.

 379   result = fchdir(fd);
 380 











 381   return dirp;

 382 }
 383 
 384 // Close the directory and restore the current working directory.
 385 //
 386 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
 387 
 388   int result;
 389   // If we have a saved cwd change back to it and close the fd.
 390   if (saved_cwd_fd != -1) {
 391     result = fchdir(saved_cwd_fd);
 392     ::close(saved_cwd_fd);
 393   }
 394 
 395   // Close the directory.
 396   os::closedir(dirp);
 397 }
 398 
 399 // Check if the given file descriptor is considered a secure.
 400 //
 401 static bool is_file_secure(int fd, const char *filename) {


   1 /*
   2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


 358 //
 359 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
 360 
 361   // Open the directory.
 362   DIR* dirp = open_directory_secure(dirname);
 363   if (dirp == NULL) {
 364     // Directory doesn't exist or is insecure, so there is nothing to cleanup.
 365     return dirp;
 366   }
 367   int fd = dirfd(dirp);
 368 
 369   // Open a fd to the cwd and save it off.
 370   int result;
 371   RESTARTABLE(::open(".", O_RDONLY), result);
 372   if (result == OS_ERR) {
 373     *saved_cwd_fd = -1;
 374   } else {
 375     *saved_cwd_fd = result;
 376   }
 377 
 378   // Set the current directory to dirname by using the fd of the directory and
 379   // handle errors, otherwise shared memory files will be created in cwd.
 380   result = fchdir(fd);
 381   if (result == OS_ERR) {
 382     if (PrintMiscellaneous && Verbose) {
 383       warning("could not change to directory %s", dirname);
 384     }
 385     if (*saved_cwd_fd != -1) {
 386       ::close(*saved_cwd_fd);
 387       *saved_cwd_fd = -1;
 388     }
 389     // Close the directory.
 390     os::closedir(dirp);
 391     return NULL;
 392   } else {
 393     return dirp;
 394   }
 395 }
 396 
 397 // Close the directory and restore the current working directory.
 398 //
 399 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
 400 
 401   int result;
 402   // If we have a saved cwd change back to it and close the fd.
 403   if (saved_cwd_fd != -1) {
 404     result = fchdir(saved_cwd_fd);
 405     ::close(saved_cwd_fd);
 406   }
 407 
 408   // Close the directory.
 409   os::closedir(dirp);
 410 }
 411 
 412 // Check if the given file descriptor is considered a secure.
 413 //
 414 static bool is_file_secure(int fd, const char *filename) {


< prev index next >