1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283(**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)modulePath=Hack_path(* Initializes the unix domain socket *)letunix_socketsock_name=trySys_utils.with_umask0o111beginfun()->Sys_utils.mkdir_no_fail(Filename.dirnamesock_name);ifSys.file_existssock_namethenSys.removesock_name;letdomain,addr=ifSys.win32thenUnix.(PF_INET,Unix.ADDR_INET(inet_addr_loopback,0))elseUnix.(PF_UNIX,Unix.ADDR_UNIXsock_name)inletsock=Unix.socketdomainUnix.SOCK_STREAM0inlet_=Unix.setsockoptsockUnix.SO_REUSEADDRtrueinlet_=Unix.bindsockaddrinlet_=Unix.listensock10inlet()=matchUnix.getsocknamesockwith|Unix.ADDR_UNIX_->()|Unix.ADDR_INET(_,port)->letoc=open_out_binsock_nameinoutput_binary_intocport;close_outocinsockendwithUnix.Unix_error(err,_,_)->Printf.eprintf"%s\n"(Unix.error_messageerr);Exit_status.(exitSocket_error)(* So the sockaddr_un structure puts a strict limit on the length of a socket
* address. This appears to be 104 chars on mac os x and 108 chars on my
* centos box. *)letmax_addr_length=103letmin_name_length=17letget_pathpath=(* Path will resolve the realpath, in case two processes are referring to the
* same socket using different paths (like with symlinks *)letpath=path|>Path.make|>Path.to_stringinletdir=(Filename.dirnamepath)^"/"inletfilename=Filename.basenamepathinletroot_part=Filename.chop_extensionfilenameinletroot_length=String.lengthroot_partinletextension_length=String.lengthfilename-root_lengthinletextension=String.subfilenameroot_lengthextension_lengthin(* It's possible that the directory path is too long. If so, let's give up and
* use /tmp/ *)letdir=ifString.lengthdir>max_addr_length-min_name_lengththenFilename.get_temp_dir_name()elsedirinletmax_root_part_length=max_addr_length-(String.lengthdir)-extension_lengthinletroot_part=ifroot_length>max_root_part_lengththenbeginletlen=String.lengthroot_partinletprefix=String.subroot_part05inletsuffix=String.subroot_part(len-5)5inletdigest=Digest.to_hex(Digest.stringroot_part)in(* 5 char prefix + 5 char suffix + 2 periods *)letmax_digest_length=max_root_part_length-12inletdigest_part=ifString.lengthdigest>max_digest_lengththenString.subdigest0max_digest_lengthelsedigestinprefix^"."^digest_part^"."^suffixendelseroot_partinFilename.concatdir(Printf.sprintf"%s%s"root_partextension)letinit_unix_socketsocket_file=unix_socket(get_pathsocket_file)