[SOLVED] Unable to compile my project

Hi, I'm having a problem compiling my code. I have defined a function to make a daemon out of the current process.
This is the file where the function is defined:

  1 extern crate libc;
  2 
  3 use std::ffi::*;
  4 use self::libc::*;
  5 
  6 pub enum DaemonErr {
  7     ForkingError,
  8     SessionError,
  9 }
 10 
 11 pub fn daemonize(working_dir: CString, mask: mode_t) -> Result<(), DaemonErr> {
 12     unsafe {
 13         if fork() != 0 { return Err(DaemonErr::ForkingError) }
 14         if setsid() < 0 { return Err(DaemonErr::SessionError) }
 15 
 16         signal(SIGHUP, SIG_IGN);
 17 
 18         if fork() != 0 { return Err(DaemonErr::ForkingError) }
 19 
 20         chdir(working_dir.as_ptr());
 21         umask(mask);
 22 
 23         for i in _SC_OPEN_MAX..0 {
 24             close(i);
 25         }
 26     }
 27 
 28     Ok(())
 29 }

And this is my main.rs:

  1 extern crate syslog;
  2 extern crate irc;
  3 extern crate daemon;
  4 
  5 use syslog::{ Facility, Severity };
  6 use syslog::Facility::*;
  7 
  8 use std::ffi::CString;
  9 use daemon::daemon::*;
 10 
 11 use irc::server::*;
 12 
 13 fn main() {
 14     let log = syslog::unix(LOG_DAEMON).unwrap();
 15 
 16     match daemonize(CString::new("/").unwrap(), 0) {
 17         Ok(()) => log.notice("oircd entered daemon mode..."),
 18         Err(DaemonErr) => log.crit("oircd was unable to daemonize!")
 19     }
 20 }

But when I want to compile and run it with cargo run I get this error message:

Compiling oircd v0.1.0 (file:///home/obi/Programming/Rust/oircd)
main.rs:16:5: 19:6 error: mismatched types:
 expected `()`,
    found `core::result::Result<usize, std::io::error::Error>`
(expected (),
    found enum `core::result::Result`) [E0308]
main.rs:16     match daemonize(CString::new("/").unwrap(), 0) {
main.rs:17         Ok(()) => log.notice("oircd entered daemon mode..."),
main.rs:18         Err(DaemonErr) => log.crit("oircd was unable to daemonize!")
main.rs:19     } 
main.rs:16:5: 19:6 help: run `rustc --explain E0308` to see a detailed explanation
error: aborting due to previous error
Could not compile `oircd`.

To learn more, run the command again with --verbose.

Sorry if this is a dumb question, but I'm new to rust and I just can't figure out what I've done wrong here.

I haven't checked what syslog does, but I think the return type of log.notice and log.crit is leaking through the match and out of your main function.

The main function is expected to return (), but because your match expression is the last statement of your function and it doesn't end in a semicolon, its value is implicitly the return value of main.

The fix should be to simply put a ; after the closing brace of your match.

Well, it's kind off emberrassing, but I just forgot to add in that semicolon...
However, thank you, it works with the semicolon in place.

This is a diagnostics issue. Rustc already suggests removing semicolons in certain _c_ases, it should suggest adding one, too.

1 Like