hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

self+linux.ha (585B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 use fs;
      6 use io;
      7 use os;
      8 
      9 // Opens the executing process's binary image.
     10 export fn self() (image | io::error | fs::error) = {
     11 	const file = match (os::open("/proc/self/exe")) {
     12 	case let file: io::file =>
     13 		yield file;
     14 	case =>
     15 		// procfs may not be available, try fallback
     16 		return self_argv();
     17 	};
     18 
     19 	match (open(file)) {
     20 	case let img: image =>
     21 		return img;
     22 	case let err: io::error =>
     23 		return err;
     24 	case errors::invalid =>
     25 		abort("Running program image is not a valid ELF file");
     26 	};
     27 };