1

私は Mach 上の 2 つのプロセス間でメッセージを送信しようとしています (正確には、これは Mach マイクロカーネルを使用した Debian GNU/Hurd です)。これは私が持っているコードです:

#define _GNU_SOURCE

#include "machheader.h"

void 
send_integer( mach_port_t destination, int i )
{
    kern_return_t err;
    struct integer_message message;

    /* (i) Form the message : */

    /* (i.a) Fill the header fields : */
    message.head.msgh_bits = 
        MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_MAKE_SEND);
    message.head.msgh_size = sizeof( struct integer_message );
    message.head.msgh_local_port = MACH_PORT_NULL;
    message.head.msgh_remote_port = destination;

    /* (i.b) Explain the message type ( an integer ) */
    message.type.msgt_name = MACH_MSG_TYPE_INTEGER_32;
    message.type.msgt_size = 32;
    message.type.msgt_number = 1;
    message.type.msgt_inline = TRUE;
    message.type.msgt_longform = FALSE;
    message.type.msgt_deallocate = FALSE;
    /* message.type.msgt_unused = 0; */ /* not needed, I think */

    /* (i.c) Fill the message with the given integer : */
    message.inline_integer = i;

    /* (ii) Send the message : */
    err = mach_msg( &(message.head), MACH_SEND_MSG, 
            message.head.msgh_size, 0, MACH_PORT_NULL, 
            MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL );

    /* (iii) Analysis of the error code; 
    if succes, print and acknowledge message and return */
    if( err == MACH_MSG_SUCCESS )
      {
        printf( "success: the message was queued\n" );
      }
    else
      {
        perror( "error: some unexpected error ocurred!\n");
        exit(err);
      }

    return;
}

/* receive_integer is a function that receives an integer from some 
   mach port; it also hides the complexity of using the mach_msg 
   primitive to the user.

   receive_integer takes two arguments; the port where the message is going
   to come from with an integer inside, and a pointer to an integer in where
   the integer contained in the mentioned message will be stored.
*/
void 
receive_integer( mach_port_t source, int *ip )
{
    kern_return_t err;

    struct integer_message message;

    /* (i) Fill the little thing we know about the message : */
    /* message.head.msgh_size = sizeof(struct integer_message ); */

    /* (ii) Receive the message : */
    err = mach_msg( &(message.head), MACH_RCV_MSG, 0, 
            message.head.msgh_size, source,
            MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL );

    if( err == MACH_MSG_SUCCESS )
      {
        printf( "success: the message was received\n" );
      }
    else
      {
        perror( "error: Some unexpected error ocurred\n" );
        exit(err);
      }

    *ip = message.inline_integer;

    return;
}

/* main function of the program; it does the following :

   (i) allocate a port for receiving a message
   (ii) send a message containing an integer; 
   it uses the send_integer function
   (iii) receive the message and display it;
   it uses the receive_integer function
   (iv) deallocate the port
*/
int 
main( void )
{       
    //int s, r; /* s -> int sent, r -> int received */ 
    //mach_port_t destination;    

    kern_return_t err;

    /* Allocate a port to receive the bootstrap message : */
    err = mach_port_allocate( mach_task_self(), MACH_PORT_RIGHT_RECEIVE,
        &destination );

    if( err != KERN_SUCCESS )
      {
        perror( "Error : could not allocate any port\n" );
        exit(err);
      }

    if(!fork()){
        s=7;
        send_integer( destination, s );
    }else{
        receive_integer( destination, &r );
        printf("The received integer is : %d\n", r );
    }   

    mach_port_deallocate( mach_task_self(), destination );

    return(r);
} 

これは machheader.h です。

#include <mach.h>

#include <stdio.h>
#include <error.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>

struct integer_message
{
    mach_msg_header_t head;
    mach_msg_type_t type;

    int inline_integer;
};

int s, r;   /* s -> int sent, r -> int received */ 
mach_port_t destination;

アプリケーションを実行すると、次のようになります。

success: the message was queued

これは、メッセージが正常にキューに入れられたことを示していますが、そこで停止し、親プロセスのキューからの読み取りに進みません。何か案が?

4

1 に答える 1

1

このコードは、fork() 中に Hurd が Mach ポートを処理する方法のために機能しません。Mach ポートは受信権を 1 つしか持つことができないため、受信権を持つポートは fork() 中に複製され、送信権は (ファイル記述子のように) コピーされます。この場合、destinationfork() のときに受信権があるため、子はまったく新しいポートを取得します。その後、各プロセスは独自のポートへの受信権を持ち、2 つのポート間に接続はありません。

あなたが望むことを行うために私が見つけた最も簡単な方法は、Kalle Niemitalo の提案procです:システム内のすべてのプロセスのタスクポートへの送信権を保持し、一致する UID を持つ任意のプロセスにそれらを与えるHurd のサーバーを使用します。タスク ポートを使用すると、プロセスに対して必要なほぼすべての操作を実行できます。メモリの変更、スレッドの開始と停止、ポート スペースの変更などです。

したがって、この場合、子から親にメッセージを送信する必要があります。子は、親の PID を使用して親へのタスク ポートを取得し、親のdestinationポートへの送信権を抽出して、そのポートにメッセージを送信できます。このような:

if(!fork()){
    /* fork allocated a receive right in the child process, but it's not
       for the same port as in the parent process.  Deallocate that.  */
    mach_port_mod_refs (mach_task_self(), destination,
                        MACH_PORT_RIGHT_RECEIVE, -1);

    /* get a send right to the parent's task port */
    pid_t parent_pid = getppid ();
    task_t parent_task = pid2task (parent_pid);

    /* extract a send right to the parent's destination port */
    mach_port_t send_right;
    mach_port_type_t send_type;

    mach_port_extract_right (parent_task, destination,
                             MACH_MSG_TYPE_MAKE_SEND,
                             &send_right, &send_type);

    /* transmit to "send_right", not "destination" */
    s=7;
    send_integer( send_right, s );
}else{
    receive_integer( destination, &r );
    printf("The received integer is : %d\n", r );
}

子プロセスがこのように親プロセスを操作できるというのは、少し奇妙に思えるかもしれません。しかし、それはハードの特徴です。 proc非常に特権的なサーバーです。2 つのプロセスが同じ UID を持っているため、アクセスが許可されます。

別の方法として、fork() での Hurd の動作を変更することもできますが、生の Mach ポートを交換するアプリケーションはほとんどないため、これは意味がないと思います。いずれにせよ、Hurd での fork() は簡単なプロセスではありません。詳細については、こちらを参照してください。行 207 ~ 449 は、ポート権が子にコピーされる場所です。一見しただけで、Hurd の fork() がいかに複雑であるか、また、元のアイデアがうまくいかなかった理由の両方がわかるはずです。

于 2016-11-11T23:25:18.653 に答える