Showing posts with label MacOSX. Show all posts
Showing posts with label MacOSX. Show all posts

Mar 8, 2011

Mac OS X: Screen Sharing

How to enable a screen sharing of a remote Mac (10.6) if you forgot to enable it and your Mac is behind a firewall?

This is the question I asked myself today when I desperately was in need to use some GUI on my Mac Pro at work place, while I am on a business trip right now and have my laptop with me :(

Same as always with the Mac OS X - it appeared to be very easy to implement.

So, you have a remote Mac "A", which is behind a firewall and you want to connect to it via an open domain "B" (to where ssh is possible) from your local Mac (a laptop, for example).

STEP #1

Enable an SSH tunnel via an open domain to you remote Mac. On your local Mac just open a terminal and enter the following:

ssh -N -L 3000:loginnameA@hostnameA:5900 loginnameB@hostnameB

The command will create a tunnel and hang. When you are done terminate the command and the tunnel will be disabled.

STEP #2
Open another terminal on your local Mac and ssh to your remote Mac to enable a screen sharing:

sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -clientopts -setvnclegacy -vnclegacy yes -clientopts -setvncpw -vncpw mypasswd -restart -agent -privs -all

STEP #3
Start sharing the screen :)

In Safari enter the following URL: vnc://localhost:3000

Should work now :)

Disable screen sharing
SSH to your remote Mac and execute the command:

sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -deactivate -configure -access -off

Don't forget to terminate the SSH tunnel (Step #1).



Hope it helps some one to save time.

Feb 24, 2011

Apple Releases First Developer Preview of Mac OS X Lion

The following video shows some new animations and gestures:

Mar 3, 2010

malloc/free interception on Mac OS X

There is a very easy way to hook malloc/free calls on Linux and on other systems, which support the GNU C library. Simply because the GNU C library lets you modify the behavior of malloc, realloc, and free by specifying appropriate hook functions.

Now, how about hooking malloc/free calls on Mac OS X?

Actually it appears to be also not a difficult task in some terms.
The very simple way of changing the behavior of malloc, calloc, realloc, and free routines is to implement your own zone (you have to be very careful with this one) or one can override calls of the default zone, which is automatically created for each process.
Let's try the second one.

We assume, for example, that we want to track all allocations and deallocations in a given application on Mac OS X.
The first step to achieve our goal consists of asking for our default zone structure. The following statement is doing that:
malloc_zone_t *zone = malloc_default_zone();
Don't forget to check for errors, and check that you got your zone.
The next step is to save default zone's callbacks to some tmp storage. You can either save this zone to a static malloc_zone_t variable (save the entire structure):
...
static malloc_zone_t original_zone;
...
original_zone = *zone;

or you can save each callback, you need to override, to a tmp variables:

system_malloc = zone->malloc;
system_free = zone->free;

the same you can do for calloc and realloc and company...

The final step is to override default hooks with custom ones:

zone->malloc = &my_malloc;
zone->free = &my_free;

which could look like the following:

void *(*system_malloc)(malloc_zone_t *zone, size_t size);
...
void (*system_free)(malloc_zone_t *zone, void *ptr);
...
void my_free(malloc_zone_t *zone, void *ptr)
{
malloc_printf("free(zone=%p, ptr=%p)\n", zone, ptr);
system_free(zone, ptr); // or if you save the whole zone: (*original_zone.free)(zone, ptr);
}
and so on...

In my_free, my_malloc and my_whatever, we can call some of our custom code to track allocations/deallocations and (if needed) we call the default function accordingly to let default zone do its job.

Snow Leopard
There is on thing you should know if you want to run your malloc hook on the Snow Leopard. According to Apple's C library malloc.c, implementing free hook is not enough to handle all frees.
Check the implementation of the free function from the library:

void
free(void *ptr) {
malloc_zone_t *zone;
size_t size;
if (!ptr)
return;
zone = find_registered_zone(ptr, &size);
if (!zone) {
malloc_printf("*** error for object %p: pointer being freed was not allocated\n"
"*** set a breakpoint in malloc_error_break to debug\n", ptr);
malloc_error_break();
if ((malloc_debug_flags & (SCALABLE_MALLOC_ABORT_ON_CORRUPTION|SCALABLE_MALLOC_ABORT_ON_ERROR)))
abort();
} else if (zone->version >= 6 && zone->free_definite_size)
malloc_zone_free_definite_size(zone, ptr, size);
else
malloc_zone_free(zone, ptr);
}

As you can see, one should also override/hook a newly added call for malloc_zone_free_definite_size:

system_free_definite_size = zone->free_definite_size;
zone->free_definite_size = my_free_definite_size;

I would also use a condition before using free_definite_size. I prefer cmake, I therefore have something like that in my CMakeLists.txt:


#
# Check that malloc_zone_t has the free_definite_size memeber
#
if(APPLE)
include(CheckCSourceCompiles)
check_c_source_compiles("
#include
void main () {
malloc_zone_t zone;
zone.free_definite_size = NULL;
}
" APPLE_MALLOC_ZONE_FREE_DEFINITE_SIZE)
endif(APPLE)

and in the source code:

#if defined(APPLE_MALLOC_ZONE_FREE_DEFINITE_SIZE)
if (zone->version >= 6 && zone->free_definite_size)
{
system_free_definite_size = zone->free_definite_size;
zone->free_definite_size = my_free_definite_size;
}
#endif

That's It.
This technique works on Mac OS X 10.5-10.6. Enjoy!

Apr 15, 2009

Development under Mac OS X

Since long time already I have been not posting here... :( So, let's try to reload this blog...

With this post I am going to start a series of posts about development under Mac OS X. I will post about something I find interesting, difficult to implement, and some tricks I developed or just googled. :) Hope it helps to someone or at least it will help me to order my experience on that field.

Well, let's start from the environment.
I consider myself to be an experienced Windows and Linux developer. This stuff I do for many years already.
With Mac I met about five(!) moths ago. I mean, I knew it exists and I even worked briefly on it some years ago, but the truth is that, most of my projects were related to Windows or Linux.

I met Mac and that was love at first sight ;) As soon as I started to work on it I immediately suffered from the filling that I want to start an application development project under Mac and use it in my daily work... really!

As the hardware I use a new MacBook Pro, 15''



The only thing I can say is - WOW! It is a fucking amazing piece of hardware! Apple did a terrific job on this one.
I am just in love with the new trackpad device and the new keyboard of MacBook. No more words... Just get this laptop and enjoy!

BTW, I still do a lot of Linux development. In order to continue to do that on my Mac I use VMware Fusion. It works like a charm. One screen is a full screened Fedora 10 guest, another is a MS Windows XP guest and all under control of Mac OS X. ;)


The OS I am using is Mac OS X Leopard.
As the development environment I decided to use XCode (v. 3.1.2). Well, I am a bit disappointed with XCode and I tell you why. Mainly because of two thinks I see missing.
  1. A terrible SVN support. For example, I didn't find a way to set SVN properties on files. Also SVN plug-in sometimes living its own live and could be doing something, which blocks all version control operations on files in XCode. I didn't find a better way to just stop that plug-in by restarting XCode.
  2. No way to define custom formating rules. Formating preferences are very weak: a couple of checkboxes and that's it... If you do Objective-C development, then in a couple of weeks you will used to default formating and syntax coloring XCode does. But anyway, I would prefer to have a bit more flexible formating settings.
I think XCode is a very new and fresh product and Apple is developing it very fast (you can tell that from new thinks you get every new release). So I believe we will see soon a strong competitor to MS Visual Studio and to Eclipse. So far I would say MS VS is the best, at least for C++ development.

Anyway I would give a big plus to XCode and the best mark just for its very good integration with Objective-C and Cocoa. In combination with Interface Builder, which is by the way just the best GUI builder I have ever seen, XCode does all you need for application development.

James Bond will return... I mean, I will continue posting on that subject ;)